Publishing ports and the host firewall
What -p really does, why a published port ignores ufw, and how to keep databases private: bind to 127.0.0.1, filter in DOCKER-USER, publish one proxy.
Publishing ports and the host firewall
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- -p 8080:80 forwards port 8080 on every host address, IPv4 and IPv6, to port 80 in the container. EXPOSE alone publishes nothing.
- Docker implements publishing with NAT rules that act before the host firewall's INPUT chain, so ufw and similar front-ends do not see or block that traffic.
- Bind to loopback with -p 127.0.0.1:5432:5432 for anything only the host should reach. Before Engine 28, hosts on the same LAN segment could still reach such ports.
- Filter traffic to published ports in the DOCKER-USER chain, matching the original port with conntrack because packets there are already translated.
- Change the default bind address for a network with com.docker.network.bridge.host_binding_ipv4, or for the default bridge with the ip daemon option.
- The nftables backend, experimental since Engine 29, has no DOCKER-USER chain. Rules go in your own nftables table instead.
The port you thought was closed
You set up a VPS carefully. ufw denies everything except SSH and HTTPS. Then you start a database container with -p 5432:5432 so a GUI client can connect, and a week later a scanner on the internet finds it. ufw status still says 5432 is closed. Both things are true at once, and the reason is where Docker puts its rules.
What -p does
docker run -d -p 8080:80 nginx:1.29 # host 8080 -> container 80, TCP, every host address
docker run -d -p 127.0.0.1:8080:80 nginx:1.29 # loopback only
docker run -d -p 8080:80/udp some/dns # UDP
docker run -d -P nginx:1.29 # every EXPOSEd port to a random high port
docker port <container> # show the mappings
The port publishing page is explicit about the default: with no address, the port is published on all host addresses, IPv4 and IPv6. It also calls publishing “insecure by default” for that reason. EXPOSE in a Dockerfile publishes nothing on its own; it only feeds -P.
Docker implements the mapping with firewall rules: a destination NAT rule that rewrites packets for host port 8080 to the container’s address and port 80, and filter rules that accept them. A small helper, docker-proxy, also listens on the port for some cases such as traffic from the host’s own loopback, controlled by the daemon’s userland-proxy setting.
Why ufw does not see it
The packet filtering page explains it directly: “Docker routes container traffic in the nat table, which means that packets are diverted before it reaches the INPUT and OUTPUT chains that ufw uses.”
Follow one packet. It arrives for host port 5432. In the nat table’s PREROUTING chain, Docker’s DNAT rule changes its destination to 172.17.0.5:5432. Now it is not addressed to the host at all, so the kernel routes it through the FORWARD chain, not INPUT. ufw’s rules are in INPUT. Docker’s rules in FORWARD accept it. ufw is telling the truth about its own chain and is simply not in the path.
The Linux firewall front-ends lesson explains ufw’s chains, and the nftables lesson explains the hooks. firewalld is different: Docker creates a docker zone with target ACCEPT and puts its bridges in it, so firewalld at least knows about the traffic.
Keeping ports private
From cheapest to most involved.
Do not publish it. A database that only the app needs should be on the app’s user-defined network with no -p at all. This is the right answer more often than not.
Bind to loopback. -p 127.0.0.1:5432:5432 makes it reachable from the host only, so an SSH tunnel gets you in from your laptop. The docs carry a warning worth knowing: in releases before Docker Engine 28.0, hosts on the same layer 2 segment could still reach ports published to localhost (moby issue 45610). Upgrade if that matters.
Change the default. So that a forgotten address does not mean the whole internet:
docker network create -o com.docker.network.bridge.host_binding_ipv4=127.0.0.1 internal-net
or in /etc/docker/daemon.json, for every user-defined bridge, and the ip key for the default bridge:
{
"default-network-opts": { "bridge": { "com.docker.network.bridge.host_binding_ipv4": "127.0.0.1" } },
"ip": "127.0.0.1"
}
Filter in DOCKER-USER. With the default iptables backend, Docker creates a DOCKER-USER chain that runs before its own forwarding rules and leaves it for you. Packets there are already translated, so match the original destination with conntrack. From the iptables page, allowing only one network to reach published ports on the external interface:
iptables -I DOCKER-USER -i eth0 ! -s 192.0.2.0/24 -j DROP
iptables -I DOCKER-USER -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
Both use -I, so the second rule ends up on top, which lets replies to the containers’ own outbound connections back in before the drop. Rules added by hand are lost on reboot unless you persist them with your distribution’s iptables tooling.
One public door. Publish a single reverse proxy on 80 and 443 and keep every other service unpublished behind it, as the devops proxy lesson describes. Pair it with your cloud’s security group as an outer layer, since that sits in front of the host entirely.
The nftables backend
Docker Engine 29 added an nftables firewall backend, "firewall-backend": "nftables" in daemon.json. The docs mark it experimental and note it cannot be used in Swarm mode. Docker then owns tables named docker-bridges, and there is no DOCKER-USER chain. Instead you create your own table with base chains on the same hooks and use chain priorities to run before or after Docker’s. Existing DOCKER-USER rules have to be migrated by hand. The default is still iptables.
About the video
The embed is the first 12 minutes of Danie van der Merwe’s “Docker Punching Holes Through Your VPS Firewall? This Is How I Solved It”, March 2022. The clip covers the problem and why it happens, checking which ports are really open, the three options he found, and how to restrict ports. It stops before a section on remote access to restricted containers through a VPN. It predates the Engine 28 loopback fix and the nftables backend. YouTube reports an English original audio track. Awesome Open Source’s video in deeper does the same with firewalld and the iptables mitigations from Docker’s docs.
What was run: nothing, since there is no Linux host or Docker here, so no packet was traced and no rule was loaded. The NAT path, the ufw explanation, the DOCKER-USER rules and the bind address options are from the port publishing, packet filtering, iptables and nftables pages in the docker/docs repository, checked 2026-11-28.
Where this fits
Bridge networks with published ports cover most cases. Next, the drivers that skip the bridge entirely: host, none, macvlan and ipvlan networks.
Check yourself
A server runs ufw with only 22 and 443 allowed. A Postgres container started with -p 5432:5432 is reachable from the internet. How?
Docker's DNAT rule in the nat table redirects the packet to the container before it reaches the INPUT chain ufw manages, and the forwarded packet is accepted by Docker's own rules. ufw never sees it.
What is the simplest fix if only an app on the same host needs that database?
Publish it on loopback, -p 127.0.0.1:5432:5432, or better, do not publish it at all and put the app on the same user-defined network so it connects by name.
You add iptables -I DOCKER-USER -p tcp --dport 8080 -j DROP to block a port published as -p 8080:80. It has no effect. Why?
Packets in DOCKER-USER have already been translated to the container's address and port 80. Match the original port with -m conntrack --ctorigdstport 8080, or match the container port.
Your team runs many containers that each publish ports by accident. How can you make loopback the default for a network?
Create the network with -o com.docker.network.bridge.host_binding_ipv4=127.0.0.1, or set it for all user-defined bridges with default-network-opts in daemon.json. Then -p 8080:80 binds to 127.0.0.1 unless an address is given.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.