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/.
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.)
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.
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.
extra_hosts:
“somehost:162.242.195.82”
“otherhost:50.31.209.229”
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?
ping nginx.dev.intranet
PING nginx.dev.intranet (172.17.0.5) 56(84) bytes of data.
64 bytes from 172.17.0.5: icmp_seq=1 ttl=64 time=0.099 ms
64 bytes from 172.17.0.5: icmp_seq=2 ttl=64 time=0.069 ms