PortGate

Resources · Security guides · Security guide

Do not mount the Docker socket into an app container

Write access to /var/run/docker.sock is root on the host. Find containers that mount it, and give tools that need it a filtering proxy instead.

Check command

docker ps -q | xargs -r docker inspect --format '{{.Name}} {{range .Mounts}}{{.Source}} {{end}}' | grep docker.sock

Copied.

/var/run/docker.sock is the Docker API. Anyone who can send it requests can start a new container with the host's root filesystem mounted and the host's PID namespace, which is root on the host. Mounting the socket "read-only" with :ro does not help: that flag stops the file from being replaced, not API calls from being made through it.

Find containers that have it

docker ps -q | xargs -r docker inspect \
  --format '{{.Name}} {{range .Mounts}}{{.Source}} {{end}}' | grep docker.sock

Every name printed can control Docker. Common ones are reverse proxies that discover containers (Traefik), CI runners, monitoring agents and hosting panels.

Why it matters

If one of those containers is compromised, through a vulnerable web UI, a leaked dashboard password or a bug in the tool, the attacker runs:

docker run --rm -it -v /:/host --privileged --pid=host alpine chroot /host

and has a root shell on the server. No kernel exploit is needed.

Give the tool a filtering proxy

Most tools only need to read container lists and events. A socket proxy exposes the Docker API over a private network with only the endpoints you allow, and rejects everything that writes:

services:
  socket-proxy:
    image: tecnativa/docker-socket-proxy
    environment:
      CONTAINERS: 1
      EVENTS: 1
      POST: 0
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
    networks: [socket]

  traefik:
    image: traefik:v3
    command:
      - --providers.docker.endpoint=tcp://socket-proxy:2375
    networks: [socket, web]

networks:
  socket:
    internal: true
  web: {}

The proxy is the only container with the socket, it sits on an internal network with no route out, and POST: 0 means nothing can create or start containers through it. Never publish port 2375 on the host.

When a tool really needs to create containers

CI runners and deploy tools do. Give them their own host or VM, or run them rootless, so a compromise of the tool is not a compromise of your production server.