Wilson Tayar //@wilsontayar

How to link containers in docker

This time we are going to see how to setup a mongodb image in the same docker environment as your app and how to link them together, so your app can connect to it!


For education purposes, I've created a very simple app in node to test this kind of stuff. You can find it in here.

This app does not use any external packages beyond the mongodb native driver from npm. Also, the Dockerfile contains everything to set up your image, including downloading references.

If you are new to docker and want to know a little bit more about Dockerfiles, this post can help you.


So, first of all, let's start a new mongo container and name it "mongo".

docker run -d --name mongo mongo

If it's your first time running a mongo container, docker will automatically pull the image from the official image registry. Don't worry.

The "-d" param means we want it to run in the background. If you don't explicit tell docker to do that it'll just start the container on your current terminal session until you exit.

After everything is set up you can check if your container is running by typing:

docker ps

If you see a mongodb container running like in the image below, great!

mongo container running mongo container running

 

Now let's clone the app repository, normal git stuff:

git clone https://github.com/wilsontayar/node-mongo-sample.git

And build the image from the dockerfile:

cd node-mongo-sample
docker build -t wilsontayar/node-mongo-sample .

This will probably take some time.

Docker is now downloading the base image, applying the dockerfile commands, downloading dependencies from npm and everything...

If you don't like the git clone part, just type in:

docker pull wilsontayar/node-mongo-sample

This way you'll pull the image directly from the official registry without the need to download sources and build the dockerfile.

After that, you can finally run the container:

docker run -d -p 3000:3000 --link mongo:mongo --name sample wilsontayar/node-mongo-sample

The important part is that "--link mongo:mongo" right there.

When you use this, docker will either use some environment variables inside your container OR update your container's /etc/hosts file telling that "mongo = x.x.x.x", where x.x.x.x is the internal ip to your mongodb's container.

And now you're done!

To test the app just navigate to localhost:3000 (if you're using boot2docker, remember to use "boot2docker ip", not localhost), type in a message and press enter to submit the form.


 

The correct "link" syntax is as follows:

--link <name>:alias

Where name is the name of the container you wish to link (remember the "--name mongo" we used earlier?) and alias is what you will tell your application, so the connection can be successful.

To know more about linking containers, take a look at the docker user guide here.

If you take a look at our app.js, specifically at the getConfiguration function, you can see that I did this:

process.env.NODE_ENV == "production" ? "mongodb://mongo/node-mongo-sample" : "mongodb://localhost:27017/node-mongo-sample"

So, if the NODE_ENV variable is set to production, our mongodb native driver will connect to the "mongo" server. That's our alias from the linking magic.

We are setting the NODE_ENV variable in our Dockerfile, this way:

ENV NODE_ENV production

 

Well, that's it for now!

Bye,

Wilson Tayar