How to Fix Docker Compose Exposed Ports (0.0.0.0:5432) & docker.sock Mounts
Docker bypasses UFW by default whenever you write "5432:5432" or "6379:6379" without a
127.0.0.1 prefix. Fix DKR001–DKR006 cleanly and verify your docker-compose.yml
locally below with 0 bytes uploaded.
100% client-side YAML AST engine · 0 bytes sent · One-time Polar.sh license (no subscription) unlocks all 72 rules + CI SARIF 2.1.0.
Vulnerable vs. Hardened docker-compose.yml (Before & After)
This side-by-side diff eliminates public database exposure (DKR001), container breakout via
privileged: true (DKR002) and docker.sock (DKR003),
unpinned :latest tags (DKR004), network_mode: host (DKR005),
and cap_add: SYS_ADMIN (DKR006).
services:
postgres:
image: postgres:latest
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: demo_plaintext_db_pass_912
worker:
image: myorg/worker:latest
privileged: true
network_mode: host
cap_add:
- SYS_ADMIN
volumes:
- /var/run/docker.sock:/var/run/docker.sock
services:
postgres:
image: postgres:16.6-alpine
restart: unless-stopped
ports:
- "127.0.0.1:5432:5432"
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?required}
security_opt: ["no-new-privileges:true"]
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 15s
worker:
image: myorg/worker:1.4.2
user: "1000:1000"
restart: unless-stopped
read_only: true
security_opt: ["no-new-privileges:true"]
cap_drop: ["ALL"]
mem_limit: 512m
Lint Your docker-compose.yml Locally (All 18 Docker Rules)
100% in-browser · 0 bytes sent · < 10 ms
Paste any docker-compose.yml or compose.yaml below to audit exposed ports,
docker.sock mounts, privilege escalation, secrets, and resource limits locally in your browser.
Exact Remediation Reference: Rules DKR001–DKR006
| Rule ID | Severity | Vulnerability & CWE | Exact Fix |
|---|---|---|---|
DKR001 |
HIGH |
Database, cache, or admin port exposed on 0.0.0.0
|
Prefix host ports with 127.0.0.1:5432:5432 or drop ports: entirely so services only talk over an internal Docker bridge network. |
DKR002 |
CRITICAL |
Privileged container enabled (privileged: true)
|
Remove privileged: true, set cap_drop: ["ALL"], and enable security_opt: ["no-new-privileges:true"]. |
DKR003 |
CRITICAL |
Docker daemon socket (/var/run/docker.sock) mounted
|
Remove direct docker.sock bind mounts from app/proxy containers; route read-only container discovery through tecnativa/docker-socket-proxy. |
DKR004 |
MEDIUM |
Unpinned container image tag (:latest or missing tag)
|
Pin explicit immutable versions or SHA-256 digests (e.g. postgres:16.6-alpine) so deployments are reproducible and auditable. |
DKR005 |
HIGH |
Host network mode enabled (network_mode: host)
|
Remove network_mode: host and isolate services on a user-defined Compose bridge network with explicit loopback or reverse-proxy ingress. |
DKR006 |
HIGH |
Dangerous Linux capability added in cap_add
|
Drop ALL, SYS_ADMIN, SYS_PTRACE, and NET_ADMIN; grant only minimal non-root capabilities such as NET_BIND_SERVICE if required. |
Frequently Asked Questions
Why does ports: "5432:5432" in docker-compose.yml bypass UFW firewall rules on Linux?
Docker injects DNAT iptables rules ahead of UFW's user input chains, exposing any 0.0.0.0 mapped port directly to the internet unless bound to 127.0.0.1:5432:5432 or kept internal without ports:.
Why is mounting /var/run/docker.sock (DKR003) equivalent to root on the host?
Any container able to call the Docker API can start a privileged container mounting /:/host, gaining immediate host root shell access.
Does the embedded Docker Compose linter upload my YAML file?
No. StackGuard runs its 18-rule YAML AST analyzer 100% locally in your browser with zero outbound network requests.
How can I block DKR001–DKR018 automatically in CI/CD?
Run stackguard-cli in GitHub Actions with SARIF 2.1.0 output and install the StackGuard .vsix extension in VS Code, Cursor, or Windsurf for 1-click Quick Fixes.