Blog

UFW and Docker: Why Docker Bypasses Your Firewall and How to Fix It

Docker publishes container ports past UFW, so "ufw deny" does nothing. See why it happens, how to find exposed ports, and four ways to close them.

Max Diamond 9 min read

You turn on UFW, allow SSH, HTTP and HTTPS, and deny everything else. Then you run docker run -p 6379:6379 redis and Redis is open to the whole internet. ufw status doesn’t list port 6379, and ufw deny 6379 changes nothing.

Your UFW rules are correct. Docker writes its own iptables rules, and traffic to a published port never reaches the rules UFW manages. This guide shows why, how to find every port Docker has opened, and four ways to close them. We tested every command on Ubuntu 24.04 with Docker Engine 29.

Understanding why Docker bypasses UFW

UFW filters traffic addressed to the server itself. That traffic goes through the INPUT chain, and UFW’s rules live there.

A published port works differently. When you run -p 6379:6379, Docker adds a NAT rule that rewrites the destination of incoming packets from the server’s IP to the container’s IP, such as 172.17.0.2. After the rewrite, the packet isn’t for the server any more. The kernel routes it through the FORWARD chain to the container, and Docker’s own rules near the top of FORWARD accept it.

Packet to your-server:6379
└─ nat PREROUTING: Docker rewrites the destination to 172.17.0.2:6379
└─ filter FORWARD: DOCKER-USER, then Docker's ACCEPT rules
└─ container
UFW's INPUT rules: never consulted

You can see the rewrite on your own server:

Terminal window
sudo iptables -t nat -L DOCKER -n
Chain DOCKER (2 references)
target prot opt source destination
DNAT 6 -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:6379 to:172.17.0.2:6379

The 6 in the prot column is TCP’s protocol number; older versions of iptables print tcp.

Docker gives you one place to filter this traffic: the DOCKER-USER chain. FORWARD jumps to it before Docker’s own rules, and Docker never changes the rules you put in it. Fix 4 uses it.

Finding every port Docker has opened

Start with the containers. Any port bound to 0.0.0.0 or [::] is open on every network interface, including the public one.

Terminal window
docker ps --format 'table {{.Names}}\t{{.Ports}}'
NAMES PORTS
redis 0.0.0.0:6379->6379/tcp, [::]:6379->6379/tcp
app 127.0.0.1:3000->3000/tcp
traefik 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp

Here redis is public, app only listens on localhost, and traefik is public on purpose.

Then test from outside. A check on the server itself can’t tell you what the internet sees. From your laptop or another server, run:

Terminal window
nmap -Pn -p 1-65535 your-server-ip

Every open port in the result is reachable by anyone. If one isn’t on your list of ports you meant to open, close it with one of the fixes below. Run the same scan again after each change.

Fix 1: Keeping internal ports unpublished

The best fix is a port that doesn’t exist. If your app is the only thing that talks to Redis, put both on a Docker network and leave Redis unpublished. Containers on the same network reach each other by name.

services:
app:
image: your-app:1.4.2
environment:
REDIS_URL: redis://redis:6379
networks: [backend]
redis:
image: redis:8.2
# no "ports:" section, so nothing is published on the host
networks: [backend]
networks:
backend:

expose: doesn’t publish a port either, so you can keep it for documentation. Only ports: (or -p) opens a port on the host.

Web apps behind a reverse proxy work the same way. Only the proxy publishes 80 and 443, and it reaches each app over the Docker network. Our Traefik setup guide shows that layout.

Fix 2: Binding the port to localhost

Sometimes a process on the host needs the port, such as a backup script or a database client you reach through an SSH tunnel. Bind it to 127.0.0.1, and Docker only listens on the loopback interface:

Terminal window
docker run -d -p 127.0.0.1:6379:6379 redis:8.2

In Compose:

services:
redis:
image: redis:8.2
ports:
- '127.0.0.1:6379:6379'

To reach it from your laptop, tunnel over SSH instead of opening the port:

Terminal window
ssh -L 6379:127.0.0.1:6379 user@your-server-ip

Our PostgreSQL on Docker guide uses the same tunnel for database access.

Fix 3: Changing the default bind address

Fixes 1 and 2 depend on everyone remembering them in every Compose file. You can make 127.0.0.1 the default instead, so a bare -p 6379:6379 only listens on localhost.

For the default bridge network, set ip in /etc/docker/daemon.json:

{
"ip": "127.0.0.1"
}
Terminal window
sudo systemctl restart docker

This only covers containers on the default bridge, which is what docker run uses. Compose creates its own network for each project, and the daemon setting doesn’t apply there. Set the same default on a Compose network with a driver option:

networks:
default:
driver_opts:
com.docker.network.bridge.host_binding_ipv4: '127.0.0.1'

To publish a port on purpose after this change, give the address in full: -p 0.0.0.0:443:443.

Fix 4: Filtering published ports in the DOCKER-USER chain

