Docker ports bypass ufw: check the server your AI agent set up

Yes: on Linux, a container port published with Docker can be reachable from the internet even when ufw status says it is denied, because Docker's own documentation says traffic to a published port is diverted before it reaches ufw's rules. To check, run ss -tlnp and docker ps on the server and scan it once from another machine you own. To fix it, publish ports on 127.0.0.1 and let one reverse proxy listen on 80 and 443.

Published Updated Method

Why can a Docker port be public when ufw says deny?

Docker's documentation says that Docker and ufw “use firewall rules in ways that make them incompatible with each other”. When you publish a container's ports, traffic to and from that container “gets diverted before it goes through the ufw firewall settings”. The reason it gives: Docker routes container traffic in the nat table, so packets are diverted before they reach the INPUT and OUTPUT chains that ufw uses. The packets are routed before the firewall rules can be applied, which the page says effectively ignores your firewall configuration.

A second Docker page adds that publishing container ports is “insecure by default”: a published port becomes available to the Docker host and to the outside world. If you include the localhost address (127.0.0.1, or ::1) with the publish flag, only the Docker host can access the port. The same page warns that in releases older than 28.0.0, hosts on the same layer-2 segment (for example, connected to the same network switch) can reach ports published to localhost.

This is an old behavior, not a new vulnerability, and it is the same on every hosting provider. It matters for apps set up by AI agents because a Compose file, written by an agent or by hand, can publish a database or admin port with a line like "5432:5432", and then nobody looks at it again. We checked three Docker pages on 25 October 2026; Docker behavior can change with the version and with the firewall backend (iptables or nftables), so check the pages for your version.

How do I check my server in two minutes?

Run these on the server. The first command shows what listens and on which address, the second shows what Docker publishes, and the third shows Docker's port mappings in the nat table.

# what listens, and on which address
sudo ss -tlnp

# ports that Docker publishes, per container
docker ps --format 'table {{.Names}}\t{{.Ports}}'

# Docker's own port mappings
sudo iptables -t nat -L DOCKER -n
  • In ss -tlnp, an address of 0.0.0.0 or [::] means the port listens on every interface. 127.0.0.1 means only the server itself can connect.
  • In docker ps, an entry such as 0.0.0.0:5432->5432/tcp is published on all interfaces. 127.0.0.1:5432->5432/tcp is not.
  • Databases, caches, message queues and admin panels are the ports you should not find on 0.0.0.0 unless you meant to expose them.

Then check from outside, because the server cannot tell you what the internet can reach. From a different machine, scan your own server only: nmap -Pn -p- SERVER_ADDRESS. Replace SERVER_ADDRESS with your server's address. Some providers rate-limit or flag scans, so read the provider's rules first. Any port in the result that you did not mean to expose is open.

How do I fix it?

ProblemFixWhere it comes from
A Compose file publishes "5432:5432" or docker run -p 5432:5432Publish on the loopback address: "127.0.0.1:5432:5432" or docker run -p 127.0.0.1:5432:5432. Other containers on the same Docker network can reach the service without any published portDocker's port publishing page
Several services need to be publicKeep one reverse proxy (Caddy, nginx or similar) as the only public listener on 80 and 443, and bind everything else to 127.0.0.1Our recommendation, not a Docker requirement
You want to limit who can reach published portsAdd a rule to Docker's DOCKER-USER chain, shown below. Docker's own jumps in the FORWARD chain run first, so put your rules in DOCKER-USER; ufw rules are not the placeDocker's iptables page
You rely on the hosting provider's firewallA firewall outside the server filters traffic before it reaches the host. We did not test any provider's, so check your provider's documentation for what it blocks by defaultOur reading; unverified
You consider turning off Docker's iptables handlingDocker says this is not recommended for most users because it will likely break container networkingDocker's iptables page

Docker describes DOCKER-USER as “a placeholder for user-defined rules that will be processed before rules in the DOCKER-FORWARD and DOCKER chains”. Its example for restricting external connections to containers is this rule, which drops packets from all IP addresses except 192.0.2.2:

