DEV Community

loading...

The behavior of docker run --network=host

Meir Gabay
I'm passionate about studying and teaching. DevOps Engineer @ DoIT International
・1 min read

I'm on WSL2 Ubuntu 20.04, Docker v20.10.2, and according to the docs

docker run --network=host ... is supported only on Linux

So now I'm trying to understand its behavior on WSL2.

It's possible to make requests with --network=host, but not possible to listen to requests (what?)

# Listening doesn't work (as expected)
docker run --network host --rm --name webserver -d nginx
curl localhost:80
# connection refused

# Making requests works, but why?
docker run --rm  --name webserver -d -p 80:80 nginx
docker run --rm --network host httpd:2.4.46 ab -n 100000 -c 10 http://localhost:80/
# works

# Another way to make requests which work, but slower
docker run --rm --network host httpd:2.4.46 ab -n 100000 -c 10 http://host.docker.internal:80/
# also works, though super slow, see - https://docs.docker.com/docker-for-windows/networking/#use-cases-and-workarounds
Enter fullscreen mode Exit fullscreen mode

Conclusion (?)

The --network=host behavior on Windows WSL2 is as follows

  • ❌ Doesn't work for listening
  • ✔️ Works for making requests from a container to the host's network. This means another container has already exposed one of its ports with -p or -P

Can anyone confirm the above conclusion? Does it make sense?

Discussion (0)

Forem Open with the Forem app