Fixes 1 to 3 stop ports from opening. This fix filters the ports that are open, so it also catches the next -p someone adds by mistake. It drops every new connection from the internet to a container, except on the ports you list.

First, find your public network interface:

Terminal window
ip -o route show default
default via 203.0.113.1 dev eth0 proto dhcp src 203.0.113.10 metric 100

The name after dev is the interface. It’s often eth0, ens3 or enp1s0.

Rules you add to DOCKER-USER with the iptables command are lost at the next reboot. Put them in /etc/ufw/after.rules instead, and UFW loads them at boot and on every ufw reload. Add this block at the end of the file, after the last COMMIT line, and change eth0 if your interface has another name:

# Filter traffic to published Docker ports
*filter
:DOCKER-USER - [0:0]
-A DOCKER-USER -m conntrack --ctstate RELATED,ESTABLISHED -j RETURN
-A DOCKER-USER ! -i eth0 -j RETURN
-A DOCKER-USER -p tcp -m conntrack --ctorigdstport 80 --ctdir ORIGINAL -j RETURN
-A DOCKER-USER -p tcp -m conntrack --ctorigdstport 443 --ctdir ORIGINAL -j RETURN
-A DOCKER-USER -j DROP
COMMIT

Each rule does one job:

  1. Replies to connections that are already open pass, so containers can still reach the internet.
  2. Traffic that didn’t arrive on the public interface passes, so containers can talk to each other and to the host.
  3. New connections to ports 80 and 443 pass. Add a line like these for each port you want public.
  4. Everything else from the internet is dropped.

The rules match --ctorigdstport, not --dport. By the time a packet reaches DOCKER-USER, Docker has already rewritten its destination, so --dport sees the container’s port. Conntrack still has the port the client asked for. With -p 8080:80, you allow 8080.

Reload UFW and check the chain:

Terminal window
sudo ufw reload
sudo iptables -L DOCKER-USER -n -v

Then run the nmap scan from outside again. Only the ports you listed should answer.

To allow a port only from some addresses, such as a monitoring server, put a source match on the line:

-A DOCKER-USER -p tcp -s 198.51.100.7 -m conntrack --ctorigdstport 9100 --ctdir ORIGINAL -j RETURN

If your proxy only takes traffic from a CDN, the same chain can allow 80 and 443 from the CDN’s address ranges only. That gets its own guide, because the ranges change and need a script to keep them current.

Checking IPv6 separately

/etc/ufw/after.rules only covers IPv4. If your server has a public IPv6 address and Docker publishes ports on [::], add the same block to /etc/ufw/after6.rules and run your scan against the IPv6 address too:

Terminal window
nmap -6 -Pn -p 1-65535 your-server-ipv6

If you don’t use IPv6 for your apps, binding ports to 127.0.0.1 (Fix 2 or 3) avoids the question, because nothing listens on [::] at all.

Picking the right fix

SituationFix
Only other containers use the portFix 1: don’t publish it
A process on the host or an SSH tunnel uses itFix 2: bind to 127.0.0.1
Several people write Compose files on one serverFix 3: default to 127.0.0.1
You want a safety net for mistakesFix 4: DOCKER-USER allow list
Your provider has a network firewallUse it as well. It filters before traffic reaches the server, so Docker can’t bypass it

They aren’t exclusive. A good production setup uses Fix 1 for internal services, Fix 2 for the few host-only ports, and Fix 4 behind both.

Answering common questions about UFW and Docker

Does Docker bypass UFW with host networking?

No. With --network host, the container listens on the server’s own interfaces, the same as a normal process. Traffic goes through INPUT, and UFW filters it as usual.

Can I set “iptables”: false in daemon.json instead?

You can, but don’t. Docker then doesn’t create any rules, so containers lose outbound internet access and published ports stop working until you write the NAT and forwarding rules yourself. Fixes 1 to 4 keep Docker’s networking and still close the ports.

Why does “ufw allow” not open a port to a container?

For the same reason ufw deny doesn’t close one: UFW’s normal rules apply to INPUT, and container traffic goes through FORWARD. A published port is open by Docker’s rules alone. With Fix 4 in place, open a port by adding a RETURN line to the block in /etc/ufw/after.rules.

Does this apply to Docker’s nftables backend?

Docker Engine 29 added an experimental nftables backend. It doesn’t create a DOCKER-USER chain, so Fix 4 doesn’t apply as written; you add your own nftables table that filters forwarded traffic instead. Fixes 1 to 3 work the same on both backends. The iptables backend is still the default.

Conclusion

You found every port Docker publishes on your server, saw why UFW never filters them, and closed the ones you didn’t mean to open. Internal services now stay on a Docker network, host-only ports listen on 127.0.0.1, and the DOCKER-USER chain drops new connections to any port you haven’t listed.

To secure the server further:

Run your apps on servers you own.

Connect a server you already run, or provision one on your cloud account. £5 a month covers the first three.