How do docker containers resolve the hostname of other docker containers running on the same machine?

I started using docker and love it mainly because Docker containers are lightweight virtual machines. But I can't figure out how docker containers can solve each other. They can connect to each other using IP addresses, but without using their hostnames, I can't even edit / etc / hosts in containers to compensate for this. When I restart the containers they get different IP addresses and hence I want to use hostnames instead of IP addresses to communicate with each other. Let's say I want to run Zookeeper Cluster Zookeeper instances in containers and I want to put the hostnames of the Zookeeper servers in config files (zoo.cfg).


source to share

2 answers

It might be worth checking the Docker links ( ). When you link to a running container, a host entry is added for the container you want to connect to.

In their example, they show

$ sudo docker run -t -i --rm --link db:db training/webapp /bin/bash
root@aed84ee21bde:/opt/webapp# cat /etc/hosts  aed84ee21bde
. . .  db


As you can see here, they link the application they are in bash to a container named db

, and then add a host entry for db

that container's IP address.

So, in the case of starting zookeeper, you can just make the containers you start, just link to the zookeeper. Hope this helps!



As of Docker 1.10 , if you create a separate docker network, Docker will resolve hostnames inside the container using the internal DNS server [1] [2] [3]. You can change the name of the network host by specifying one of them from --name

to docker run

. Otherwise the hostname will refer to the container id (12 char long hash shown docker container ls


See also:


[1] = docker docs - built-in DNS server on custom networks

[2] = Docker Engine Release Notes - 1.10.0 (2016-02-04) - Network

[3] = Docker Download Requests - Vendoring libnetwork



All Articles