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

(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:

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:

Using alpine:edge will work