Guides / Containers

Docker bypasses ufw: why published ports stay open and how to close them

intermediateUpdated 26 Nov 20261316 words

You enable ufw, allow SSH, HTTP and HTTPS, and deny everything else. Then you start a database with docker run -p 5432:5432 postgres:17 and it is reachable from the whole internet, although ufw status does not list port 5432. This is how Docker networking works on Linux, and the Docker documentation says so: "Docker and ufw use firewall rules in ways that make them incompatible with each other." The packets take a path that ufw does not filter, and four changes keep container ports private.

What happens to a packet for a published port

When you publish a port, Docker adds a DNAT rule to the nat table. A packet for port 5432 on the public address of the host is rewritten in PREROUTING to the address of the container (for example 172.17.0.2:5432) before any filter rule sees it. From then on the packet is no longer addressed to the host. It travels through the FORWARD chain, not the INPUT chain where ufw keeps the rules you add with ufw allow and ufw deny.

Docker also puts its own chains at the top of FORWARD. This is the start of the chain on a Debian host with Docker 29.9 and ufw:

sudo iptables -S FORWARD
-P FORWARD DROP
-A FORWARD -j DOCKER-USER
-A FORWARD -j DOCKER-FORWARD
-A FORWARD -j ufw-before-logging-forward
-A FORWARD -j ufw-before-forward
...

Rules reached from DOCKER-FORWARD accept the traffic to published ports, so the ufw chains after it never see those packets. Rules from ufw route end up in ufw-user-forward, also after the Docker chains. The only chain that comes first is DOCKER-USER, which Docker creates empty and leaves to you.

firewalld on RHEL, Rocky Linux and AlmaLinux behaves alike: Docker creates a firewalld zone named docker with the target ACCEPT and a policy that allows forwarding from any zone into it.

Find the ports that are open

List the published ports of the running containers:

docker ps --format 'table {{.Names}}\t{{.Ports}}'

0.0.0.0:5432->5432/tcp, [::]:5432->5432/tcp means all IPv4 and IPv6 addresses of the host. 127.0.0.1:5432->5432/tcp means loopback only. A port without an arrow (5432/tcp) is exposed to other containers but not published.

sudo ss -tlnp on the host shows the same ports with the process docker-proxy, which listens for each published port while the userland proxy is on (the default). See Audit listening ports for the full list of what listens on a server.

The reliable test is from outside. From another machine:

nc -zv -w 3 203.0.113.10 5432

succeeded or open means the port is reachable, whatever ufw status says.

Fix 1: publish on 127.0.0.1

Most published ports are only needed by something on the same host: a reverse proxy, a backup script, you over an SSH tunnel. Give the address in front of the port:

docker run -d -p 127.0.0.1:5432:5432 postgres:17

In a compose file, quote the mapping:

services:
  db:
    image: postgres:17
    ports:
      - "127.0.0.1:5432:5432"

Add "[::1]:5432:5432" if you also want the port on IPv6 loopback. Since Docker 28.0, other hosts on the same network segment can no longer reach a port published on 127.0.0.1; with older versions they could, so keep Docker up to date as well.

To reach such a port from your laptop, use an SSH tunnel instead of opening it:

ssh -L 5432:127.0.0.1:5432 user@203.0.113.10

Fix 2: do not publish at all

Containers on the same Docker network reach each other by service name. A database that only the app uses does not need a ports: entry. Only the reverse proxy publishes 80 and 443:

services:
  proxy:
    image: caddy:2.10
    ports:
      - "80:80"
      - "443:443"
  app:
    image: example/app:1.4.2
    # reached by the proxy as http://app:8080
  db:
    image: postgres:17
    # reached by the app as db:5432, never from outside

Fix 3: change the default address

You can make 127.0.0.1 the default for every -p 8080:80 without an address. For the default bridge network, set ip in /etc/docker/daemon.json. Compose and docker network create use user-defined networks, which read the driver option com.docker.network.bridge.host_binding_ipv4 instead. default-network-opts sets it for all new bridge networks:

