Checks / Docker and containers
Container ports are published on all addresses
- Check
docker.public_ports- Severity
- medium
- Runs
- on the server, in the daily run of the agent
- Looks at
- Ports published on 0.0.0.0 or :: (Docker adds its own iptables rules and bypasses ufw)
What the check looks at
The agent reads the port bindings of every running container. It reports each port published without a host
address or with 0.0.0.0 or ::, with the container name, for example db 5432/tcp on 0.0.0.0. Ports
bound to 127.0.0.1 or ::1 pass. Containers on the host network are covered by
docker.host_network and the list of listening sockets.
Why it matters
-p 5432:5432 or ports: ["5432:5432"] publishes the port on every address of the host. Docker reaches the
container with a DNAT rule and its own chains at the start of the FORWARD chain. These packets never pass
the INPUT rules of ufw, and firewalld forwards them through the docker zone that Docker creates with the
target ACCEPT. The Docker documentation calls Docker and ufw "incompatible". A database or admin interface
published this way is open to the internet although ufw status does not list it.
The warning is at medium severity because many published ports are meant to be public, such as 80 and 443 of a reverse proxy.
How to fix it
The fix is the same on every distribution.
-
Decide for each reported port whether it must be reachable from other machines.
-
If only the host needs it (a reverse proxy, a backup job, you through an SSH tunnel), bind it to loopback:
services: db: ports: - "127.0.0.1:5432:5432"or
docker run -p 127.0.0.1:5432:5432 ..., then recreate the container (docker compose up -d <service>). -
If only other containers need it, remove the
ports:entry. Containers on the same Docker network reach each other by service name. -
If some outside addresses need it, keep the binding and filter in the
DOCKER-USERchain with-m conntrack --ctorigdstport <port>; the guide Docker bypasses ufw has a tested script and a systemd unit for it.
To reach a loopback port from your machine, use an SSH tunnel:
ssh -L 5432:127.0.0.1:5432 user@server.
How to check the fix
docker ps --format 'table {{.Names}}\t{{.Ports}}'
0.0.0.0 or [::] should appear only for the ports you want public. From another machine,
nc -zv -w 3 <server> 5432 should fail.
When a warning is fine
Ports that are meant to be public, such as 80 and 443 of a reverse proxy, a mail server or a public API. Accept the result for that container on the dashboard. A firewall at the hosting provider in front of the server also blocks a port, since Docker cannot change it. The finding then still tells you that the server itself does not filter the port, which matters on the day someone changes the rules at the provider.
An accepted result is left out of the digest and the counts until it changes; then it shows again.
Sources
- Docker docs: Packet filtering and firewalls
— "Docker and ufw", firewalld zone
docker. - Docker docs: Port publishing and mapping —
default bind address
0.0.0.0and[::]. - Docker docs: Docker with iptables —
DOCKER-USER,--ctorigdstport.