Skip to content
This repository has been archived by the owner on Sep 26, 2021. It is now read-only.

env emits DOCKER_HOST_IP #3057

Closed
wants to merge 1 commit into from
Closed

Conversation

msabramo
Copy link

This makes the env command emit a new environment variable called DOCKER_HOST_IP.

E.g.:

$ bin/docker-machine env
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.101:2376"
export DOCKER_HOST_IP="192.168.99.101"
export DOCKER_CERT_PATH="/Users/marca/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
# Run this command to configure your shell:
# eval $(bin/docker-machine env)

This is useful because it can be used in docker-compose.yml to make a container's external port bind to the Docker host's public IP address.

...
    ports:
      - "${DOCKER_HOST_IP}::8000"
...

I prefer to bind to the DOCKER_HOST_IP rather than letting it default to 0.0.0.0, because then I get useful output from docker-compose port and can do stuff like:

$ docker-compose port anonweb 8000
192.168.99.101:32775

$ curl -I http://$(docker-compose port anonweb 8000)/status/pid
HTTP/1.1 200 OK
Server: gunicorn/19.4.5
Date: Sun, 14 Feb 2016 05:41:58 GMT
Connection: close
Content-Type: application/json; charset=UTF-8
Content-Length: 113
SM-Request-ID: 37e72936-0c2a-44d3-8494-e0d6280387fd

See:
docker/compose#2915 (comment)

Cc: @sudarkoff

This is useful because it can be used in `docker-compose.yml` to make a
container's external port bind to the Docker host's public IP address.

See:
docker/compose#2915 (comment)
Signed-off-by: Marc Abramowitz <[email protected]>
@msabramo
Copy link
Author

Hmm, why did the documentation check fail? Let me know if there's something I can do on my end (document the new var?) to fix it.

@SvenDowideit
Copy link
Contributor

test this please

@msabramo This docs failure was due to a jenkins node problem - putting a comment "test this please" should re-trigger a test (and it worked for me) - this time successfully

@msabramo
Copy link
Author

@SvenDowideit Oh cool! Thanks! Good to know!

@dgageot
Copy link
Member

dgageot commented Feb 15, 2016

@msabramo Sorry, we are clearly not going to add another environment variable to the env command. cc @docker/machine-maintainers wdyt?

@jeanlaurent
Copy link
Member

@dgageot no, we are not. And in this case, this is clearly duplication for a very specific use case.

@msabramo
Copy link
Author

Sure, your call of course. What is the drawback of adding an environment variable?

Maybe someone can suggest an alternative way to get a usable IP address/port pair for accessing the container from a Mac? The best I can come up with is using a combo of docker-machine ip with docker-compose port or docker port and that's fine but it feels a bit weird to use 2 different tools.

@dgageot
Copy link
Member

dgageot commented Feb 15, 2016

@msabramo the role of the env command is to point docker CLI to the right machine. It sets the environment variables that are needed by the CLI.
Additional variables, that are not used by docker CLI, are out of scope for the env command.

Sorry @msabramo and thanks again for the PR. Next time, you have something you'd like to see implemented, please open a ticket, discuss about the feature and then, when everybody agree that it's a good idea to move forward, you or somebody else can open a PR.

@dgageot dgageot closed this Feb 15, 2016
@msabramo
Copy link
Author

Ah ok thanks for the explanation.

Technically I am using it in a docker-compose.yml file and so Docker Compose is passing the IP address to Docker which is using it. But it's not strictly necessary for connecting to Docker.

Unfortunately this was the best idea I had for making it really easy for folks to run containers from Macs and easily be able to access them.

I wonder if there is some other solution for this that I have overlooked?

@dnephin
Copy link

dnephin commented Feb 16, 2016

I've seen a few stackoverflow issues with this exact problem, and at least one Compose issue.

I think this would be useful for developers, is there a disadvantage to provide this information in a pre-parsed form (instead of requiring a developer to parse out the IP address from the DOCKER_HOST var) ?

@dgageot
Copy link
Member

dgageot commented Feb 16, 2016

@dnephin docker-machine ip $DOCKER_MACHINE_NAME should do the trick, right?

@dnephin
Copy link

dnephin commented Feb 16, 2016

I guess someone would need to do

export DOCKER_HOST_IP=$(docker-machine ip default)

in a script somewhere, which doesn't seem that unreasonable to me.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants