Published · Updated
Docker published ports bypass UFW
During a DDoS incident I found that a container’s published port answered from the internet even though UFW on the host was enabled. Docker had written its own iptables rules for that port, and none of my UFW rules applied to the traffic.
- Docker
- UFW
- iptables
- DOCKER-USER
- firewall
- Linux
Why UFW doesn’t filter a published port
The rules you add with ufw allow and ufw deny apply to traffic addressed to the host itself. A published port works differently. With -p 8080:80, Docker adds a DNAT rule that rewrites the destination to the container’s address, so the packet is forwarded to the container instead of being delivered to the host.
Forwarded traffic goes through Docker’s own chains, which accept it before UFW’s rules are reached. ufw status still shows the default deny for incoming traffic, and the container answers anyway.
In the incident the application ran in a container with a published port and no reverse proxy in front of it. Anyone who knew the origin IP could reach the application directly, past Cloudflare and past the host firewall.
How to check a server from outside
A test run on the server itself proves nothing here, because local connections take a different path through the firewall. The check has to come from another network.
On the host, docker ps shows which ports are bound to 0.0.0.0 or [::], meaning every interface. Test each of those that UFW is supposed to block from outside.
- From another network: nmap -Pn -p 8080 <origin IP>, or curl -v http://<origin IP>:8080/.
- On the host: docker ps --format "{{.Names}} {{.Ports}}" to list the published ports.
- On the host: iptables -L DOCKER-USER -n -v to see whether anything filters them.
Three ways to close the port
Which one fits depends on whether anything outside the host needs that port at all.
Setting "iptables": false in /etc/docker/daemon.json stops Docker from writing rules, but then containers lose the NAT they need for outbound traffic unless those rules are written by hand.
Filtering in DOCKER-USER doesn’t have to be set up by hand. I used ufw-docker, an open-source script that adds the rules to /etc/ufw/after.rules and makes DOCKER-USER check the rules you add with ufw route. Docker keeps managing iptables itself, new connections from public addresses to containers are dropped, and you open a port with ufw route allow or ufw-docker allow <container> <port>. The rule matches the container port, not the host port, so with -p 8080:80 you allow 80. By default the script also lets through all traffic from the private ranges 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16, which matters when the server sits on a provider’s private network; the --docker-subnets option narrows that.
- Bind the port to localhost, for example "127.0.0.1:8080:80" in Docker Compose, and put a reverse proxy on the host in front of it. Only the proxy listens on a public interface. It is the simplest fix when the proxy is the only thing that should be public; the cost is one more service to configure.
- Filter in the DOCKER-USER chain. Docker sends forwarded traffic through it before its own rules, so a DROP there takes effect, with ACCEPT rules above it for trusted sources. By then the destination port has already been rewritten to the container port, so match that port or use -m conntrack --ctorigdstport for the published one, and persist the rules, for example in /etc/ufw/after.rules.
- Behind Cloudflare, accept traffic on ports 80 and 443 only from Cloudflare’s published IP ranges. With Docker, that allowlist also has to live in DOCKER-USER or on a proxy bound to the host, for the reason above, and the ranges need updating when Cloudflare changes them.
A complete DOCKER-USER rule set
If you would rather keep the rules in your own file than use ufw-docker, this block is a full version of the DOCKER-USER option. Ports 80 and 443 stay public, and every other published port is closed to traffic arriving on the public interface.
Add it at the end of /etc/ufw/after.rules, after the existing COMMIT, and replace eth0 with your public interface, which ip route show default prints.
# BEGIN DOCKER-USER
*filter
:DOCKER-USER - [0:0]
-A DOCKER-USER -m conntrack --ctstate ESTABLISHED,RELATED -j RETURN
-A DOCKER-USER -i eth0 -p tcp -m conntrack --ctorigdstport 80 -j RETURN
-A DOCKER-USER -i eth0 -p tcp -m conntrack --ctorigdstport 443 -j RETURN
-A DOCKER-USER -i eth0 -j DROP
COMMIT
# END DOCKER-USER- The first rule lets through replies to connections that are already open, including ones containers start themselves, so outbound traffic keeps working.
- --ctorigdstport matches the port the client connected to. With -p 443:80 you list 443, not 80, which is the opposite of the ufw route rules above.
- RETURN hands the packet back to Docker’s chains, which still check that the port is published. These rules open nothing on their own.
- The DROP covers only traffic arriving on eth0. SSH and other services on the host don’t pass through this chain, because that traffic isn’t forwarded. Traffic from a VPN or a second private interface isn’t filtered here either, so check whether that is what you want.
- To open a closed port for one address, add a rule above the DROP: -A DOCKER-USER -i eth0 -s 203.0.113.10 -p tcp -m conntrack --ctorigdstport 8080 -j RETURN. Behind Cloudflare, that means one rule per published range.
- UDP is closed too, so HTTP/3 on 443 needs its own rule with -p udp.
Applying, checking and undoing the rules
I tested this set on Ubuntu 24.04 with Docker 29.1.3 and UFW 0.36.2, in disposable containers with a second container acting as the outside client. Ports 80 and 443 answered, other published ports didn’t, and containers could still reach the internet and each other. The rules also survived ufw reload, a Docker restart and starting the firewall before Docker. I haven’t run this exact set on a production server, so try it somewhere you can get back into first.
It covers IPv4 only, and I didn’t test IPv6. It also relies on Docker’s default iptables backend: with the experimental nftables backend in Docker 29, there is no DOCKER-USER chain.
sudo ufw reload
sudo iptables -L DOCKER-USER -n -v --line-numbers- From another network: nmap -Pn -p 80,443,8080 <origin IP>, listing the ports you published. Only 80 and 443 should answer.
- From a container: an outbound request, for example docker exec <container> wget -qO- http://example.com/, confirms that replies still get back.
- To undo it, remove the block, run ufw reload and then iptables -F DOCKER-USER. The reload alone leaves the rules in place.
What I did in the incident
I changed and isolated the origin IP and limited the ways the server could be reached, and only then tuned the WAF and rate limiting in Cloudflare. The case study and the note on Cloudflare and exposed origins cover the rest of the incident.
See also: Cloudflare does not protect a directly reachable origin, CI/CD and Docker deployment.
To talk about a similar setup or problem, use the contact page.