Databases and servers
Docker published ports bypass ufw: why it happens and what to do
In short
Docker published ports bypass ufw because Docker routes container traffic in the nat table, so packets are diverted before they reach the INPUT and OUTPUT chains that ufw filters. The documented fixes are to publish on 127.0.0.1 when only the host needs the port, or to add filtering rules to the DOCKER-USER chain.
Why Docker published ports bypass ufw
Docker's page on packet filtering says it plainly: ufw ships with Ubuntu, and Docker and ufw use firewall rules in ways that make them incompatible. Docker creates its rules with iptables by default, with an nftables backend marked experimental and picked through the firewall-backend option. For container traffic, Docker works in the nat table, so a packet bound for a published port is redirected toward the container before the INPUT chain ever sees it. ufw's allow and deny rules live in INPUT and OUTPUT, which is why a ufw deny rule for a port can look correct and still leave a container on that port reachable.
The default makes this worse. The port publishing page calls publishing insecure by default: with no host address in -p 8080:80, the daemon publishes on every host address, 0.0.0.0 for IPv4 and [::] for IPv6, so the port is open to the outside world and not only to the host. The Compose reference says the same thing about a ports entry with no host IP, adding that Docker then binds to all interfaces, bypassing host firewall rules.
Unpublished ports are a different story. The same page says ports of containers on a bridge network are reachable from the Docker host and from containers on the same network, and the daemon blocks access from elsewhere to ports that were never published.
How to restrict a published Docker port: the documented options
Each row below comes from a Docker page. Pick by who needs to reach the service.
| Who needs the port | What to do | Where the docs say it |
|---|---|---|
| Only other containers on the same network | Do not publish it at all | Port publishing page |
| Only the host itself, for example a reverse proxy on the host | Publish on 127.0.0.1 (and [::1] for IPv6) | Port publishing page, Compose ports reference |
| A short list of outside addresses | Insert a DROP rule for other sources in DOCKER-USER | iptables page |
| Everything on this host should default to local only | Set the default binding address for the bridge network | Bridge driver page, dockerd reference |
| You want Docker to stop writing rules | Avoid it; the docs say it is likely to break container networking | Packet filtering page |
Checked against: source 1, source 2, source 3, source 4, source 5, source 6
Bind to 127.0.0.1 when only the host needs the port
This is the smallest change and the one I would reach for first for a database, a cache or an admin panel that sits behind a proxy on the same machine. The port publishing page gives this form, and with a localhost address only the Docker host can reach the container port. In Compose, the short syntax takes the host IP in front, as in "127.0.0.1:8001:8001", or the long syntax sets host_ip.
One version detail matters. The port publishing page warns that in Docker releases older than 28.0.0, hosts on the same layer 2 segment, such as machines on the same switch, could still reach ports published to localhost. On an older engine, binding to 127.0.0.1 is weaker than it looks, and upgrading the engine is part of the fix.
To make local-only the default instead of repeating the address everywhere, the bridge driver option com.docker.network.bridge.host_binding_ipv4 sets the default IP for published ports, and despite its name it accepts an IPv6 address. The dockerd flag --ip does the same for the default bridge network only. Treat both as a safety net; an explicit address in each ports entry is easier to review.
docker run -p 127.0.0.1:8080:80 -p '[::1]:8080:80' nginx
# compose.yaml
services:
db:
ports:
- "127.0.0.1:5432:5432"Use the DOCKER-USER chain to filter Docker traffic
When outside clients do need the port, filtering has to happen in a chain Docker reads first. The iptables page describes DOCKER-USER as a placeholder for your own rules, processed before the DOCKER-FORWARD and DOCKER chains. Rules appended to the FORWARD chain run after Docker's rules, so they arrive too late.
By default every external source address may connect to a published port. The page's example drops traffic arriving on the external interface from anything other than one allowed address; replace ext_if with the host's real interface name. It also shows subnet and address range variants of the same rule.
To allow or block one published service, the page matches on the original destination address and port through conntrack, and it warns that this kind of matching may degrade performance. As a general Linux point outside Docker's docs, rules added by hand with iptables need your distribution's own persistence method to come back after a reboot.
sudo iptables -I DOCKER-USER -i ext_if ! -s 192.0.2.2 -j DROP
sudo iptables -I DOCKER-USER -p tcp -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPTChecked against: source 4
Why disabling Docker iptables is the wrong fix for ufw
Setting "iptables": false in daemon.json can look like the clean way out. Docker's own page calls the option not appropriate for most users and likely to break container networking. Without replacement rules, containers on bridge networks lose outbound internet access through masquerading, and their ports become reachable from the local network, which is the reverse of what someone fighting ufw wants. The page also says it is not possible to stop Docker from creating firewall rules completely.
A firewall in front of the host, such as a cloud provider's network firewall, is a separate layer that the host's iptables rules cannot override. Docker's pages do not cover it, so treat it as an extra layer on top of the binding and DOCKER-USER fixes.
Checked against: source 1
How to check whether a Docker port is exposed
An ordered check I would run after any change, from the docs where possible:
- List each container's published ports with
docker container inspect <id> --format "{{json .NetworkSettings.Ports}}", the command the port publishing page uses. A HostIp of 0.0.0.0 or :: means every address. - On the host, the general Linux tool
ss -lntpshows listening sockets; it is a general tool, not one the Docker docs name. - From a second machine outside the host, try to connect to the port. Only that test shows what the outside world sees.
- Check the engine version and plan an upgrade if it is older than 28.0.0.
- Write down every rule you add to DOCKER-USER so the next person knows it exists.
- If a container will not start after you change a binding, the page on a published host port that is already taken covers that error. For the opposite symptom, a VM where nothing gets through, see the note on a web server timing out behind a cloud firewall.
Where this fits on a server running containers
These facts were read from docs.docker.com on 29 Nov 2026. Docker's networking pages change with engine releases, so re-check the packet filtering and port publishing pages after each engine upgrade, especially the nftables status.
A database in a container is the usual victim. Before exposing PostgreSQL on a new version, the PostgreSQL 18 pg_upgrade notes and the Redis maxmemory eviction policy guide cover the services that most often sit behind these ports, and the Docker Compose v1 to v2 migration notes cover the compose file side. More server notes are on the engineering hub, and the server care plan describes ongoing firewall and patch work.
What else do people ask?
Why does ufw not block Docker containers?
Docker sends container traffic through the nat table, so packets are redirected before they reach the INPUT and OUTPUT chains where ufw's rules sit. Docker's docs call the two incompatible.
How do I make a Docker port reachable only from localhost?
Publish it with a host address, as in -p 127.0.0.1:8080:80, or "127.0.0.1:8080:80" in Compose. On engines older than 28.0.0, hosts on the same layer 2 segment could still reach such ports.
Should I set iptables to false in daemon.json to fix ufw?
Docker's docs advise against it for most users: it is likely to break container networking and can expose ports to the local network.
Where do I put firewall rules for Docker containers?
In the DOCKER-USER chain, which Docker processes before its own chains. Rules appended to FORWARD run after Docker's rules.
Do unpublished container ports bypass ufw too?
The port publishing page says the daemon blocks outside access to ports that were not published; they stay reachable from the host and containers on the same network.
Does a Compose ports entry without an IP open the port to the internet?
It binds to all interfaces, 0.0.0.0, and the Compose reference notes that this bypasses host firewall rules.
Sources
- Docker docs, packet filtering and firewalls, fetched 2026-11-29
- Docker docs, port publishing and mapping, fetched 2026-11-29
- Docker docs, Compose workload specification (ports), fetched 2026-11-29
- Docker docs, Docker with iptables (DOCKER-USER chain), fetched 2026-11-29
- Docker docs, bridge network driver, fetched 2026-11-29
- Docker docs, dockerd reference, fetched 2026-11-29
Keep reading
Read next
- Docker port mapping not workingFix: The service answers inside the container but not from the host
- ASP.NET Core container does not answer on the published portFix: The container is running and logs no error, but the mapped host port never answers
- Docker ignores your -p mapping because the container uses host networkingFix: WARNING: Published ports are discarded when using host network mode