BLACK FRIDAY 2026 LIVE: RackNerd 4GB VPS $47.98/yr (Lifetime) · ElevenLabs 55% Off · Hostinger 73% Off · Amazon Local AI Hardware View Verified Deals →
Lab Essay #07 · Security & Firewall7 min readReal Lab Measurements

The Docker UFW Bypass Trap: Why Your Firewall Is Wide Open Right Now (60-Second Port Audit)

TL;DR

Enabling `ufw default deny incoming` gives self-hosters a fatal false sense of security. Whenever your `docker-compose.yml` writes `ports: - "5432:5432"` or `"5678:5678"`, Docker injects NAT rules directly into `iptables` BEFORE UFW’s `INPUT` chain, exposing your database or admin UI to the entire internet. Fix it permanently by removing host port mappings for internal containers and binding any reverse-proxied port exclusively to `127.0.0.1:5678:5678`.

Terminal screenshot showing postgres 127.0.0.1:5432, redis 127.0.0.1:6379, and nginx 127.0.0.1:8095 bound strictly to loopback with zero public database ports
Figure 1 · Live ss -tulpn Loopback Audit on Production Ubuntu 24.04 Host: Every internal service on our live KVM host binds strictly to 127.0.0.1 (Postgres 5432, Redis 6379, origin Nginx 8095). Even if a firewall rule is flushed during a kernel update, zero internal ports are reachable over public IPv4.

1. Why Shodan Finds Unhardened AI Servers in 9 Minutes

Every weekend, builders spin up a fresh Ubuntu 24.04 VPS, responsibly run `ufw allow 22,80,443/tcp && ufw enable`, and copy a Compose file containing `ports: - "5678:5678"` or `ports: - "6379:6379"`. They assume UFW blocks ports 5678 and 6379 from outside traffic.

It does NOT. Because Docker routes published container ports via `iptables` `PREROUTING` → `FORWARD` (`DOCKER-USER` chain), packets headed for published container ports skip UFW’s `ufw-before-input` chain completely. Automated scanners sweep public IPv4 space for exposed Redis (6379), Postgres (5432), Ollama (11434), and unauthenticated n8n/OpenClaw gateways within minutes of provisioning.

2. The Zero-Magic Fix: Internal Docker Bridges + Explicit 127.0.0.1 Binding

Some blog posts tell you to edit `/etc/docker/daemon.json` with `"iptables": false`—do NOT do that, because it breaks outbound container DNS and inter-container NAT unless you hand-maintain firewall tables. The clean, unbreakable pattern requires zero daemon hacks:

  • Delete `ports:` completely on PostgreSQL, Redis, and Qdrant containers: n8n talks to `postgres:5432` and `redis:6379` over the private Docker bridge network, which is unreachable from the internet.
  • When Caddy runs in the SAME `docker-compose.yml`, delete `ports:` on n8n/OpenClaw too—only Caddy needs `80:80` and `443:443`.
  • When using host Nginx or `cloudflared` on the host OS, ALWAYS prefix container ports with loopback: `"127.0.0.1:5678:5678"`.
  • Disable SSH password login (`PasswordAuthentication no` in `/etc/ssh/sshd_config`) and verify key-only login before closing your session.
# 1. List ONLY public-facing listening sockets (should show ONLY 22, 80, 443)
sudo ss -tulpn | awk 'NR==1 || /0\.0\.0\.0:|\[::\]:/'

# 2. Safe loopback-only Compose port mapping when reverse-proxying from host
services:
  n8n:
    ports:
      - "127.0.0.1:5678:5678"   # NEVER "5678:5678"
  postgres:
    # NO ports: block at all! Reachable only as postgres:5432 on internal bridge.

Related Engineering Deep-Dives