Question

How to ping Docker container from another container by name?

Hi all,

I want to be able to ping or basically access a running docker container from another container by simply using the docker name rather than an IP address. I’ve tried a few guides but I could not get it working. Has anyone been able to get this working?

Thanks a lot!

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

Hello,

Yes this now comes more or less out of the box with Docker Networks, so what you need to do is:

  • Create two containers:
docker run -d --name web1  -p 8001:80 eboraas/apache-php
docker run -d --name web2  -p 8002:80 eboraas/apache-php
  • Important note: it is very important to explicitly specify a name with --name for your containers otherwise I’ve noticed that it would not work with the random names that Docker assigns to your containers.

  • Then create a new network:

docker network create myNetwork
  • After that connect your containers to the network:
docker network connect myNetwork web1
docker network connect myNetwork web2 
  • Check if your containers are part of the new network:
docker network inspect myNetwork
  • Then test the connection:
docker exec -ti web1 ping web2

Again it is quite important to explicitly specify names for your containers otherwise this would not work. I figured this out after spending a few hours trying to figure it out.

Here’s a quick video demo on how to do the above:

Hope that this helps! Regards, Bobby