In my previous blog entry, I mentioned that Docker didn’t provide any way to add host-to-ip mappings to /etc/hosts. Well, the most recent version of Docker (1.3.1) now addresses that issue with a new add hosts option. Let’s take a quick look at what it does.

Given a standard default /etc/hosts.

% docker run -it ubuntu cat /etc/hosts
172.17.0.8	b14031841b2b
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters

We can add a mapping for server ‘foo’ at ‘10.0.0.3’.

% docker run -it --add-host foo:10.0.0.3 ubuntu cat /etc/hosts
172.17.0.9	189a650112db
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
10.0.0.3	foo

We can also add mappings for multiple servers, in this case ‘foo’ and ‘bar’.

% docker run -it --add-host foo:10.0.0.3 --add-host bar:10.7.3.21 ubuntu cat /etc/hosts
172.17.0.13	3f8543ecc372
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
10.7.3.21	bar
10.0.0.3	foo

So far so good. But there are a couple more scenarios to consider. The first is how to handle the case where a single server is hosting multiple services. These services could be in a single Docker container, separate containers or not in a container at all. What’s important is that they are both on some remote server.

So let’s consider the case where our services are named svca and svcb. We could try to add both of them as host mappings. Let’s see how that works out.

% docker run -it --add-host svca:10.0.0.9 --add-host svcb:10.0.0.9 ubuntu cat /etc/hosts
172.17.0.16	5d7eaa5dcd16
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
10.0.0.9	svcb
10.0.0.9	svca

Both services are pointing to the same IP address, but I’m not sure this is valid for /etc/hosts. The format is supposed to be IP hostname [hostalias]*.

Lucky for us, there is another option. When invoking add-host, we can include both services inside of double quotes. It’s not documented, but it does work as you can see below.

% docker run -it --add-host "svca svcb":10.0.0.9 ubuntu cat /etc/hosts
172.17.0.17	380df28b4612
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
10.0.0.9	svca svcb

There we go, both services pointing to the same IP address and /etc/hosts written in a valid format.

Another issue is how to provide hostaliases of localhost. As far as I can tell, Docker still doesn’t have anything to address this issue. For the time being this means that I’ll still be using Mungehosts to do that work.

In summary, add-host is a valuable new tool in the Docker toolbox for pointing to remote services. It’s worked well for all the cases I needed for it and it’s easy to use. It’s good to see Docker adding more config options to make our lives easier.