I am trying to get the following pretty basic (or so I thought) networking setup to work using Docker 1.9:
- I have multiple containers that run services, e.g. a
postgres
container and apython
container (there might be more than two). - Those containers are connected to each other through a
bridge
network. - I want them to be addressable using unique host names (i.e. the Python container should be able to do
ping postgres
to ping the Postgres container).
Following the tutorial (https://docs.docker.com/engine/userguide/networking/dockernetworks/), I can use the following sequence of commands to achieve this:
#create the containers
docker run -itd --name container1 busybox
docker run -itd --name container2 busybox
#create the network
docker network create test
docker network connect test container1
docker network connect test container2
This works quite well and Docker correctly sets the entries in etc/hosts
to point to the correct IP addresses. However, I also want to be able to run several instances of that setup (i.e. containers + network) simultaneously. This does not work because the entry for each container in the /etc/hosts
file is equal to its name, which needs to be unique. Specifying the hostname
parameter does not solve this problem since it only changes the local hostname of the container (i.e. the one it sees itself).
I would be very interested in a way to do this without resorting to having a DNS service running on a container. It seems to be a simple problem but unfortunately I was not able to find any configuration options to change the name of a container in the /etc/hosts
file.
BTW, I want the hostname to be the same in every instance of my network+container setup so that I do not need to dynamically pass the hostnames into the container (e.g. to tell the Python container the address of the Postgres container)
EDIT: I did some research on Docker's issue tracker and there seems to be a feature for this in the pipeline: https://github.com/docker/libnetwork/issues/737
docker 1.10, and PR 19242 can help:
docker 1.10 has a new section Network-scoped alias:
Now let us connect
container6
to thelocal_alias
network with a different network-scoped alias.