Docker Community Forums

Share and learn in the Docker community.

Autodiscovery for mongodb URL given a linked container?


(Pulkit Singhal) #1

Any ideas or suggestions to push the envelope of the outlined approaches would be most helpful.

  1. Approach A: Use hosts entry which is added automatically by Docker (when linking).
    I found that advice on the web but when I peeked into the hosts file for my WEB service’s container (which has a mongodb container linked into it) … I did not find any entry added automatically by Docker.

  2. Approach B: Docker Cloud inserts a linked service’s env variables which is quite convenient but if I try to reuse them to configure an env variable that my web services uses like so:
    DB_URL : mongodb://$MONGO_PORT_27017_TCP_ADDR:$MONGO_PORT_27017_TCP_PORT/sellerapp-dev-pulkit
    Then it does not get resolved because the linked variables’ values aren’t substituted. If I change my code itself to reuse MONGO_PORT_27017_TCP_* env variables then I risk tightly coupling my code to docker :frowning:


(Pulkit Singhal) #2

I heard back from some folks at docker cloud … wanted to share:

On Docker Cloud, we do not evaluate link environment variables as Docker engine itself does not evaluate link environment variables. It is up to the user to evaluate the link environment variable inside the container, as that is how Docker works.

The only environment variables that we do evaluate at deploy time are the ones that start with $DOCKERCLOUD_.

In other words, even though you have set the DB_URL to mongdb://$MONGO_PORT_27017_TCP_ADDR:$MONGO_PORT_27017_TCP_PORT, we do not evaluate the environment variables $MONGO_PORT_27017_TCP_ADDR and $MONGO_PORT_27017_TCP_PORT.

You can test this on your local Docker engine:

$ docker run -it --link mongo -e "TEST=\$MONGO_PORT_27017_TCP_ADDR" alpine printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=e8a6924aeb2e
TERM=xterm
MONGO_PORT=tcp://172.17.0.44:27017
MONGO_PORT_27017_TCP=tcp://172.17.0.44:27017
MONGO_PORT_27017_TCP_ADDR=172.17.0.44
MONGO_PORT_27017_TCP_PORT=27017
MONGO_PORT_27017_TCP_PROTO=tcp
MONGO_NAME=/adoring_varahamihira/mongo
MONGO_ENV_GOSU_VERSION=1.7
MONGO_ENV_GPG_KEYS=DFFA3DCF326E302C4787673A01C4E7FAAAB2461C42F3E95A2C4F08279C4960ADD68FA50FEA312927
MONGO_ENV_MONGO_MAJOR=3.2
MONGO_ENV_MONGO_VERSION=3.2.4
TEST=$MONGO_PORT_27017_TCP_ADDR
HOME=/root

(Pulkit Singhal) #3

Another clue I found is that maybe only a stack can auto-link the db’s url … I found a sample from the tutum branding days: https://botleg.com/stories/orchestrate-docker-containers-with-tutum/

We are linking the redis service to this service with the links parameter. This allows us to access the port 6379 of redis, which is the exposed port of redis service. We can also access the IP of redis service with redis keyword.

They were able to link the redis connection just fine! Why can’t I do this for mongo? Will just have to try it out I guess with a stack instead of plain old services.


(Arealmaas) #4

Did they say anything about Approach A ? Really strange that host isn’t added to etc/hosts.


(Pulkit Singhal) #5

No only what I posted earlier


(Arealmaas) #6

Just a note: When you use alpine images it might not work: https://github.com/gliderlabs/docker-alpine/issues/8

Using alpine:edge will work