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!
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.
Hello,
Yes this now comes more or less out of the box with Docker Networks, so what you need to do is:
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:
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
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
andcontainer2
on the same network, you can pingcontainer2
fromcontainer1
using:container2
to the correct IP address.Here are the steps to set up this kind of communication:
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:Here,
container1
andcontainer2
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
fromcontainer1
using the container name: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.
create a bridge network and attach the containers to it , then you can ping them by name.