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!


Submit an answer


This textbox defaults to using Markdown to format your answer.

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

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
August 27, 2019
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

  1. Use the Same Docker Network: Both containers should be on the same Docker network for this to work. Docker provides default DNS resolution within the same network.

  2. Container Names as Hostnames: By default, Docker sets up DNS entries using container names as hostnames within the same network. For example, if you have two containers named container1 and container2 on the same network, you can ping container2 from container1 using:

ping container2
  1. Docker will resolve container2 to the correct IP address.

Here are the steps to set up this kind of communication:

  1. Create a Docker Network (if not already done): If you haven’t already created a custom Docker network, you can do so with the following command:
docker network create my_network
  1. Replace my_network with the desired network name.

  2. Run Containers on the Same Network: When you run your containers, ensure that you attach them to the same Docker network using the --network flag:

docker run -d --name container1 --network my_network your_image1 
docker run -d --name container2 --network my_network your_image2
  1. Here, container1 and container2 are given as examples of container names. Replace them with your actual container names and use your Docker images.

  2. Test Connectivity: You should now be able to access container2 from container1 using the container name:

docker exec -it container1 ping container2
  1. This should resolve the hostname and successfully ping the other container.

  2. Hostnames in Application Configuration: If you’re using an application inside your containers and you need to specify hostnames in your configuration files, you can use the container names as the hostname.

Keep in mind that DNS resolution using container names is a Docker feature and may not work if you are trying to access containers outside of Docker, or if you have custom DNS configurations that interfere with Docker’s internal DNS resolution. In most standard Docker setups, this approach should work as described.

create a bridge network and attach the containers to it , then you can ping them by name.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.