$ iptables -I DOCKER-USER -i ext_if ! -s 192.0.2.2 -j DROP

Docker says to change ext_if to your host's actual external interface, and shows the same rule with a subnet (! -s 192.0.2.0/24). The addresses are from the documentation range; replace them with the address or network that may connect. This rule locks out everyone else, public web visitors included, so it fits services that only your own machines should reach, not a public site. Check how your system restores iptables rules after a reboot before you rely on a hand-added rule.

A prompt you can give your coding agent

Audit how this project exposes network ports. Work read-only: do not restart services, change firewall rules or print secret values.

1. List every `ports:` mapping in all Docker Compose files and every `-p` or `--publish` flag in scripts, Makefiles and docs.
2. For each mapping, say whether it is bound to all interfaces (for example "5432:5432") or to 127.0.0.1 (for example "127.0.0.1:5432:5432").
3. Mark databases, caches, admin panels and message queues that are bound to all interfaces.
4. Propose changes on a new git branch: bind those ports to 127.0.0.1, or remove the mapping if only other containers need the service. Keep one reverse proxy as the only public listener.
5. Report environment variable NAMES only, never values.
6. List the commands I must run on the server myself (`ss -tlnp`, `docker ps`), because you cannot see the server's real state.

What does the Agent Stack Picks data show about agents and self-managed servers?

Agent Stack Picks asks AI models, without a repository or tools, to set up one part of a new app and counts the tool each model names first. Issue 2026-10 has 900 parsed picks. Our data has no Docker category, so it cannot say how often agents choose Docker, and we do not count Docker mentions in the models' free-text reasons. What it does show is where a model's answer is a server you manage yourself. Model pick, not a product recommendation: see the methodology.

Category (issue 2026-10)Hetzner named firstAll picks in the categoryWhere
Hosting6 (9.5 percent; 95 percent interval 4.4 to 19.3 percent)63All 6 in the EU context (6 of 15 EU hosting picks), from 6 different models
Database367EU context only
File storage362EU context only
  • In the same hosting picks, Vercel was named first 36 times and Render 8 times; those are managed platforms rather than a server you administer yourself.
  • The sample is small: 6 hosting picks in one context, one run, one month. It says what models name when asked about EU users, not what coding agents do inside a real project.
  • The issue here is not a provider's security. Docker's behavior is the same on any Linux server that runs Docker.

Checked 25 October 2026

The Docker statements come from three pages of Docker's documentation, read on 25 October 2026: Packet filtering and firewalls (section Docker and ufw), Port publishing and mapping and Docker with iptables. We quote short phrases and copied the DOCKER-USER rule from the last page. The pick counts come from issue 2026-10 (measured 19 October 2026) and are on the open data page. Our other notes this week cover Next.js and Node.js security releases and the Railway security incident.

Questions and answers

Does ufw protect Docker containers?

Not for published ports. Docker's documentation says traffic to a published container port is diverted before it goes through the ufw firewall settings, so a ufw deny rule for that port can have no effect. Check what is reachable from outside instead of reading ufw status.

Does binding to 127.0.0.1 hide a service from the internet?

Per Docker, if you include the localhost address (127.0.0.1 or ::1) with the publish flag, only the Docker host can access the published port. In releases older than 28.0.0, hosts on the same layer-2 segment can reach ports published to localhost, so keep Docker current.

Do I need to disable Docker's iptables handling?

Docker says this is not recommended for most users because it will likely break container networking. Bind ports to 127.0.0.1 and restrict access with the DOCKER-USER chain instead.

How do I test from outside?

From a different machine that you own, scan your own server with nmap -Pn -p- SERVER_ADDRESS and look for ports you did not mean to expose. Check your hosting provider's rules on scanning first, and never scan servers that are not yours.

Does my hosting provider's firewall fix this?

A firewall outside the server filters traffic before it reaches the host, so Docker's rules on the host do not change it. We did not test any provider's firewall, so check which ports your provider's firewall allows by default.

Sources

Related