Docker published ports and firewalls
| Class | Security misconfiguration |
|---|---|
| Also known as | Docker bypassing ufw, ports published to 0.0.0.0 |
| CWE | CWE-1327: Binding to an Unrestricted IP Address |
| OWASP Top 10 | A02:2025 Security Misconfiguration |
| Typical severity | High, critical when the port belongs to a database |
| Affects | Linux servers running Docker Engine with a host firewall such as ufw or firewalld: docker run -p and Compose ports: entries with no host address |
The flag -p 8080:80 on docker run, or the line "8080:80" under ports: in a Compose file, connects port 8080 on every address of the host to port 80 inside a container.1 Docker makes that connection by writing its own packet filter rules on the host, and those rules run before the rules of ufw, the firewall that ships with Ubuntu.2 A server whose ufw default policy denies all incoming traffic can therefore still answer on any port a container publishes. Docker's own documentation calls publishing a port "insecure by default".1
The behaviour was reported to the Docker project on 18 March 2014, by a user whose mapped ports were reachable from outside although ufw denied all incoming traffic.3 The current firewall page describes Docker and ufw as incompatible.2
Path of a packet
On Linux, the kernel packet filter (iptables by default, or nftables) processes packets in tables and chains. ufw places its rules in the INPUT chain, which handles packets addressed to the host itself. For a published port, Docker adds a rule to the nat table that rewrites the destination of an arriving packet from the host's address and port to the container's internal address and port. That rewrite, called destination NAT, happens before the INPUT chain is consulted.2 The packet is now addressed to another machine (the container) and goes through the FORWARD chain instead, where Docker's chains accept it, and ufw's INPUT rules never see it.4
Docker also leaves the source open: by default, every external address may connect to a port published on the host's addresses.4 Rules appended to FORWARD run after Docker's and never see packets Docker already accepted. On hosts with firewalld, Docker creates a zone named docker with target ACCEPT and a policy that allows forwarding into it from any zone.2
Where the default reaches further
With no host address in the mapping, Docker publishes to 0.0.0.0 and [::], so the port opens on IPv6 as well as IPv4.1 Swarm services ignore a changed default bind address and are always published on 0.0.0.0.1 Before Docker 28.0.0, a port published to 127.0.0.1 could still be reached by other machines on the same local network segment, such as hosts on the same switch.1
A container started with network_mode: host has no port mapping at all: every port its process opens is a port on the host.5 Docker writes no firewall rules for host networking.2 A service bound to all addresses inside such a container is bound to all addresses of the server, a binding that can give "much broader access than intended".6
NewsBlur, June 2021
NewsBlur was moving its database servers into Docker containers. Its operator kept ufw on a strict allowlist of internal servers, but on the new MongoDB server Docker had inserted an allow rule into iptables that opened MongoDB's port 27017 to everyone.7 The server became the primary database at 18:10. At 21:35 an automated attacker connected, dropped the collections and left a note demanding 0.03 BTC for about 250 GB of data. Bandwidth graphs and MongoDB logs showed only deletion commands and no data transfer, and the service was restored from a snapshot of the previous primary by 04:30.7 The follow-up changes were a private network for database servers, database user authentication on every database, and fewer users allowed to drop data; the operator wrote that the service had been "relying on the firewall".7 A database with Default credentials or no password at all, reachable this way, is the case covered in Publicly exposed database.
Closing a published port
Do not publish internal services
Containers on the same bridge network reach each other's ports without publishing; unpublished ports cannot be reached from outside the host.1 A database, cache or queue used only by the app container needs no ports: entry. The Compose expose key documents such internal ports and does not publish them to the host.5
Bind to the loopback address
A loopback address in front of the mapping limits the port to the host itself, which suits a service that a Reverse proxy on the same host forwards to:1
docker run -p 127.0.0.1:8080:80 -p '[::1]:8080:80' nginx# compose.yaml
services:
db:
image: postgres:17
ports:
- "127.0.0.1:5432:5432"To change the default for the default bridge network, set "ip": "127.0.0.1" in /etc/docker/daemon.json and restart the daemon; user-defined bridge networks take the option com.docker.network.bridge.host_binding_ipv4, set per network or for all of them under default-network-opts.1
Filter in DOCKER-USER
With the iptables backend, Docker processes the DOCKER-USER chain before its own forwarding rules, so a rule placed there runs first.4 This drops traffic to published ports from every source outside one subnet, where ext_if is the host's external interface:
sudo iptables -I DOCKER-USER -i ext_if ! -s 192.0.2.0/24 -j DROPPackets reach this chain after the destination rewrite, so a rule that names a port must use the container's port, or match the original address and port with the conntrack extension.4 The nftables backend, experimental since Docker 29.0.0, has no DOCKER-USER chain; rules go in separate tables hooked at the same points.8
Add a provider firewall
A provider firewall is set in the hosting account, not in the server's own packet filter, so Docker's rules on the server do not touch it. A DigitalOcean Cloud Firewall is a network-based service at no extra cost that blocks all traffic no rule permits;9 a Hetzner Firewall with no rules blocks all inbound traffic.10 Setup steps are in Securing a Docker application on a self-managed server.
Checking a host
The mappings of a running container are listed by:
docker container inspect <id> --format "{{json .NetworkSettings.Ports}}"A HostIp of 0.0.0.0 or :: means the port is published on every address of that family.1 ufw can report itself active while such a port is open: at NewsBlur, sudo iptables -L | grep 27017 showed the database open to everyone.7 A port scan from another network shows what answers.
Prompt for a coding agent
Look through every Compose file, docker run command and deploy script in this project for published ports. For each ports entry or -p flag with no host address in front, decide whether the service must be reached from the internet. If only other containers use it (a database, cache, queue or admin tool), remove the published port and keep the services on a shared Docker network. If only a reverse proxy on the same host uses it, add 127.0.0.1: in front of the mapping. Leave the reverse proxy's ports 80 and 443 as they are. Also list any service that uses network_mode: host. Report each file and line you changed and why.
In SecFlash
SecFlash reports a Compose database port with no host address in front as SF-CLD-908 on the Cloud page. On a connected server, a port that listens on every address and answers from outside is SF-SRV-102, a port open on IPv6 only is SF-SRV-104, and containers on the host's network are SF-SRV-407, all on the Server page. A Hetzner server or a DigitalOcean Droplet with no provider firewall is SF-CLD-101 or SF-CLD-201 on the Cloud page.
See also
- Publicly exposed database
- Securing a Docker application on a self-managed server
- Responding to a database exposed to the internet
- Docker socket exposure
- Exposed administration interface
- Category: Self-managed servers
References
- a b c d e f g h i Docker (n.d.). "Port publishing and mapping". Docker Docs.
- a b c d e Docker (n.d.). "Packet filtering and firewalls". Docker Docs.
- back moby/moby (2014). "docker and ufw serious problems", issue #4737. GitHub.
- a b c d Docker (n.d.). "Docker with iptables". Docker Docs.
- a b Docker (n.d.). "Workload specification". Compose file reference, Docker Docs.
- back MITRE (n.d.). "CWE-1327: Binding to an Unrestricted IP Address". CWE.
- a b c d NewsBlur (2021). "How a Docker footgun led to a vandal deleting NewsBlur's MongoDB database". The NewsBlur Blog.
- back Docker (n.d.). "Docker with nftables". Docker Docs.
- back DigitalOcean (n.d.). "Cloud Firewalls". DigitalOcean Docs.
- back Hetzner (2021). "Firewalls: Overview". Hetzner Docs.
External links
- Docker with iptables, Docker Docs