PC-018HIGHDocker & Infrastructure

Database Port (3306, 5432, 6379, 27017) Bound to 0.0.0.0 in docker-compose.yml

Flags MySQL (3306), PostgreSQL (5432), Redis (6379), and MongoDB (27017) port mappings in Docker Compose files that bind to all host network interfaces (0.0.0.0) and bypass UFW firewalls.

Rule Slug
docker-compose-exposed-database-port
Affected Files
docker-compose*.yml, docker-compose*.yaml, compose*.yml, compose*.yaml
Detection Method
Deterministic Regex
Live Rule Tester100% Browser-Local
HIGH: Detected 2 violations
Line 6:- "5432:5432" # HIGH RISK: Binds 0.0.0.0:5432 and bypasses UFW!
Line 10:- "0.0.0.0:6379:6379"

Vulnerable Pattern vs. Hardened Fix

Vulnerable Pattern
# docker-compose.yml
services:
  postgres:
    image: postgres:16-alpine
    ports:
      - "5432:5432" # HIGH RISK: Binds 0.0.0.0:5432 and bypasses UFW!
  redis:
    image: redis:7-alpine
    ports:
      - "0.0.0.0:6379:6379"
Recommended Fix
# docker-compose.yml
services:
  postgres:
    image: postgres:16-alpine
    # Option A (Production): Omit 'ports:' entirely; app connects via postgres:5432
    # Option B (Localhost only): Bind strictly to loopback
    ports:
      - "127.0.0.1:5432:5432"
  redis:
    image: redis:7-alpine
    expose:
      - "6379"

Why This Fails in Production

One of the most notorious Linux server security traps occurs when deploying a docker-compose.yml file containing a shorthand port mapping like - "5432:5432" or - "6379:6379" onto a VPS or cloud VM. By default, Docker interprets a host port without an explicit IP address as 0.0.0.0:5432, binding your database container to every network interface on the host, including its public internet-facing NIC.

Crucially, Docker manipulates Linux iptables (DOCKER-USER / NAT PREROUTING chains) directly, which completely bypasses standard host firewalls such as UFW (ufw allow 80,443) and firewalld rules. Even system administrators who diligently verify ufw status shows only ports 22, 80, and 443 open are routinely shocked to find their PostgreSQL, MySQL, MongoDB, or Redis instances directly reachable from Shodan and automated internet bots.

Unauthenticated Redis (6379) and MongoDB (27017) containers exposed in this manner are typically discovered and compromised within minutes, resulting in instant database ransomware notes, Redis slave-replication remote code execution, or silent credential brute-forcing against Postgres (5432) and MySQL (3306).

Containers running on the same Docker Compose bridge network communicate over internal service hostnames (such as postgres:5432 or redis:6379) without publishing any host port at all. If you genuinely need local host access for CLI tools or SSH tunneling, explicitly bind the port to loopback (127.0.0.1:5432:5432) so the database is never exposed to external network interfaces.

Remediation Steps

  1. 1Remove the ports: mapping completely for database services in production docker-compose.yml; application containers on the same Compose network connect automatically via service name and internal port.
  2. 2If local host access is required for development or SSH tunnel debugging, explicitly prefix the loopback IP: - "127.0.0.1:5432:5432".
  3. 3Verify externally exposed ports from another machine or with ss -tulpn on the host after redeploying.
  4. 4Ensure every database container enforces strong authentication passwords and TLS even on private internal Docker networks.
OpsCheck: Validate & Harden Your Docker Compose File

Run a full network, healthcheck, and port exposure audit with the OpsCheck Docker Compose Validator.

Open OpsCheck ↗

Frequently Asked Questions

Does UFW block Docker ports like "5432:5432" automatically?
No! Docker inserts its own NAT iptables rules ahead of UFW’s INPUT chain. Unless you bind to 127.0.0.1:5432:5432 or remove the ports block, the database port is exposed publicly on the internet even when UFW says it is closed.
How do my app containers talk to Postgres if I remove the ports: section?
Docker Compose creates an isolated bridge network automatically. Your app container connects directly to postgres:5432 using the Compose service name as the hostname, with zero host ports published.
What is the difference between ports: and expose: in Docker Compose?
ports: publishes ports onto the host machine’s network interfaces. expose: only documents internal container-to-container ports on the Docker network and never opens them to the host or internet.

Related Docker & Infrastructure Rules

Scan local project folder →