← Docker Compose Security Check

docker-compose security: privileged, docker.sock, exposed ports and how to fix each

A Compose file decides how much of the host each container can touch. Most compose-related incidents are not clever exploits. They are one line that removed a boundary, such as a database published on every interface or a credential committed in plain text.

# hands the host to the container
services:
  web:
    privileged: true
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

# narrow, loopback-only, pinned
services:
  db:
    image: postgres:16
    ports: ["127.0.0.1:5432:5432"]
    environment:
      POSTGRES_PASSWORD_FILE: /run/secrets/db_password

The thirteen patterns a line scan can find

Is privileged: true ever needed in docker compose?

Rarely. It disables most of the isolation Docker provides, so a compromised container can load kernel modules, read host devices and escape to the host. Almost every real need is narrower: map one device with devices:, or add one capability such as NET_ADMIN with cap_add, and keep privileged off.

Is mounting docker.sock into a container safe?

No. The Docker API is root on the host: a container that can reach it can start another container with the host filesystem mounted. A :ro suffix does not restrict the API. If a dashboard or CI agent needs it, use a socket proxy that exposes only the read-only endpoints required.

How do I stop a published database port being reachable from the internet?

Bind it to loopback in ports: with "127.0.0.1:5432:5432", or remove ports: entirely, since other services on the same Compose network reach it by service name. Remember Docker writes its own firewall rules, so a host firewall such as ufw does not block a port you published on 0.0.0.0.

Check your own compose file

Paste your compose file into Docker Compose Security Check to see every finding with its line, risk and fix, or run the CLI in your own repository:

python3 check.py --fail-on-risk

That exits with a nonzero status on any medium or high finding, so you can wire it into your own CI. Expect some findings to be deliberate (a monitoring agent may truly need the socket); review each one.