When you publish a container port with docker run -p 5432:5432 (or ports: ["5432:5432"] in compose.yaml), Docker inserts DNAT rules into the Linux PREROUTING / DOCKER chains before ufw's INPUT chain ever evaluates the packet — exposing Postgres, Redis, or internal admin apps to the entire internet! Fix #1 (simplest): bind internal containers explicitly to loopback (127.0.0.1:5432:5432). Fix #2 (host-wide): add DOCKER-USER firewall rules (ufw-docker) in /etc/ufw/after.rules.
Audit your live VPS right now. Paste your server IP and port (5432, 6379, 8080, 3000) into our 3-Region Open Port Checker — many admins are shocked to see Open despite ufw status showing deny (incoming)!
Bind to 127.0.0.1 in compose.yaml. Writing "127.0.0.1:5432:5432" instead of "5432:5432" keeps local Nginx/Caddy access working while blocking external internet scans 100%.
Never set "iptables": false blindly in /etc/docker/daemon.json unless you manage container NAT masquerading yourself (otherwise containers lose outbound internet access!).
Buried inside Docker's packet-filtering documentation is one of the most dangerous gotchas in Linux self-hosting: ufw filters the INPUT chain, while routed container traffic traverses PREROUTING → FORWARD.
Verify if your ports are accidentally public
Test your VPS IP against common database and app ports right now:
- Port 5432 Checker (PostgreSQL)
- Port 6379 Checker (Redis)
- Port 3306 Checker (MySQL)
- Port 3000 Checker (Node / Grafana)
Fix 1 (Recommended): Bind compose.yaml ports to 127.0.0.1
Any container that sits behind Nginx/Caddy or only talks to other local services should bind explicitly to 127.0.0.1:
services:
postgres:
image: postgres:17-alpine
ports:
# SAFE: reachable only on localhost, invisible to the internet
- "127.0.0.1:5432:5432"
Better yet, if only other containers in the same Compose stack talk to Postgres or Redis, omit ports: completely — containers on the same Docker bridge network can still reach postgres:5432 internally!
Fix 2 (Host-Wide Safety Net): Enforce UFW on DOCKER-USER
Append the ufw-docker block to /etc/ufw/after.rules (replacing eth0 if your primary NIC is ens3 — check ip route show default), then run sudo ufw reload:
# END UFW AND DOCKER
*filter
:ufw-user-forward - [0:0]
:DOCKER-USER - [0:0]
-A DOCKER-USER -j RETURN -s 10.0.0.0/8
-A DOCKER-USER -j RETURN -s 172.16.0.0/12
-A DOCKER-USER -j RETURN -s 192.168.0.0/16
-A DOCKER-USER -j ufw-user-forward
-A DOCKER-USER -j DROP -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -d 192.168.0.0/16
-A DOCKER-USER -j DROP -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -d 10.0.0.0/8
-A DOCKER-USER -j DROP -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -d 172.16.0.0/12
-A DOCKER-USER -j DROP -p udp -m udp --dport 0:32767 -d 192.168.0.0/16
-A DOCKER-USER -j DROP -p udp -m udp --dport 0:32767 -d 10.0.0.0/8
-A DOCKER-USER -j DROP -p udp -m udp --dport 0:32767 -d 172.16.0.0/12
-A DOCKER-USER -j RETURN
COMMIT
After reloading ufw, explicitly allow public reverse-proxy ports via sudo ufw route allow proto tcp from any to any port 80,443 and re-verify in our Port Checker. Looking for a fast Ubuntu 24.04 host with daily backups and DDoS protection? See our Docker VPS plans.
