Illia Nikitchenko

Home / Cases

The ufw fix that uninstalled ufw

Closing a known gap between Docker and ufw should have been a quiet five-minute job. Instead, a single apt install quietly removed the firewall I was trying to fix — and I almost didn't notice.

Starting point

The VPS behind this site runs Caddy in Docker, with ufw handling the firewall: only SSH, 80 and 443 allowed in, everything else denied by default. That part had been true since the server was first set up. What I hadn't fixed yet was a known limitation, sitting on this site's own loose-ends list: Docker manages its own firewall rules and does not ask ufw for permission.

The gap

When a container is published with -p 80:80, Docker inserts its own rules directly into iptables, in a chain that traffic reaches before it would ever hit ufw's own chains. So if I later ran a test container on this same server with, say, -p 5432:5432 for a database I was only meant to reach locally, Docker would cheerfully expose port 5432 to the entire internet — while ufw status kept insisting that only 22, 80 and 443 were open. Technically true, and also wrong.

Internet ufw (INPUT chain) SSH, direct host traffic Host service allowed ports only Docker NAT / FORWARD bypasses ufw by default DOCKER-USER chain rule added here
Everything that reaches a container goes through Docker's own chains first — ufw never sees it unless something explicitly tells Docker's chain to check.

First fix: a rule in DOCKER-USER

DOCKER-USER is a chain Docker creates and always checks first, specifically left empty for the admin to fill in. Adding a rule there is the one supported way to restrict Docker's own port publishing without fighting Docker on every container restart:

sudo iptables -I DOCKER-USER -i eth0 -p tcp -m multiport ! --dports 80,443 -j DROP
sudo iptables -I DOCKER-USER -i eth0 -p udp -m multiport ! --dports 443 -j DROP

In plain terms: anything arriving from the internet through Docker, destined for a port that isn't 80 or 443, gets silently dropped. SSH isn't affected — it never goes through Docker's chains in the first place.

Tested, worked immediately. The only thing left was making it survive a reboot.

Making it permanent

Manually-added iptables rules vanish on restart unless something reapplies them at boot. The obvious-looking answer is the iptables-persistent package:

sudo apt install iptables-persistent -y

Symptom

Buried in the installer's summary, one line that's easy to read past:

Installing:
  iptables-persistent

Installing dependencies:
  netfilter-persistent

REMOVING:
  ufw

Installing a package to persist a firewall rule had just uninstalled the firewall. No prompt, no confirmation — just a line in a longer scroll of apt output.

Checking the damage

What I checkedCommandResult
Was the live protection actually gone?sudo iptables -S INPUTStill jumping into ufw-before-input, ufw-after-input, etc. — the old rules were still loaded in the kernel, just orphaned
Could I still manage it?sudo ufw statuscommand not found — the binary and service were gone, only the stale rules remained
Were the old allow rules still saved on disk?sudo ufw show added (after reinstalling)OpenSSH, 80, 443 — all intact, apt remove had not touched the config files under /etc/ufw/

So the live site was never actually exposed — the currently-loaded kernel rules kept working. But that was luck, not design: the next reboot would have started with no firewall service to reapply anything, leaving only whatever Docker itself decided to open.

Root cause

ufw and iptables-persistent both want to be the thing that restores firewall state at boot. apt's dependency resolver treated that as a conflict and picked a side — silently removing ufw to satisfy the new package — rather than refusing the install or asking. Nothing in the installer's prompts called this out explicitly; it only shows up as one line in the "Summary" block if you're reading closely.

Fix

Backed out of the conflicting package, and went back to a single tool managing the whole firewall:

sudo apt remove iptables-persistent netfilter-persistent -y
sudo apt install ufw -y
sudo ufw show added          # confirm the old rules are still there first
sudo ufw enable
sudo ufw status verbose

The Docker-specific rule moved out of the removed package entirely and into a file ufw already applies on every enable and reload — /etc/ufw/after.rules:

# BEGIN UFW AND DOCKER
*filter
:DOCKER-USER - [0:0]
-A DOCKER-USER -i eth0 -p tcp -m multiport ! --dports 80,443 -j DROP
-A DOCKER-USER -i eth0 -p udp -m multiport ! --dports 443 -j DROP
-A DOCKER-USER -j RETURN
COMMIT
# END UFW AND DOCKER
sudo ufw reload
sudo iptables -L DOCKER-USER -n -v --line-numbers

One tool, one place, one thing to remember to back up.

Result

BeforeAfter
ufwManaging SSH/80/443, unaware of DockerManaging SSH/80/443 and the Docker rule, in one config
Docker-published portsAnything -p exposes reaches the internet, ufw or notOnly 80/443 reachable, regardless of what a container later publishes
PersistenceTwo competing tools, one of which just got uninstalled by the otherA single ufw reload reapplies everything, including the Docker rule

Smaller things that went wrong

What I learned

Loose ends

The DOCKER-USER rule only filters IPv4 right now (eth0); the server also has a public IPv6 address, which means an equivalent ip6tables rule is still open work. Also on the list: extending this same pattern (ufw + explicit Docker rule) before any future lab container gets a -p flag on this box.

← Back to all cases