Docker and UFW: Why Your Published Ports Bypass the Firewall
UFW and Docker both manage iptables, and Docker's rules win. Here's how to check what's really exposed and lock containers down properly.

Infrastructure & Support Team
On this page
You set up UFW on your VPS, allowed only SSH, HTTP and HTTPS, and ran ufw status to double-check. Then you started a database or an admin dashboard in Docker with -p 8080:80, and a port scan shows it's open to the whole internet anyway. This isn't a UFW bug. It's how Docker and UFW interact, and it catches experienced administrators too. This guide explains why Docker bypasses the firewall, how to see what you're really exposing, and the safe ways to fix it.
Why Docker bypasses UFW#
UFW is a friendly front end to iptables (or nftables on newer systems). Docker also writes iptables rules — lots of them — so containers can reach the network and published ports can reach containers.
The key detail is which rules see the traffic:
- UFW's "allow/deny port X" rules sit mainly in the INPUT path: traffic addressed to the host itself.
- A published container port is forwarded to the container's private IP. Docker rewrites the destination (DNAT) before the packet reaches the input rules, so it travels through the FORWARD path instead.
- Docker inserts its own accept rules in that forward path for every published port.
The result: UFW says port 8080 is denied, but the packet never asks UFW. Docker's own documentation warns that Docker and UFW don't work well together for exactly this reason — see Docker's packet filtering and firewalls page.
Step 1: check what you're actually exposing#
On the server, list running containers and their port bindings:
docker ps --format 'table {{.Names}}\t{{.Ports}}'Anything like 0.0.0.0:8080->80/tcp or [::]:8080->80/tcp is published on every interface, including your public IP. Something like 127.0.0.1:8080->80/tcp is local-only.
You can also see the listening sockets:
sudo ss -tulpnThen confirm from a different machine — your laptop, not the server:
nc -vz YOUR_SERVER_IP 8080If that connects, the world can reach it.
Fix 1: bind to localhost (best for most setups)#
Most containers don't need to be public at all. Databases, Redis, admin panels and app servers usually sit behind a reverse proxy. Bind them to the loopback address:
docker run -d --name app -p 127.0.0.1:8080:80 my-app:latestIn Docker Compose:
services:
app:
image: my-app:latest
ports:
- "127.0.0.1:8080:80"
db:
image: postgres:16
# no "ports:" at all — only other containers on this network can reach itThen put a reverse proxy (Nginx, Caddy or Traefik) on ports 80 and 443 and let it forward to 127.0.0.1:8080. UFW only needs to allow 80, 443 and SSH. If you're new to firewall rules on a VPS, our firewall configuration guide covers the basics.
Containers on the same Docker network can talk to each other by service name without publishing any ports. A database that only your app uses should have no
ports:entry at all.
Fix 2: don't publish what you don't need#
Go through your Compose files and delete every ports: entry that exists "just in case". Debug ports, metrics endpoints and database ports left open are among the most common ways small servers get compromised.
Fix 3: filter in the DOCKER-USER chain#
Sometimes a container must be public but only to certain addresses — say, a database that one external office IP connects to. Docker provides a chain called DOCKER-USER that is evaluated before Docker's own forward rules, specifically for your filtering.
A sketch that drops traffic to published ports from anywhere except one trusted address, while keeping replies to existing connections working:
# Allow replies to connections that are already established
sudo iptables -I DOCKER-USER -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
# Drop new connections to containers arriving on the public interface, except from 198.51.100.7
sudo iptables -A DOCKER-USER -i eth0 ! -s 198.51.100.7 -m conntrack --ctstate NEW -j DROPReplace eth0 with your public interface (check with ip -br addr) and the address with your trusted IP. These rules are not persistent across reboots by themselves; save them with your distribution's iptables persistence tooling, and test carefully. A mistake here can cut off containers that need to be public, so keep an SSH session open while you work. Docker's documentation linked above explains the chain in detail.
Seeing which rules traffic actually hits#
When something is reachable and you don't understand why, look at the packet counters instead of guessing. Each iptables rule counts the packets that matched it:
sudo iptables -L DOCKER-USER -n -v --line-numbers
sudo iptables -L DOCKER -n -v
sudo iptables -t nat -L DOCKER -n -vMake a connection attempt from your laptop, then run the commands again. The rule whose counter went up is the one that handled your traffic. In the nat table you'll see the DNAT rule Docker created for the published port — that's the rewrite that sends the packet down the forward path and past UFW's input rules.
On newer distributions the iptables command may be a compatibility layer over nftables. The commands above still work for reading Docker's rules, but if you manage nftables directly, list everything with sudo nft list ruleset to see the complete picture in one place.
Don't forget IPv6#
If your server has an IPv6 address and Docker's IPv6 support is enabled, a container port published on [::] is reachable over IPv6 as well. People often test only the IPv4 address, see the port closed, and miss that it's wide open on IPv6. Check docker ps for [::] bindings and test the IPv6 address from outside too:
nc -6 -vz YOUR_SERVER_IPV6 8080Binding to 127.0.0.1 closes both, because the service is then only reachable through the loopback interface.
Re-test after every change#
Firewall work is easy to get subtly wrong, so make re-testing a habit. After each change:
- Run
docker psand confirm the bindings are what you expect. - Test the port from an outside machine, over IPv4 and (if you have it) IPv6.
- Reboot once during a quiet period and test again. Rules that weren't persisted disappear on reboot, and Docker re-creates its own rules when it starts, so the state after a reboot is the state that matters.
Fix 4: use a provider-level or host-level firewall in front#
If your hosting provider offers a network firewall outside the VM, it filters traffic before it ever reaches Docker's rules. That gives you defence in depth even if a container is published by mistake. Treat it as a second layer, not a replacement for binding services correctly.
What not to do#
You'll find advice to set "iptables": false in /etc/docker/daemon.json. That stops Docker from writing rules, but it also stops Docker from setting up the rules containers rely on for networking. Outbound access from containers and port publishing commonly break in confusing ways. Avoid it unless you're prepared to manage every rule Docker would have created yourself.
Also avoid "fixing" exposure by changing the published port to something obscure like -p 48213:5432. Port scanners check every port; obscurity buys you minutes, not security.
A hardening checklist for Docker hosts#
- List published ports with
docker psand remove anything that doesn't need to be public. - Bind internal services to
127.0.0.1and reach them through a reverse proxy. - Keep databases off published ports entirely; use Docker networks.
- Use the DOCKER-USER chain when a public port must be restricted to certain IPs.
- Re-test from outside the server after every change.
- Keep the host itself hardened: SSH keys, no root login, automatic security updates. Our VPS security hardening guide is a good baseline.
- Back up your volumes. Hardening reduces risk; backups are what save you. See creating backups.
If you need a VPS with full root access to run Docker, LordCloud's VPS plans list the resources for each tier. Whatever host you use, the rule is the same: assume every published port is public until you've proven otherwise from the outside.
Frequently asked questions
Does Docker really ignore UFW?
Effectively, yes, for published container ports. Docker adds its own iptables rules for forwarded traffic, which is evaluated before UFW's normal input rules ever see it.
Should I set iptables to false in Docker's daemon.json?
Generally no. It stops Docker from managing the rules containers need for networking, which commonly breaks outbound access and inter-container traffic. Fix exposure with binding or the DOCKER-USER chain instead.
Is binding to 127.0.0.1 enough?
For services that only a reverse proxy or the host itself should reach, yes. The port is then only reachable from the server itself, and the reverse proxy handles public traffic on 80 and 443.
How do I check which container ports are public?
Run docker ps and look at the PORTS column. Anything shown as 0.0.0.0 or [::] followed by a port is published on all interfaces. Confirm from another machine with nc or nmap.
References
Drafted with AI assistance and checked against LordCloud's published product information. Spotted a mistake?Suggest an edit.



