How to use Docker links to *safely* connect from a Virgo application to a MongoDB

February 27, 2014 | 2 min Read

Some months ago, the Docker Team announced Docker 0.6.5[1]. Since that release it is possible to give your container names - this makes it much easier to find the correct container to interact with. It didn’t take long that I got used to this new feature.

Together with the freshly introduced explicit linking this is a huge security improvement.

Links allow containers to discover and securely communicate with each other by using the flag -link name:alias[2]. Setting the new Docker daemon flag -icc=false disallows inter-container communication, if needed.

Let’s give it a try and create an explicit link from an application running inside a Virgo container to a MongoDB running in another container.

$ docker run -name mongodb -t eclipsesource/mongodb
$ docker run -link mongodb:db -name virgo -t eclipsesource/virgo-spring-325

Docker will provide some environment variables within the linked container:

DB_PORT_27017_TCP_ADDR
DB_PORT_27017_TCP_PORT

Those variable names are built from the exported port of the container mongodb and the second part of the link option given when starting the Virgo container. For more information about linking please consult the Docker documentation section “Link Containers”[2].

Within the Virgo application you can access the environment variables when creating the MongoDB factory:

The snippet above uses the defaults declared in mongodb.properties when no environment variables are present.

Happy linking…

[1] https://blog.docker.io/2013/10/docker-0-6-5-links-container-naming-advanced-port-redirects-host-integration/ [2] https://docs.docker.io/en/latest/use/working\_with\_links\_names/