Docker Community Forums

Share and learn in the Docker community.

Can't resolve set hostname from another docker container in same network


(Eugene) #1

I’ve had db and server container, both running in the same network. Can ping db host by its container id with no problem.
When I set a hostname for db container manually (-h myname), it had an effect ($ hostname returns set host), but I can’t ping that hostname from another container in the same network. Container id still pingable.
Although it works with no problem in docker compose.
What am I missing?


(Yehudibrito) #2

You have to add the -l option by passing the container name. Because it will add to the host file of the container from which you are ping.


(Eugene) #3

Sorry, I didn’t get it. -l is a shortcut for --label, right? Where should I add -l passing container name?


(Yehudibrito) #4

Sorry. Correct is --link


(Yehudibrito) #5

I checked that the --link will be removed then use the --add-host

Add entries to container hosts file (–add-host)

You can add other hosts into a container’s /etc/hosts file by using one or more --add-host flags. This example adds a static address for a host named docker :

$ docker run --add-host=docker:10.180.0.1 --rm -it debian

root@f38c87f2a42d:/# ping docker
PING docker (10.180.0.1): 48 data bytes
56 bytes from 10.180.0.1: icmp_seq=0 ttl=254 time=7.600 ms
56 bytes from 10.180.0.1: icmp_seq=1 ttl=254 time=30.705 ms
^C--- docker ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max/stddev = 7.600/19.152/30.705/11.553 ms

Sometimes you need to connect to the Docker host from within your container. To enable this, pass the Docker host’s IP address to the container using the --add-host flag.


(Eugene) #6

That helps. Thank you very much :slight_smile: