brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: Docker host networking ignores your port mappings

Docker
~/tips/tip-docker-host-network-ignores-ports

docker run --rm --network host -p 8080:80 nginx:alpine

With host networking the app's own port is the port, and -p does nothing.

Switch a container to --network host and the -p 8080:80 you had stops meaning anything. The app now listens straight on the host, on whatever port it uses inside.

docker run --rm --network host -p 8080:80 nginx:alpine

Docker prints WARNING: Published ports are discarded when using host network mode and nginx listens on port 80 of the host, not 8080. Compose ignores ports: on such a service the same way. To change the port, you have to change the app’s own config.

Host networking is useful for apps that need to see the real network: discovery protocols such as DLNA, mDNS or the multicast that Home Assistant integrations use, or apps that need a lot of ports. It also skips Docker’s NAT, which helps a little with throughput.

I would keep it for those cases only. The container shares the host’s network stack completely, so a port clash with something on the host simply makes one of them fail to start, and every port the app opens is open on the host without a published ports list to remind you. Container name DNS on user defined networks does not apply either. On Docker Desktop, host networking is an opt-in setting and only from version 4.34.

Further reading: Host network driver

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link