How to ping Docker-Container inside host network?
Asked Answered
V

1

6

I'm working with Docker containers for a while now but can't figure out how to ping docker containers which are part of my host network.

So until now I created my containers specifing the name and networks flags like described in many tutorials like: https://www.digitalocean.com/community/questions/how-to-ping-docker-container-from-another-container-by-name

Where I am able to create a network and afterwards run my containers in these networks for example like:

docker run -d --name web1  -n testnetwork
docker run -d --name web2  -n testnetwork

That would enable me to ping my containers from each other with:

docker exec -it web1 bash # enter container
ping web2 #ping second container

Now I have to use a given application which only runs in the "host" network for now. To access this container from my other containers they have to be in the same network (== "host").

But It seems like I cant ping my containers from each other anymore. I'm also unable to ping my containers from my host machine using their name.

Did I overlooked something?

Any help would be appreciated! Best regards

Vomer answered 8/10, 2020 at 9:45 Comment(0)
S
8

If you set --network host, you basically disable Docker's entire networking stack. Among other things, that disables normal inter-container communications: if you're using host networking you can't call another container by its name. Host networking is very rarely necessary (and doesn't work well on some host platforms); the first thing I'd look at is whether you can switch back to standard (bridged) networking.

If you do run a container with --network host, it's indistinguishable from other processes running on that host. That means you can't directly send ICMP packets to it, any more than you can ping(1) your ssh daemon or Web browser. You need to connect to the container using the host's IP address or DNS name, even from other containers on the same host. From inside of a Docker container, how do I connect to the localhost of the machine? discusses several ways to do this.

(I don't think you can customize the behavior of Docker or Linux when a container receives an ICMP ECHO packet; ping(1) a container doesn't seem that useful.)

Sall answered 8/10, 2020 at 11:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.