Docker Community Forums

Share and learn in the Docker community.

Question docker host know container ip and hostname

(Cesarjorgemartinez) #1


When I pull an image and start a container, ex:
docker run --name test --hostname test -ti centos

Then, from the docker host machine, I can ping to docker container IP:

But not by container hostname
ping test
ping: unknown host test

How can you do so that the host knows the name of a container?

Without add manually this entry inside /etc/hosts of the docker host machine.


(David Maze) #2

This doesn’t work reliably across platforms (I’m pretty sure Docker for Mac can’t do it, for instance) and isn’t the right way to access services running in a container.

Instead, use the standard docker run -p option

docker run -d -p 8080:8080 me/myapp

and then you can access the exposed port 8080 on your host system, e.g. point your browser at http://localhost:8080/.

(Cesarjorgemartinez) #3

Only is an example, but I don’t need to access an exposed port, I need ping the hostname of a container, from the docker host

(David Maze) #4

Why? What does “ping a hostname” mean? What’s your specific goal with this task?

(The ping(1) command does two things: it does a DNS resolution of a host name to an IP address, and it sends that IP address ICMP ECHO packets and looks for corresponding ICMP REPL packets. Neither of these things make sense in Docker space.)

(Macdjord) #5

I don’t know about @cesarjorgemartinez, but I had a situation recently where I needed a container to know its host’s IP address: I was setting up containerized elasticsearch nodes, and each node had to know what IP address to advertise so that other nodes could open connections to it. Since these containers were not on the same Docker network, due to being on different physical hosts, that had to be the host IP.

I never found any automated way of doing it; instead I ended up just creating an environment variable on each host machine called ${HOST_IP} and importing that as part of the compose file.

(Cesarjorgemartinez) #6

If we need that a container known the host IP and name, we can to use extra host of docker compose:
Add hostname mappings. Use the same values as the docker client --add-host parameter.


  • “somehost:”
  • “otherhost:”

The difficulty I have is that the host knows the names of the containers, not only IPs (for ips, use ifconfig inside host)
Docker can somehow expose these names to the host?

(Defreitas) #7

See this answer, this make possible to run a container and solve it by hostname at host machine, it would be helpful for development

docker run --rm --hostname dns.mageddo --name dns-proxy-server -p 5380:5380 \
  -v /opt/dns-proxy-server/conf:/app/conf \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v /etc/resolv.conf:/etc/resolv.conf \

Running a test container

docker run --hostname nginx

Testing it

PING ( 56(84) bytes of data.
64 bytes from icmp_seq=1 ttl=64 time=0.099 ms
64 bytes from icmp_seq=2 ttl=64 time=0.069 ms

Resolve containers from host by network-alias or container name or host