UFW rules don't block (or unexpectedly allow) a Docker container's port
ufw status shows DENY for port 8080, but the container is still reachable from the internetWhat's happening
Docker manipulates iptables directly when you publish a port (`-p 8080:8080` or `ports:` in Compose), inserting its own rules in the `DOCKER-USER` and `nat` chains. These rules are processed before UFW's rules, so a container's published port can remain reachable from the public internet even though `ufw status` shows it denied — UFW is managing the `ufw-*` chains, but Docker's NAT rules route traffic around them. This is the opposite problem to the normal expectation and a common surprise/security issue on self-hosted VPS setups.
Fix
1. Don't rely on UFW alone to firewall Docker-published ports — add explicit rules to the `DOCKER-USER` chain, which Docker respects: ```bash iptables -I DOCKER-USER -p tcp --dport 8080 ! -s <allowed-ip-range> -j DROP ``` 2. Alternative: don't publish the port to all interfaces. Bind to localhost only and let a reverse proxy (Nginx/Caddy/Traefik) handle public exposure: ```yaml ports: - "127.0.0.1:8080:8080" ``` 3. If you need UFW to be the single source of truth, configure Docker to not manipulate iptables at all (`"iptables": false` in `/etc/docker/daemon.json`) and manage all port exposure manually — this requires you to replicate NAT rules yourself, so only do it if you understand the tradeoff. 4. Verify from an external host, not just `ufw status` — `nmap -p 8080 your-server-ip` tells you what's actually reachable.
Got a different error? Paste it here for a free diagnosis.