Easy Networking Without Links in Docker
Docker 1.8 quietly introduced a new feature for simple container networking.
A week after Docker 1.7.1 was released, a pull request was merged to update libnetwork, mainly to bring some multi-host networking abilities to the Docker experimental channel.
The update brought with it a new libnetwork feature,
the population of container /etc/hosts
files as a primitive form of service
discovery. In short, as of Docker 1.8.0 you can do intuitive things like this
with two terminals side-by-side:
$ docker run --name testa -it ubuntu:14.04.3 bash → root@020974ec7883:/# cat /etc/hosts | grep testb 172.17.0.4 testb 172.17.0.4 testb.bridge root@020974ec7883:/# ping -qc1 testb PING testb (172.17.0.4) 56(84) bytes of data. --- testb ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.152/0.152/0.152/0.000 ms |
← $ docker run --name testb -it ubuntu:14.04.3 bash ← ← ← root@2907a8b2766b:/# ping -qc1 testa PING testa (172.17.0.3) 56(84) bytes of data. --- testa ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.135/0.135/0.135/0.000 ms |
This all happened with little fanfare - the
changelog
is silent on the matter
and people have only recently been raising issues about the problems it can cause
(#1, #2).
For example, if you have a DNS server being updated with container names and
addresses, the entries in /etc/hosts
could conflict.
However, for an average developer on a single
machine living under the tyranny of links or digging up IP addresses with
docker inspect
, this feature is fantastic! For example, you can recreate the
db container of an application without needing to either a) update
IP addresses in the app container or b) recreate the app container entirely (to
make links work again).
Next time you’re thinking about manually creating links or writing a Docker
Compose file, this feature might come in useful as a lightweight alternative.
Of course, it won’t work if you have --icc=false
set on your Docker daemon.