Articles / Linux

  • docker
  • ufw
  • iptables
  • firewall
  • self-hosting
  • linux

Your Self-Hosted App Is Still Reachable After ufw Says Deny

You run an app in Docker, add ufw deny 8080/tcp, see the rule listed, and the app still answers from another machine. ufw isn't broken: traffic to a published container port never reaches the rules you just wrote.

You can reproduce the problem on a test box:

sudo ufw default deny incoming
sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw deny 8080/tcp

docker run -d --name demo -p 8080:80 nginx:alpine

# from a DIFFERENT machine
curl -4 -m 5 http://203.0.113.10:8080/

Replace 203.0.113.10 with your server's address. With Docker's default iptables setup, you get the nginx welcome page.

The packet is redirected before ufw sees it

Docker rewrites the destination of incoming packets in the nat table, from your host's port 8080 to port 80 on the container's private address. That happens before the routing decision.

By the time the kernel routes the packet, it no longer looks like it's for this host, so the kernel forwards it:

  • A connection to a normal host process, such as sshd, goes through the INPUT chain, where your ufw allow and deny rules apply.
  • A connection to a published container port goes through FORWARD, where Docker has added its own rules to accept it.

Docker's documentation is blunt about this: Docker and ufw are "incompatible with each other". You can see the redirect yourself:

docker ps --format '{{.Names}}  {{.Ports}}'
sudo iptables -t nat -L DOCKER -n -v

Anything shown as 0.0.0.0:8080->80/tcp is offered on every IPv4 address the host has. [::] means the same for IPv6.

Best fix: don't publish it to the world

Bind the port to loopback if only the host needs it, which is usually true behind a reverse proxy running directly on that host.

docker rm -f demo
docker run -d --name demo -p 127.0.0.1:8080:80 nginx:alpine

In Compose, it looks like this:

services:
  app:
    image: nginx:alpine
    ports:
      - "127.0.0.1:8080:80"

Recreate the container with docker compose up -d. A plain restart keeps the old port mapping.

If your reverse proxy is also a container, you can go further. Put both on the same Docker network, remove ports from the app altogether, and have the proxy connect to app:80. Only the proxy publishes anything.

Quick detour, because this one is nasty. Before Docker Engine 28.0.0, other hosts on the same layer 2 segment (the same switch, say) could reach ports published to localhost, so "localhost only" wasn't entirely true. Check docker version, and upgrade if the server is older.

Make loopback the default for next time

You will eventually type -p 8080:80 from muscle memory, so change what it means. In /etc/docker/daemon.json:

{
  "ip": "127.0.0.1",
  "default-network-opts": {
    "bridge": {
      "com.docker.network.bridge.host_binding_ipv4": "127.0.0.1"
    }
  }
}

Merge this with anything already in the file, then restart Docker. You need both settings: ip covers the default bridge, while the other covers user-defined networks, including the ones Compose makes.

Existing networks keep their old options, so recreate them (docker compose down, then up -d). An explicit address such as 0.0.0.0:8080:80 still overrides the default.

Need LAN access? Filter in DOCKER-USER

Sometimes the port should be open to some machines and closed to the rest. Docker provides the DOCKER-USER chain for your rules, and it runs before Docker's own forwarding rules.

This drops connections to published port 8080 unless they come from the LAN. Change eth0 to your external interface and set your own subnet. The container must be published on a reachable address, such as 0.0.0.0:8080:80.

sudo iptables -I DOCKER-USER -i eth0 -p tcp \
  -m conntrack --ctorigdstport 8080 --ctdir ORIGINAL \
  ! -s 192.168.1.0/24 -j DROP

Hang on, why not just --dport 8080? Because the destination has already been rewritten by the time the packet gets here, so --dport sees the container's port, 80. Match the original port with conntrack, which remembers what the packet looked like before the rewrite.

You need --ctdir ORIGINAL too. Without it, the rule can also match replies to a container's own outbound connections to some remote port 8080, and drop them.

The rule disappears on reboot. One way to restore it is a small systemd unit, saved as /etc/systemd/system/docker-user-rules.service:

[Unit]
Description=Local DOCKER-USER firewall rules
Requires=docker.service
After=docker.service

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/sbin/iptables -I DOCKER-USER -i eth0 -p tcp -m conntrack --ctorigdstport 8080 --ctdir ORIGINAL ! -s 192.168.1.0/24 -j DROP

[Install]
WantedBy=multi-user.target

Enable it with sudo systemctl enable --now docker-user-rules. If you already added the rule by hand, this inserts a harmless duplicate until the next reboot.

The catch is a short window at boot where containers can be up before the rule is. If that is unacceptable, use a specific bind address or an upstream firewall instead.

Two things that can fool your test

IPv6 takes its own path

The rule above is IPv4 only. If your containers have IPv6 networking, you need matching ip6tables rules with your IPv6 prefix.

On an IPv4-only bridge, Docker's default userland proxy can still accept connections on the host's IPv6 addresses. That is a host process, so I'd expect ufw to filter it while IPv4 sails past. Test with both curl -4 and curl -6.

The nftables backend has no DOCKER-USER

Docker 29 added an nftables backend, described as experimental in the documentation I checked. If you have switched it on, your DOCKER-USER rules may be silently ignored. You need your own nftables table with a base chain on the forward hook instead.

Check from outside, every time

A curl from the server itself takes a different path, so it proves little about what strangers can reach. Run these from another machine:

curl -4 -m 5 http://203.0.113.10:8080/
curl -6 -m 5 'http://[2001:db8::10]:8080/'

After a loopback binding or a working drop rule, both should fail. For the LAN rule, test once from an allowed address and once from outside the subnet. Then reboot and test again.

I wouldn't reach for "iptables": false in the daemon config. Docker warns that it is likely to break container networking, which leaves you maintaining the NAT and forwarding rules yourself. That is a lot of work to avoid typing 127.0.0.1:.