A couple of weeks ago I wrote here about
running a reverse proxy on a separate gateway server. In the comments,
a reader pointed out something in the docs of my own project that was simply wrong.
The docs said: the admin console listens on port 9443, so allow that port only from trusted networks,
"for example with a firewall". The project runs in Docker. And on a typical Linux server with ufw,
that advice does nothing at all.
$ sudo ufw status Status: active To Action From -- ------ ---- 22/tcp ALLOW Anywhere 80/tcp ALLOW Anywhere 443/tcp ALLOW Anywhere Port 9443 isn't in the list, the default policy is deny, so it's closed. Except it isn't:
# from a machine somewhere else on the internet $ nc -zv my-server.example.com 9443 Connection to my-server.example.com 9443 port [tcp/*] succeeded! If you run anything in Docker with ports: and rely on ufw or firewalld to keep some of those
ports private, check this on your own server before reading further. It takes a minute.
Check your server in a minute
1. List what Docker publishes, and on which address:
docker ps --format 'table {{.Names}}\t{{.Ports}}' NAMES PORTS corvus 0.0.0.0:80->80/tcp, [::]:80->80/tcp, 0.0.0.0:443->443/tcp, ... 0.0.0.0:9443->9443/tcp, [::]:9443->9443/tcp postgres 0.0.0.0:5432->5432/tcp, [::]:5432->5432/tcp Anything with 0.0.0.0: or [::]: in front is published on every address of the host,
including the public one, over IPv4 and IPv6.
2. Test each of those ports from outside, not from the server itself (a phone hotspot works):
nc -zv <public-ip> 5432 # or several at once nmap -Pn -p 5432,9443,8080 <public-ip> If something answers that ufw says is closed, you're affected. The database in the example above is
the classic case: published "for local tools", reachable by the whole internet.
Why it happens
ufw and firewalld are front-ends for the kernel's packet filter. When you write ufw deny 9443, the
rule ends up in the INPUT chain, which handles packets addressed to the host itself.
Docker also writes packet filter rules, on its own, every time it starts a container with published
ports:
- In the nat table, a DNAT rule rewrites "anything arriving for host port 9443" into "container 172.18.0.2, port 9443". This happens in PREROUTING, before any filtering.
- After the rewrite, the packet is no longer addressed to the host. It's addressed to a container, so the kernel forwards it, and it goes through the FORWARD chain, not INPUT.
- In FORWARD, Docker puts its own accept rules for published ports in front of everything else.
packet for :9443 ──► nat PREROUTING: DNAT to 172.18.0.2:9443 │ ▼ FORWARD (not INPUT) ├─ DOCKER-USER ← yours, Docker leaves it alone ├─ DOCKER ← "accept published port" ✔ └─ ufw rules ← never reached for this packet Your ufw rule is correct. It's just in a chain this packet never visits. firewalld ends up in the same
place: Docker puts its interfaces in its own zone, and published ports are reachable whatever your
public zone says.
None of this is a bug, by the way. Docker documents it. It's just very easy to miss, because
ufw status looks so reassuring.
Four ways to fix it
From "best" to "last resort". You'll often combine the first two.
1. Don't publish what doesn't need to be public
The most common reason for a published port is "the reverse proxy needs to reach it". It doesn't need
a published port for that. Put the proxy and the app on the same Docker network and use the
container name:
services: proxy: image: your-reverse-proxy ports: - '80:80' - '443:443' networks: [web] app: image: your-app # no "ports:" at all networks: [web] networks: web: The proxy forwards to http://app:3000, and port 3000 is not published anywhere. Same for
databases: only the containers that need them should be on their network.
2. Publish on a specific address, not on all of them
If you do need a port on the host, say which address it belongs to:
ports: - '127.0.0.1:9443:9443' # only from the server itself (use an SSH tunnel) # or - '10.0.0.5:9443:9443' # only on the LAN / VPN interface For the localhost version, reach it from your laptop with:
ssh -L 9443:127.0.0.1:9443 you@my-server # then open https://localhost:9443 Binding to a VPN address (WireGuard, Tailscale and friends) is a nice middle ground for admin
interfaces: reachable from your devices, invisible to everyone else.
One caveat: docker ps will now show 127.0.0.1:9443->9443/tcp. Check it after every change to the
compose file, it's easy to lose the address when editing.
3. Filter in the DOCKER-USER chain
If you really want a firewall rule, Docker gives you one chain it never touches: DOCKER-USER,
evaluated in FORWARD before Docker's own rules.
There's one trap. By the time the packet gets there, the DNAT has already happened, so its destination
port is the container's port, not the host port you published. Match on the original destination
with conntrack instead:
# allow 9443 only from 192.168.1.0/24, drop it for everyone else arriving on eth0 sudo iptables -I DOCKER-USER -i eth0 -p tcp \ -m conntrack --ctorigdstport 9443 --ctdir ORIGINAL \ ! -s 192.168.1.0/24 -j DROP Things to know before you rely on this:
- Rules added with
iptablesare gone after a reboot. Persist them, for example in/etc/ufw/after.rulesif you use ufw, or with your distribution's iptables persistence package. - If Docker publishes over IPv6 too (
[::]:9443indocker ps), you need the same withip6tables. - Test from outside afterwards. A rule you didn't test is a rule you hope works.
There are helper projects that wire DOCKER-USER into ufw for you (search for "ufw-docker"). They work,
but read what they change first.
4. Let the application check, or filter outside the host
- Many admin interfaces can restrict access by IP themselves. It's enforced by the app, so Docker's NAT doesn't matter. Worth turning on in addition to the above, not instead of it.
- If your server is at a cloud provider, its firewall (security groups and similar) sits outside the host and doesn't care what Docker does to iptables. It's a good outer layer.
What I'd avoid
Setting "iptables": false in Docker's daemon.json makes ufw "work" again by stopping Docker from
writing any rules. It also breaks outbound networking and port publishing for your containers unless
you write all those rules yourself. Not worth it for this.
(The exception is host networking, network_mode: host. There's no NAT and no forwarding, the
container listens on the host directly, and ufw applies as usual.)
What I changed in my own project
The project is Corvus, an open-source reverse proxy with
a built-in WAF, and the fix was mostly in the docs. The install page now has a warning right under the
ports table, and the advice for the admin port lists what actually works: the console's own
allowlist of networks, publishing it on 127.0.0.1 or a LAN/VPN address, or a DOCKER-USER rule.
The reader who pointed it out was right, and the old sentence had been there since 1.0. If you
maintain a Docker-based project whose docs say "close this port with your firewall", it's worth a
second look at yours too.
And if you've found another way that ports leak out of Docker, I'd like to hear it in the comments.