By bitmap
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!
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!
Accepted Answer
Hello,
Yes this now comes more or less out of the box with Docker Networks, so what you need to do is:
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
docker network connect myNetwork web1
docker network connect myNetwork web2
docker network inspect myNetwork
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
create a bridge network and attach the containers to it , then you can ping them by name.
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.
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
container2
to the correct IP address.Here are the steps to set up this kind of communication:
docker network create my_network
Replace my_network
with the desired network name.
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
Here, container1
and container2
are given as examples of container names. Replace them with your actual container names and use your Docker images.
Test Connectivity: You should now be able to access container2
from container1
using the container name:
docker exec -it container1 ping container2
This should resolve the hostname and successfully ping the other container.
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.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
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
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.