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.
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 checked | Command | Result |
|---|---|---|
| Was the live protection actually gone? | sudo iptables -S INPUT | Still 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 status | command 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
| Before | After | |
|---|---|---|
| ufw | Managing SSH/80/443, unaware of Docker | Managing SSH/80/443 and the Docker rule, in one config |
| Docker-published ports | Anything -p exposes reaches the internet, ufw or not | Only 80/443 reachable, regardless of what a container later publishes |
| Persistence | Two competing tools, one of which just got uninstalled by the other | A single ufw reload reapplies everything, including the Docker rule |
Smaller things that went wrong
-
Separately, while turning on HTTP/3 the same evening, a stray extra dash in
compose.yaml(-- "443:443/udp"instead of- "443:443/udp") broke the YAML parser with a crypticcould not find expected ':'. YAML's whitespace- and punctuation-sensitivity makes this kind of typo easy to introduce and surprisingly unobvious to read back. -
Ran
docker compose up -dfrom the home directory instead of~/caddy— Compose only looks forcompose.yamlin the current directory, so it quietly did nothing and reportedno configuration file provided: not foundinstead of guessing where the file might be.
What I learned
- A package install's "Summary" block is worth reading in full, not just the top line —
REMOVING: ufwwas three lines down from the part that looked relevant. - Two tools that both want to own the same piece of system state (here: what iptables looks like at boot) are a conflict waiting to surface, even if nothing says so loudly.
- Before trusting a fresh firewall tool after reinstalling it, check what it would apply (
ufw show added) before telling it to actually apply anything — cheap insurance against locking yourself out over SSH. - Keeping a second SSH session open while touching firewall rules on a remote box is not paranoia, it's the rollback plan.
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.