Docker Swarm – Unable to connect to other containers because IP lookup fails

Say we provision an overlay network using docker swarm and create various containers with following names:

  • Alice
  • Bob
  • Larry
  • John

now if we try to ping any container from another it fails because it does not know how to do the IP lookup i.e., alice does not know bob’s IP and so on. We have been taking care of this by manually editing the /etc/hosts on every container and entering the name/IP key value pair in that file but this is becoming very tedious with every restart of our network. There ought to be a better way of handling this.

E.g., services created using docker stack do not suffer from this problem. Due to various reasons we are stuck with creating containers using the vanilla docker create. How can we make containers discover each other on the overlay network without manual labor of editing /etc/hosts?

Below is detailed workflow we currently have to follow:

  • we first provision a docker swarm and overlay network
  • Then for each container, we create it using the docker create command and then start it using docker start command. we use the --network flag to attach the container to the overlay network at time of creation
  • We then use docker container inspect to get the IP address of each container. This involves running n commands and noting down IP address.
  • Then we log into each container and edit the /etc/hosts file by hand and enter the (name, IP) key-value pair of the other containers. So this means having to enter n*(n-1) records by hand when summed across containers.

Not sure why docker create does not do all this automatically – docker already knows (or can know) all the IP addresses. Containers provisioned using docker stack e.g., do not have to go through this manual process to “discover” each other. The reason why we cannot use docker stack is because:

  • it does not allow us to specify container name
  • we run various commands (mostly docker cp) before starting the container and not possible to do this using stack

Source: StackOverflow