Loading…
Loading documentation...
Prevent Docker published ports from bypassing UFW using loopback bindings, daemon.json defaults, persistent DOCKER-USER iptables rules, and CrowdSec.
Commands checked on 2026-11-23 with Docker Engine 27+, UFW 0.36+, nftables 1.0+, Ubuntu 24.04.
A Docker host where internal databases, vector stores, and admin panels are never exposed on public network interfaces, UFW no longer misleads you about published container ports, and CrowdSec firewall blocks apply to container traffic too.
sudo access on a Linux host running Docker Engine (docker version) and UFW or nftables; see Managed Docker first stack.PREROUTING and routes packets through the FORWARD chain before UFW's INPUT rules run.Check live listening sockets and published Docker mappings:
docker ps --format "table {{.Names}}\t{{.Ports}}"
sudo ss -tulpn | grep -E ':(5432|6379|6333|7700|9000|8080|11434)\b'
Any mapping showing 0.0.0.0:5432->5432/tcp or :::5432 is reachable from outside regardless of ufw deny 5432.
In docker-compose.yml, remove ports: completely for backend services talked to by a reverse proxy or sibling container on a shared Docker bridge network, or bind strictly to loopback:
services:
postgres:
image: postgres:17-alpine
ports:
- "127.0.0.1:5432:5432"
Set a defensive daemon-wide default in /etc/docker/daemon.json so forgotten -p 8080:80 flags default to 127.0.0.1 instead of 0.0.0.0:
sudo tee /etc/docker/daemon.json >/dev/null <<'EOF'
{
"ip": "127.0.0.1",
"log-driver": "json-file",
"log-opts": {
"max-size": "20m",
"max-file": "5"
},
"default-address-pools": [
{ "base": "172.24.0.0/14", "size": 24 }
]
}
EOF
sudo systemctl reload docker || sudo systemctl restart docker
Note that setting custom default-address-pools also prevents bridge CIDR collisions with Kubernetes pod/service CIDRs or overlapping corporate VPN ranges.
When a container must publish public ports (such as Caddy/Traefik on 80/443), Docker evaluates DOCKER-USER before its own DOCKER forwarding rules. Append a DOCKER-USER chain block at the bottom of /etc/ufw/after.rules (using eth0 or your actual WAN interface from ip route show default):
*filter
:DOCKER-USER - [0:0]
-A DOCKER-USER -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A DOCKER-USER -i lo -j RETURN
# Allow traffic arriving from Docker bridges back out to containers/internet
-A DOCKER-USER ! -i eth0 -j RETURN
# Allow public HTTP/HTTPS on WAN interface to reach reverse proxy container
-A DOCKER-USER -i eth0 -p tcp -m multiport --dports 80,443 -j ACCEPT
# Drop unauthorized external packets targeting forwarded container ports
-A DOCKER-USER -i eth0 -j DROP
COMMIT
Reload UFW cleanly without dropping active SSH sessions:
sudo ufw reload
sudo iptables -L DOCKER-USER -n -v
Ensure CrowdSec inspects DOCKER-USER (or nftables forward hook / ipset) rather than only host INPUT, otherwise banned scanners still hit published container ports:
grep -E 'mode|iptables_chains' /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml 2>/dev/null || true
When using iptables mode, include DOCKER-USER in iptables_chains: and restart crowdsec-firewall-bouncer.
Never test firewall exposure from the same machine (localhost traffic skips external NIC paths). Run nmap from a second host against YOUR_SERVER_IP:
nmap -Pn -p 22,80,443,5432,6379,11434 YOUR_SERVER_IP
Expected result: 80 and 443 report open, while 5432, 6379, and 11434 report filtered or closed.
docker build. You forgot -A DOCKER-USER ! -i <WAN_IFACE> -j RETURN or -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT, which drops reply packets to bridge interfaces (docker0, br-*).ufw reload fails with iptables-restore syntax error. Check interface name (ip -br link) and verify iptables-restore -t < /etc/ufw/after.rules before reloading.-A DOCKER-USER -i eth0 -p tcp --dport 2222 -j ACCEPT before the final DROP line, or bind Gitea SSH cleanly.80/443 and let backend databases communicate exclusively over an internal Docker network (internal: true).DOCKER-USER filtering with external perimeter firewall controls in Firewall and port forwarding./etc/docker/daemon.json (max-size: 20m, max-file: 5) so noisy containers never fill /var/lib/docker.Every Servers Are Up virtual machine and container includes an integrated HTML5 browser-based SSH, RDP, and VNC console in your dashboard, default-off inbound firewall rules with per-IP allow-lists, and full programmatic control via REST API v1 and Model Context Protocol (MCP). Explore our Virtual Datacenter Plans or reach out to Servers Are Up Support.