Interview Prep/Docker/Networking and Compose

How do you expose a container only to the local host, not the internet?

BeginnerScenarioNetworking and Compose
Answer

Situation read → -p 5432:5432 binds to 0.0.0.0 and Docker inserts iptables rules that bypass ufw.

What I'd do → -p 127.0.0.1:5432:5432, or do not publish at all and let other containers connect over the Docker network. For Compose: ports: ["127.0.0.1:5432:5432"].

Why → Published ports are DNAT'd in the DOCKER chain before most host firewall rules.

Interview tip → Use the DOCKER-USER chain for custom filtering.

All Docker questions Roadmap: Docker & containers

More on Networking and Compose