Allowing two sibling docker containers to see each other over the network

Default featured post

Allowing two sibling docker containers to see each other over the network. Docker world is simply fascinating, yet confusing at many times. Especially, when running multiple sibling containers at the same time. One of the first difficulty is that the sibling’s containers cannot see each other over the network. This means that, if we have two services they are not even aware of each other existence. Hence, pinging a sibling container from the other container will fail.

To tackle the issue, one can suggest adding both containers in a docker-compose.yml. However, this makes sense when there is a dependency or a meaningful relation between the containers. Otherwise, doing so is absolutely wrong.

If two sibling’s containers need to access to each other over the network, yet be completely independent, then the best option is to use docker network.

Establishing a docker network is fairly straightforward. Let’s go through it with an example.

Assume, we have two containers, Apache and MySQL. And these containers need to be able to ping each other over the network if needed.

The first step is to spin up our containers. apache.service and mysql.service respectively which the former listens on the port 80 and the latter listens to 3306

$ sudo docker run --name mysql.service -e MYSQL_ROOT_PASSWORD=test -d -p 3306:3306 mysql
$ sudo docker run --name apache.service -d -p 80:80 httpd

Now, we need to create a docker network, like this:

$ sudo docker network create test-network

After that, we should be able to find our network.

$ sudo docker network list

We should see the test-network with bridge driver.
Then need to add the running containers to the network,

$ sudo docker network connect test-network apache.service
$ sudo docker network connect test-network mysql.service

If the containers are not running, we can always attach them to the network when using docker create.

$ sudo docker create --name mysql.service --network test-network -p 3306:3306 mysql
$ sudo docker create --name apache.service --network test-network -p 80:80 httpd

And that’s all. Now, we can ssh to any of the containers and ping the sibling container by its name. Here, we use nmap command like below:

$ nmap -p 3306 mysql.service
$ nmap -p 80 apache.service

To disconnect our running containers from the network:

$ sudo docker network disconnect test-network apache.service
$ sudo docker network disconnect test-network mysql.service

Finally, to remove a docker network just need to run:

$ sudo docker network rm test-network

Docker network is a very powerful tool and its usage does not limit to what is discussed here. For instance, the docker network can be used when two, even related, containers cannot be started at the same time but need to access each other over the network.

For more information, we highly recommend reading docker network documentation at below link:
https://docs.docker.com/network/