PortGate

Guides · Docker · 2 min

How to Stop Docker from Bypassing UFW Firewall Rules on Ubuntu

Published container ports skip UFW. Bind them to localhost and drop unexpected traffic in the DOCKER-USER chain.

Docker inserts its own iptables rules. A container published as 0.0.0.0:6379 is reachable from the internet even when UFW says DENY 6379. UFW manages the INPUT chain. Docker DNATs published ports in the nat table and then accepts them in FORWARD, before your UFW policy runs.

Confirm the bypass

On the host, list listeners and published ports:

ss -tulpn
docker ps --format '{{.Names}} {{.Ports}}'
sudo ufw status verbose

If Redis shows 0.0.0.0:6379 while UFW has no ALLOW for 6379, the firewall is not the control you think it is. From a second machine, nc -vz your.server 6379 succeeding is the proof.

Fix 1: stop publishing to every interface

Databases, queues, and admin panels should listen on the host loopback only.

services:
  redis:
    image: redis:7
    ports:
      - "127.0.0.1:6379:6379"

The same form works on the command line: docker run -p 127.0.0.1:6379:6379 redis:7. Other containers on a user-defined bridge reach Redis by its service name and do not need a host port at all. Remove the ports key when the service is only used inside Compose.

Do not attach a database to --network host. That places it on the host network namespace and ignores the publish address entirely.

Fix 2: filter the public interface in DOCKER-USER

DOCKER-USER is the chain Docker reserves for your rules. Every forwarded packet passes through it before Docker's own accept rules. Two details decide whether a rule there works.

  • Order. On Docker 27 and earlier, Docker creates the chain with a final RETURN rule. A DROP appended with -A lands after that RETURN and never matches. The script below flushes the chain and writes it in order, ending with its own RETURN.
  • Ports. By the time a packet reaches DOCKER-USER, Docker has already rewritten its destination to the container's address and port. --dport 443 matches the container port, not the port the visitor used. Match the published host port with -m conntrack --ctorigdstport.

Find the public interface first:

ip route get 1.1.1.1 | awk '{for (i=1;i<NF;i++) if ($i=="dev") print $(i+1)}'

Save this as /usr/local/sbin/docker-user-firewall.sh, set IF to that interface, and list the host ports you publish on purpose in PUBLIC_PORTS:

#!/bin/sh
set -eu
IF=eth0
PUBLIC_PORTS="80 443"

iptables -N DOCKER-USER 2>/dev/null || true
iptables -F DOCKER-USER
# Replies to connections containers opened, and traffic that did not arrive on the public interface
iptables -A DOCKER-USER -m conntrack --ctstate ESTABLISHED,RELATED -j RETURN
iptables -A DOCKER-USER ! -i "$IF" -j RETURN
# Host ports you publish on purpose, matched before Docker's DNAT
for port in $PUBLIC_PORTS; do
  iptables -A DOCKER-USER -i "$IF" -p tcp -m conntrack --ctorigdstport "$port" -j RETURN
done
# Everything else that reached a container from the internet
iptables -A DOCKER-USER -i "$IF" -j DROP
iptables -A DOCKER-USER -j RETURN
sudo chmod 755 /usr/local/sbin/docker-user-firewall.sh
sudo /usr/local/sbin/docker-user-firewall.sh
sudo iptables -L DOCKER-USER -n -v --line-numbers

The listing should show the DROP as the second-to-last rule, above the final RETURN. If your provider's private network arrives on the same interface, add iptables -A DOCKER-USER -i "$IF" -s 10.0.0.0/8 -j RETURN before the drop, with your private range.

Keep it after a reboot

Docker does not flush DOCKER-USER when it restarts, but a reboot clears every iptables rule, and ufw reload does not rebuild this chain. Run the script after Docker starts:

# /etc/systemd/system/docker-user-firewall.service
[Unit]
Description=Filter published Docker ports on the public interface
After=docker.service
Wants=docker.service

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

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now docker-user-firewall.service

IPv6 and the nftables backend

If the Docker daemon has IPv6 enabled ("ipv6": true in /etc/docker/daemon.json, or a network created with --ipv6), published ports are also reachable over IPv6. Repeat the script with ip6tables in place of iptables. If dockerd runs with --firewall-backend=nftables, there is no DOCKER-USER chain. Bind ports to 127.0.0.1 (Fix 1) and filter in your own nftables table, as Docker's nftables documentation describes.

Check the fix

From a second machine, every port you did not list should time out:

nc -vz -w 3 your.server 6379

On the host, redis-cli -h 127.0.0.1 ping should still answer. Run the outside check again after sudo systemctl restart docker and after a reboot.

PortGate's server agent reports a public Docker bind as drift against the approved baseline. Approving a baseline that still publishes Redis to 0.0.0.0 records the hole as normal. Close the port first, then approve the new state.