{
  "ip": "127.0.0.1",
  "default-network-opts": {
    "bridge": {
      "com.docker.network.bridge.host_binding_ipv4": "127.0.0.1"
    }
  }
}

Check the file and restart Docker (this restarts containers unless live-restore is on):

sudo dockerd --validate --config-file=/etc/docker/daemon.json
sudo systemctl restart docker

Existing networks keep their options; recreate them (docker compose down && docker compose up -d) to get the new default. Ports that must be public then need an explicit address, such as "0.0.0.0:443:443". Treat this as a safety net and still write the address in each mapping, so the compose file means the same on every host.

Fix 4: filter in the DOCKER-USER chain

When a port must be reachable from some addresses only (a monitoring server, an office IP), add rules to DOCKER-USER, and keep two details in mind.

  • The rules see the packet after DNAT, so --dport matches the port inside the container, not the published one. Match the published port with the conntrack module and --ctorigdstport.
  • Replies to connections that containers open themselves also pass this chain. Let established traffic through first.

This script allows TCP port 5432 from 198.51.100.7 only and drops it from every other source on the external interface eth0 (find yours with ip route get 1.1.1.1):

#!/bin/sh
# /usr/local/sbin/docker-user-rules.sh
set -e
iptables -F DOCKER-USER
iptables -A DOCKER-USER -m conntrack --ctstate RELATED,ESTABLISHED -j RETURN
iptables -A DOCKER-USER -i eth0 -p tcp -m conntrack --ctorigdstport 5432 --ctdir ORIGINAL \
  -s 198.51.100.7 -j RETURN
iptables -A DOCKER-USER -i eth0 -p tcp -m conntrack --ctorigdstport 5432 --ctdir ORIGINAL -j DROP

The script flushes the chain first, so you can run it again after a change. Rules added by hand are gone after a reboot. A small systemd unit runs the script each time Docker starts or restarts:

# /etc/systemd/system/docker-user-rules.service
[Unit]
Description=Rules in the DOCKER-USER chain
After=docker.service
Requires=docker.service
PartOf=docker.service

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/sbin/docker-user-rules.sh

[Install]
WantedBy=multi-user.target
sudo chmod 755 /usr/local/sbin/docker-user-rules.sh
sudo systemctl daemon-reload
sudo systemctl enable --now docker-user-rules.service
sudo iptables -L DOCKER-USER -n -v

These rules cover IPv4. With IPv6 enabled for your Docker networks, repeat them with ip6tables. The DOCKER-USER chain belongs to the iptables firewall backend. Docker 29 can also run with the experimental nftables backend (firewall-backend in daemon.json), which does not create this chain.

Host network and other gaps

A container started with --network host does not use DNAT at all. Its ports are ordinary sockets of the host and go through INPUT, so ufw applies to them as to any other service. The trade-off is that the container sees all interfaces of the host and every service on 127.0.0.1; see the check for the host network.

A firewall at the hosting provider (Hetzner, AWS security groups, DigitalOcean cloud firewalls) filters before the packets reach the server, so Docker cannot bypass it. It is a good second layer for Fix 1 to 4, not a replacement, because a rule opened there for a test stays open until someone closes it. The host side of the firewall is in Firewall with nftables or ufw.

Check the result

  1. docker ps --format 'table {{.Names}}\t{{.Ports}}' shows 0.0.0.0 or [::] only for the ports you want public.
  2. nc -zv -w 3 <public address> <port> from another machine fails for every other port.
  3. After a reboot, sudo iptables -L DOCKER-USER -n still lists your rules, if you use Fix 4.

Patchbrief reports every container port published on all addresses in the daily check docker.public_ports.

Sources

Check your servers every day

The agent of Patchbrief reports the packages and settings of a server once a day; the digest names what to fix first.

Start free