read

Docker is fanstastic for creating isolated development environments and I am using it more and more.

One of the problems I found when using docker as a development environment is that its not yet possible to assign a static ip address to a container. This makes harder to develop because you need to keep track of the ip address of your containers. I was looking for some time for a solution for this. I found some options, involving DNS and Service Discovery tools but most of then were too complex for a simple dev environment. I wanted something simple and straightforward to configure.

Recently I found out dnsdock which does everything I need.

dnsdock is a simple DNS server for Docker containers. It connects to Docker Remote API and keeps track of running containers and their respective ip addresses. You can then access to your containers by using something like

containername.docker
. dnsdock will automatically redirect to the appropriate ip address. You can use really complex hostnames if you need like
<anything>.<container-name>.<image-name>.<environment>.<domain>
. The domain is ".docker" by default but its configurable.

The setup is really simple and its very well explained in the project github page.

I only had some trouble, adding the docker nameserver to /etc/resolv.conf, mostly because this file is auto generated in Mint and I needed to find out where to include the nameserver line. In Ubuntu/Linux Mint you need to add to

/etc/resolvconf/resolv.conf.d/head
file otherwise it wont work.

dnsdock also provides a really nice REST api that lets you query the available services.

Its best to have dnsdock container to start automatically on your system startup. Since my OS is Linux Mint I used upstart for it.

Create a dnsdock.conf file in /etc/init with the following content:

description "Starts dnsdock container to allow DNS quries to docker"
author "Me"
start on filesystem and started docker
stop on runlevel [!2345]
respawn
script
docker start dnsdock
end script

There are other ways of doing this. Refer to docker documentation

And voilá. now you have your simple docker dns server starting at boot and ready to receive dns queries. When the ip addresses of your containers changes it will be updated automatically.

Blog Logo

Bruno Paz


Published

Image

Bruno Paz

Web Application Engineer. Expert in PHP and Symfony Framework. Enthusiast about new technologies. Sports and FCPorto fan!

Back to Home