How do you stop Docker from quietly bypassing ufw?
Here's a homelab and small-VPS gotcha that I think more people should know about, and I'd like to hear how you deal with it. You enable ufw, deny everything incoming, allow SSH, and ufw status looks clean. Then a Compos
Here's a homelab and small-VPS gotcha that I think more people should know about, and I'd like to hear how you deal with it.
You enable ufw, deny everything incoming, allow SSH, and ufw status looks clean. Then a Compose file publishes 5432:5432 and Postgres can be reached from the internet anyway.
The reason is that Docker writes its own packet-filter rules for published ports, and those rules are evaluated before the host firewall rules you set with ufw or firewalld. So ufw deny 5432 isn't the end of the story.
Three patterns I keep seeing:
-
Short port syntax on every interface.
"5432:5432"means0.0.0.0:5432, not "local only". -
network_mode: host. No published ports to review at all: every socket the service listens on is on the host network. -
The dual-stack leak. IPv4 is locked to
127.0.0.1, but the same container port is also published on the IPv6 wildcard::. If a dashboard only shows the IPv4 bind, everything looks fine.
The fixes I know of, each with a trade-off:
- Bind explicitly:
"127.0.0.1:5432:5432"and put a reverse proxy in front of anything public. Simple, but every Compose file has to get it right, overrides included. - Add your own rules to the
DOCKER-USERchain, which Docker leaves for user rules. That's powerful, but it's one more firewall layer to remember. - Set
"iptables": falseindaemon.json. That stops Docker from touching the rules, but then container networking is your job. - Use helper scripts that make ufw aware of Docker.
- Audit what is actually published (
docker ps,docker inspect) instead of trusting the firewall status.
I built a small stdlib-only auditor for that last option and wrote about how it classifies bindings (including the IPv6 case) here: How to Find Docker Ports That Bypass Your Host Firewall. I'm not trying to sell anything here. I'm more interested in what works for you:
- Which approach do you use on a single host or homelab?
- Has
DOCKER-USERheld up for you long term, or did you switch to binding to loopback everywhere? - Does anyone check IPv6 published ports as a routine step?
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes โ full credit and traffic to the original publisher.