SaferComputing.com

Reference · Network exposure

Docker published ports bypass UFW: check and fix -p 0.0.0.0

A container published with docker run -p 8080:80 listens on every host address, and Docker's own firewall rules handle it before UFW sees the traffic. Find exposed containers with docker ps and republish them on 127.0.0.1.

Published
Updated
Last checked
Applies to
Docker Engine on Ubuntu and other Linux hosts using UFW or firewalld
Technical specification
Target artifactContainer ports published with docker run -p or a Compose ports: list, on a Linux host protected by UFW or firewalld.
Default behaviour-p 8080:80 publishes the port on every address of the host. Docker diverts that traffic in its own firewall rules before it reaches the chains UFW uses, so ufw deny does not stop it.
Safe stateContainers that only the host needs are published on 127.0.0.1; every port published on all addresses is one you meant to offer; any filtering of published ports sits in Docker's DOCKER-USER chain, or with the nftables backend in a table of your own; Docker Engine 28.0.0 or later.
Check on Linuxdocker ps --format 'table {{.Names}}\t{{.Ports}}'
Check from another devicenc -vz -w 5 SERVER_IP PORT
Fix on Linuxrecreate the container with -p 127.0.0.1:HOST_PORT:CONTAINER_PORT, or "127.0.0.1:HOST_PORT:CONTAINER_PORT" in the Compose ports: list

Read the result

Port column in docker psMeaning
0.0.0.0:8080->80/tcpPublished on every IPv4 address of the host, whatever UFW says
[::]:8080->80/tcpThe same on every IPv6 address
127.0.0.1:8080->80/tcpOnly the host itself can connect
80/tcp with no arrowNot published on the host

Docker's documentation says that publishing container ports is insecure by default: a published port is available not only to the host but to the outside world.

Why UFW does not see the traffic

UFW's usual rules filter traffic addressed to the host itself. Traffic to a published port is rewritten by Docker's own rules and forwarded to the container, so Docker's chains handle it before UFW's rules see it. A sudo ufw status that shows only ports 22, 80 and 443 says nothing about a database container published on 0.0.0.0:5432.

Fix it

docker run
Linux, as a user in the docker group
docker stop CONTAINER && docker rm CONTAINER
docker run -d -p 127.0.0.1:8080:80 --name CONTAINER IMAGE

Repeat the rest of the original docker run options, such as volumes, environment variables and the restart policy. docker inspect CONTAINER shows them before you remove it.

Docker Compose
Lines in docker-compose.yml
    ports:
      - "127.0.0.1:8080:80"

Then run docker compose up -d in the same folder to recreate the container.

A container that must be reachable from some addresses only

Publish it, then filter in the DOCKER-USER chain, which Docker provides for your own rules and processes before its own. Docker's packet-filtering documentation, under "Restrict external connections to containers", gives the rules for your firewall backend. Rules you add to UFW do not apply. Docker 29.0.0 added an nftables backend, experimental and off unless the daemon runs with --firewall-backend=nftables or "firewall-backend": "nftables" in daemon.json; it has no DOCKER-USER chain, and Docker's documentation says to put your rules in a separate table, in base chains with the same types and hook points as Docker's.

Reaching a loopback-only container from a laptop
Your laptop
ssh -N -L 8080:127.0.0.1:8080 USER@SERVER

While the tunnel runs, http://127.0.0.1:8080 on the laptop reaches the container, and nothing new listens on the server's network interfaces.

Troubleshooting

Published on 127.0.0.1, but another machine on the same network still connects

On Docker Engine releases older than 28.0.0, hosts on the same local network segment could reach ports published to 127.0.0.1. Check with docker version and update the engine.

A container shows no ports, yet something answers

A container started with --network host (or network_mode: host in Compose) shares the host's network, so its ports are not published and docker ps shows no arrow for them. They listen directly on the host; find them with sudo ss -tulpn from the open-port self-audit.

Sources