Why Docker published ports bypass UFW, and how to lock them down

Docker writes its own iptables rules, so ufw deny does not stop traffic to published container ports. Bind to localhost or filter in the DOCKER-USER chain instead.

3 min read

Contents
  1. The surprise
  2. Why UFW does not see the traffic
  3. Fix 1: do not publish what does not need to be public
  4. Fix 2: filter in the DOCKER-USER chain
  5. What not to do
  6. Check from the outside
  7. Summary

The surprise

A very common setup on a small server looks like this: UFW is enabled with a default deny policy, only SSH and HTTPS are allowed, and a few services run in Docker. Then one day you run a port scan from outside and find your Postgres or Redis container answering on the public interface, even though ufw status never mentions that port.

Nothing is broken. This is how Docker networking works, and once you know why, the fix is simple.

Why UFW does not see the traffic

UFW is a frontend for iptables (or the nftables backend of iptables). Its rules live mostly in the INPUT chain, which handles packets addressed to the host itself.

When you publish a port with -p 5432:5432, Docker adds its own rules:

  • a DNAT rule in the nat table (DOCKER chain, reached from PREROUTING) that rewrites the destination from the host IP to the container IP;
  • an accept rule in the filter table’s FORWARD path for that container.

Because the destination is rewritten before the routing decision, the packet is no longer “for the host”. It is forwarded to the container’s bridge network and never passes through INPUT, where UFW’s deny rules are waiting. You can see this yourself:

sudo iptables -t nat -L DOCKER -n --line-numbers
sudo iptables -L FORWARD -n --line-numbers

You will find a DNAT entry for every published port and a jump to DOCKER-USER near the top of FORWARD. That chain is the hook Docker leaves for you.

Fix 1: do not publish what does not need to be public

Most of the time the database should not be reachable from the internet at all. The cleanest fix is to publish only on the loopback address:

# compose.yaml
services:
  db:
    image: postgres:16
    ports:
      - "127.0.0.1:5432:5432"

Now the DNAT rule only matches traffic to 127.0.0.1. Local tools and an SSH tunnel still work:

ssh -L 5432:127.0.0.1:5432 user@server

Even better, if only other containers talk to the database, remove the ports section completely. Containers on the same Compose network reach each other by service name (db:5432) without publishing anything.

If you want loopback to be the default for every published port that does not name an address, set it in /etc/docker/daemon.json and restart Docker:

{
  "ip": "127.0.0.1"
}

After that, -p 8080:80 binds to localhost, and you have to write -p 0.0.0.0:8080:80 on purpose to expose something. I like this on servers because exposure becomes an explicit decision.

Fix 2: filter in the DOCKER-USER chain

Sometimes a port must be published, but only some clients should reach it, for example an admin UI that only the office network may use. Docker never flushes DOCKER-USER, and it is evaluated before Docker’s own forwarding rules, so this is where your filtering goes.

One detail matters: by the time a packet reaches FORWARD, the destination has already been rewritten to the container IP and container port. To match on the port the client originally connected to, use conntrack:

# allow 203.0.113.0/24 to reach published port 8080, drop everyone else on eth0
sudo iptables -I DOCKER-USER -i eth0 -p tcp \
  -m conntrack --ctorigdstport 8080 --ctdir ORIGINAL \
  ! -s 203.0.113.0/24 -j DROP

Replace eth0 with your external interface (ip route get 1.1.1.1 shows it). Limiting the rule to the external interface keeps traffic between containers and from the host working.

Rules added with iptables are lost on reboot. Persist them with iptables-persistent (sudo netfilter-persistent save), or add them in /etc/ufw/after.rules under a *filter section that declares :DOCKER-USER - [0:0], so they load together with UFW. Test after a reboot before you trust it.

What not to do

You may find advice to set "iptables": false in daemon.json. That stops Docker from writing rules, but it also breaks outbound NAT and published ports for containers until you rebuild all of that by hand. It trades one surprise for several, so I avoid it.

Check from the outside

Firewall changes are only real once you test them from another machine:

# from a laptop or a second server
nmap -Pn -p 22,80,443,5432,6379,8080 your.server.ip

On the server, sudo ss -tlnp shows which addresses each port is bound to. A line like 0.0.0.0:5432 or [::]:5432 owned by docker-proxy means the port is published on all interfaces.

Summary

  • Docker’s DNAT rules send published-port traffic through FORWARD, so UFW’s INPUT rules never apply.
  • Publish internal services on 127.0.0.1, or not at all, and use Compose networks between containers.
  • Use "ip": "127.0.0.1" in daemon.json to make public exposure opt-in.
  • For ports that must be public but restricted, add rules to DOCKER-USER and match the original port with conntrack.
  • Always verify with a scan from outside.