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

- URL: https://nodebudget.dev/guides/docker-security-hardening-ufw-bypass-myth/
- Author: Yuanqiang Leng (https://github.com/a895047059)
- Updated: 2026-11-26

## 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`.

## 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.

> **Audit Your Live Server Right Now**: SSH into your VPS and run `ss -tulpn | grep -E "0\.0\.0\.0|\[::\]"`. If you see `:5432`, `:6379`, `:5678`, `:3000`, or `:11434` bound to `0.0.0.0` or `*`, that port is publicly reachable right now—regardless of what `ufw status` claims.

## 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.

```bash
# 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.
```

---
Source: NodeBudget (https://nodebudget.dev) · License: CC BY 4.0 · Disclosure: Some outbound links under /go/* earn affiliate commission. As an Amazon Associate I earn from qualifying purchases.