Why UFW Doesn’t Block Docker’s Ports, and How to Fix It
Ports published with -p slip past UFW. Why it happens in iptables, a simple experiment that shows the problem, and three fixes from simple to complete.

Table of contents
A familiar scenario: you have run ufw default deny incoming on the server, only SSH and 443 are open, then you start a database with -p 5432:5432 and a few days later its logs show login attempts from unknown IPs. UFW isn’t broken; Docker simply made a decision about that packet before UFW ever saw it.
Why this happens
When you publish a port, Docker creates a DNAT rule in the nat table: a packet arriving at host port 5432 has its destination rewritten to the container’s internal IP right at the start of its journey (the PREROUTING chain). From that moment the packet is no longer “for the host”, so it goes through the FORWARD chain instead of INPUT.
Ordinary UFW rules such as ufw deny 5432 are evaluated on the INPUT path, so they never see this packet. On the FORWARD path, Docker has installed its own ACCEPT rules for containers. Docker’s official documentation says it plainly: Docker and UFW use firewall rules in ways that make them incompatible.
Experiment: see the problem for yourself
sudo ufw status # assume only 22 and 443 are open
docker run -d --name web-test -p 8080:80 nginx:alpine
sudo iptables -t nat -L DOCKER -n | grep 8080
Now send a request from another machine (not the server itself):
curl -I http://SERVER_IP:8080
Even though 8080 was never opened in UFW, nginx answers with 200 OK. Clean up afterwards:
docker rm -f web-test
Fix 1: don’t publish the port to the internet at all
Most services (databases, Redis, admin panels) don’t need to be reachable from outside. You have two cases:
- Only other containers need it: define no
portsat all. Containers on the same Compose network reach it by service name (details in Docker networking explained). - The host itself needs it, e.g. Nginx: publish the port on loopback only.
services:
db:
image: postgres:17
# no ports: reachable only inside the Compose network
app:
image: myapp:latest
ports:
- "127.0.0.1:8080:3000" # only Nginx on this server can reach it
This is the simplest and most reliable approach, and it is enough for most servers: the only public ports are 80 and 443, owned by Nginx on the host, and UFW means something again.
"8080:3000" means 0.0.0.0:8080: every interface, including the public IP. If the server has IPv6, the same port is also opened on [::].Fix 2: rules in the DOCKER-USER chain
Docker keeps a chain called DOCKER-USER empty for you and evaluates it before its own rules; it doesn’t flush it when it restarts either. Any rule you put there applies to container traffic.
Because packets have already been DNATed at this point, their destination port is no longer 8080. Use the conntrack match to compare against the original port and IP. Example: allow port 8080 for one specific IP only:
sudo iptables -I DOCKER-USER -p tcp -m conntrack --ctorigdstport 8080 --ctdir ORIGINAL -j DROP
sudo iptables -I DOCKER-USER -p tcp -m conntrack --ctorigsrc 203.0.113.10 --ctorigdstport 8080 --ctdir ORIGINAL -j ACCEPT
Order matters: -I inserts at the top of the chain, so the ACCEPT rule you ran second ends up above the DROP. Check the order:
sudo iptables -L DOCKER-USER -n -v --line-numbers
These rules disappear on reboot. To make them permanent, put them in /etc/ufw/after.rules (inside the *filter section) so UFW loads them when it starts. The open-source ufw-docker project packages exactly this pattern; once installed, you manage access to containers with commands like:
sudo ufw route allow proto tcp from any to any port 3000
Note that in ufw route rules you write the port inside the container, not the port published on the host, because of the same DNAT we saw above.
Fix 3 (not recommended): turn off iptables in Docker
Setting "iptables": false in /etc/docker/daemon.json stops Docker from creating rules, but it also breaks outbound NAT for containers and port publishing, so you have to build all of it by hand. Containers suddenly lose internet access and debugging it takes hours. For an ordinary server it isn’t worth the trouble.
Final checklist
# what is listening on all interfaces?
sudo ss -tlnp | grep -vE '127\.0\.0\.1|\[::1\]'
# which containers have public ports?
docker ps --format '{{.Names}}\t{{.Ports}}' | grep -E '0\.0\.0\.0|\[::\]'
Most importantly, always test from outside the server; a test from the server itself tells you nothing about the firewall:
nc -zv -w 5 SERVER_IP 8080
nmap -Pn -p 1-65535 SERVER_IP # only against your own server
| Approach | Complexity | When it fits |
|---|---|---|
| Don’t publish, or bind to 127.0.0.1 | Very simple | Almost always: services behind Nginx or internal only |
| DOCKER-USER / ufw-docker | Moderate | When a container port must be open to specific IPs only |
"iptables": false |
Complex and fragile | Practically never |
If you haven’t hardened SSH on the server yet, SSH hardening is the next step.



Comments
No comments yet. For technical questions, include the details (OS version, the exact error and the command you ran) to get a precise answer.