
A firewall that really filters with Docker: ufw and DOCKER-USER
DevOpsSecurityHomeLabDockeriptablesufwFirewallLinux
Why ufw does not filter the ports Docker publishes, how traffic travels through iptables, and how to restrict a port persistently with the DOCKER-USER chain.
Contents
A server with ufw enabled and the incoming policy set to deny looks closed. With Docker it is not: a container with ports: "3306:3306" is reachable from any machine on the network even though ufw status does not allow it. This guide explains why that happens and how to set up a filter that works, with a rule that survives reboots.
Diagram generated with Claude.
Why ufw cannot see Docker’s ports
ufw manages rules in the INPUT chain, which handles traffic addressed to the machine itself. But when you publish a container port on a bridge network, Docker does two things:
- It adds a
DNATrule in thenattable (PREROUTING) that changes the packet’s destination from “the server’s IP, port 3306” to “the container’s internal IP, port 3306”. - Since the packet is no longer addressed to the machine but to another IP, the kernel forwards it, and it goes through the
FORWARDchain, notINPUT.
As a result, traffic to the container never touches ufw’s rules. It makes sense once you see it that way, but it is surprising the first time.
You can check it on your own server:
sudo ufw status
sudo iptables -t nat -L DOCKER -n
sudo ss -tlnp | grep docker-proxy
If the last output shows a port listening on 0.0.0.0 that ufw does not allow, that port is open to the whole network.
The first defence: do not publish more than needed
Before writing rules, the cheapest option is not to publish the port on every interface. If the service is only used by a proxy on the same machine, or reached through a tunnel, bind it to the loopback address:
ports:
- "127.0.0.1:8080:80"
That way the port cannot be reached from outside the machine and no rule is needed. It is what I do with everything that reaches the internet through a tunnel.
The case this does not solve is a service that must be reachable from the local network, but only from some machines, such as a database queried by two specific hosts. That is where DOCKER-USER comes in.
The DOCKER-USER chain
Docker creates the DOCKER-USER chain and puts it at the start of FORWARD, precisely so the administrator can place rules before Docker’s own. Docker does not modify or delete it on restart.
One key detail: when this chain evaluates a packet, the DNAT has already been applied. So the rules must use --ctorigdstport, the original port the packet was addressed to, and not --dport, which would already hold the container’s port.
There is a second, equally important detail: --ctorigdstport matches in both directions of the connection, so a DROP rule using it also drops the container’s replies, and even the allowed machine would never manage to connect. The fix is to add --ctdir ORIGINAL, which limits the rule to packets travelling from client to container. Replies keep flowing through Docker’s own rules.
With that in mind, the idea is simple: accept the port from the allowed IPs and drop the rest.
The insertion order is reversed
iptables -I inserts the rule at the top. If you insert an ACCEPT first and a DROP afterwards, the DROP ends up in front and blocks everything. Rules must be inserted in the opposite order to how you want them evaluated: the DROP first and the ACCEPT rules afterwards.
PORT=3306
ALLOWED=192.168.1.20
# 1) The DROP first (it will end up last)
sudo iptables -I DOCKER-USER -p tcp -m conntrack --ctorigdstport "$PORT" --ctdir ORIGINAL -j DROP
# 2) Then the ACCEPT rules (they end up in front)
sudo iptables -I DOCKER-USER -p tcp -m conntrack --ctorigdstport "$PORT" --ctdir ORIGINAL \
-s "$ALLOWED" -j ACCEPT
Each additional ACCEPT is inserted the same way, after the DROP. The important part is checking the result:
sudo iptables -L DOCKER-USER -n --line-numbers
The ACCEPT rules must have a lower line number than the DROP.
Making it persistent with systemd
iptables rules are lost on reboot. Since the DOCKER-USER chain is created by Docker, the script must run after Docker starts. A oneshot unit solves it.
The script, at /usr/local/sbin/docker-user-firewall.sh:
#!/bin/bash
set -eu
PORTS="3306 3307"
ALLOWED="192.168.1.20 192.168.1.21"
for p in $PORTS; do
# Delete previous rules so the script is idempotent
while iptables -D DOCKER-USER -p tcp -m conntrack --ctorigdstport "$p" --ctdir ORIGINAL -j DROP 2>/dev/null; do :; done
for ip in $ALLOWED; do
while iptables -D DOCKER-USER -p tcp -m conntrack --ctorigdstport "$p" --ctdir ORIGINAL -s "$ip" -j ACCEPT 2>/dev/null; do :; done
done
# DROP first; the ACCEPT rules end up in front
iptables -I DOCKER-USER -p tcp -m conntrack --ctorigdstport "$p" --ctdir ORIGINAL -j DROP
for ip in $ALLOWED; do
iptables -I DOCKER-USER -p tcp -m conntrack --ctorigdstport "$p" --ctdir ORIGINAL -s "$ip" -j ACCEPT
done
done
The unit, at /etc/systemd/system/docker-user-firewall.service:
[Unit]
Description=DOCKER-USER rules to restrict database ports
After=docker.service
Requires=docker.service
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/docker-user-firewall.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
And you enable it:
sudo chmod +x /usr/local/sbin/docker-user-firewall.sh
sudo systemctl daemon-reload
sudo systemctl enable --now docker-user-firewall.service
Two notes about this unit:
After=andRequires=docker.serviceare essential. If the script runs before the chain exists, it fails and the port stays unfiltered.- It is
oneshotwithRemainAfterExit=yes, sosystemctl statuswill showactive (exited). That is normal and not a failure.
Test from another machine
Testing from the server itself is useless: local traffic does not take the same path and will always work. Test from another host, one allowed and one not:
# From a host that is NOT allowed: it must time out
nc -zv -w 3 192.168.1.10 3306
# From an allowed host: it must connect
nc -zv -w 3 192.168.1.10 3306
Summary
ufwonly filtersINPUT; the ports Docker publishes go throughFORWARD.- First, publish on
127.0.0.1everything that does not need to be seen from the network. - For the rest,
DOCKER-USERwith--ctorigdstportand--ctdir ORIGINAL, remembering that-Ireverses the order. - Persistence with a
oneshotunit that starts after Docker. - Always verify from another machine and with
--line-numbers.
Next steps
If what you want is to publish a service on the internet without opening any router port, the next guide will explain how to do it with Cloudflare Tunnel and dynamic DNS.
Copyright: CC BY-NC-ND 4.0 · José Ángel Alejo Sillero