The Docker socket is root

Advanced · 5 min

Why access to /var/run/docker.sock, or the docker group, equals root on the host, why read-only mounts don't help, and safer ways to give tools access.

Before thisContainer isolation and its limitsThe Docker architecture: client, daemon and runtimes

Better Stack · 5 min

The Docker socket is root

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Better StackReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • /var/run/docker.sock is the Docker Engine API, plain HTTP over a Unix socket. Anything that can write to it can do everything the daemon can, and the daemon runs as root.
  • With API access, one request creates a container that bind-mounts the host's / and runs as root. No exploit is needed, so it isn't treated as a vulnerability.
  • Being in the docker group is the same as having passwordless root. Docker's own post-install docs say the group grants root-level privileges.
  • Mounting the socket read-only changes nothing. The :ro flag only stops changes to the socket file, not requests sent through it.
  • Common socket consumers are reverse proxies, dashboards, auto-updaters and CI runners. Each one is a path from its bugs to host root.
  • Mitigations, from strongest: don't mount it; use rootless Docker so the daemon has only a user's power; put a filtering proxy in front that allows only the read endpoints a tool needs; use an AuthZ plugin.
  • Never expose the API on TCP without TLS client certificates. For remote access, use SSH through a Docker context.

A file that is a remote control

On a Linux host, docker ps does not look at containers itself. The CLI sends an HTTP request to the daemon over a Unix socket, /var/run/docker.sock, as the Docker architecture lesson showed. The daemon runs as root and does the work.

So the socket is a remote control for a root process. Whoever holds it can ask the daemon to do anything the daemon can do. This lesson is about the many ways people hand that remote control out without noticing.

From socket access to host root in one request

You don’t need the docker CLI to use the socket. curl speaks HTTP over Unix sockets. The Engine API is at version 1.57 in the current Moby source. These are the calls, written from the API’s OpenAPI definition. I did not run them, because there is no daemon here:

# create a container that bind-mounts the host's root filesystem
curl -s --unix-socket /var/run/docker.sock \
  -H 'Content-Type: application/json' \
  -d '{"Image":"alpine","Cmd":["sleep","3600"],"HostConfig":{"Binds":["/:/host"]}}' \
  http://localhost/v1.57/containers/create?name=x

curl -s --unix-socket /var/run/docker.sock -X POST \
  http://localhost/v1.57/containers/x/start

Inside that container, /host is the host’s /, and the process is UID 0 with no user namespace. Editing /host/etc/shadow, adding a key to /host/root/.ssh/authorized_keys or dropping a cron job into /host/etc/cron.d gives full control of the machine. Add "Privileged":true and "PidMode":"host" and the container can also load kernel modules and see every host process.

No bug is exploited. The daemon did exactly what an authorised client asked. Moby’s threat model says so: Engine API access “grants the daemon user’s privileges”, API clients are trusted, and a bug isn’t a vulnerability “if the attacker needs API access”.

The read-only myth

A common compromise is mounting the socket read-only:

volumes:
  - /var/run/docker.sock:/var/run/docker.sock:ro

It doesn’t help. :ro makes the file read-only: you can’t delete or replace the socket node. Connecting to a socket and sending requests is not a write to the file. The threat model is explicit: “a read-only bind mount does not make the API read-only”.

The docker group is the same thing

On Linux the socket is owned by root:docker with mode 660, so members of the docker group can use it without sudo. That convenience is the same hole with a different door. Docker’s post-install page carries the warning: the docker group grants root-level privileges to the user. Treat membership exactly like sudo rights, except that sudo at least asks for a password and logs the command.

Who asks for the socket

The socket appears in many popular setups:

  • Reverse proxies such as Traefik read container labels to build routes.
  • Dashboards such as Portainer manage containers through it.
  • Auto-updaters pull new images and recreate containers.
  • CI runners build and run images by talking to the host’s daemon, often called Docker-outside-of-Docker.
  • Log and metrics agents read container metadata.

Each of them is a web-facing or network-facing program. A bug in any one is a path to host root. Compose even has a shortcut, use_api_socket: true, which mounts the socket and also your registry credentials into the container, so the container acts “as a pure delegate” for you, in the reference’s words. Use it only for tools you would trust with your own shell.

Doing better

In rough order of strength:

  1. Don’t mount it. Many tools have alternatives: Traefik can read routes from a file provider, and image updates can be done by your CI pipeline instead of an updater container on the host.
  2. Rootless Docker. If the daemon runs as an ordinary user, socket access gives that user’s power, not root’s. See running as non-root and rootless Docker.
  3. A filtering proxy. Run a small proxy that holds the real socket and forwards only allowed requests, for example GET on /containers and /events. The tool gets the proxy’s address instead. Be strict: allowing POST on /containers lets a client create a container again, which puts you back at root.
  4. An AuthZ plugin. The daemon passes every HTTP API request to it for an allow or deny decision. The threat model notes plugins see only JSON bodies and what they restrict is up to them.

Remote access

The daemon can also listen on TCP. On port 2375 without TLS, anyone who can reach the port has root, and scanners look for exactly that on the internet. If you need a TCP listener, Docker’s “Protect the Docker daemon socket” page covers TLS with client certificates on 2376. The simpler route is SSH: a context with host=ssh://user@server tunnels the API over an SSH connection you already secure. That is the subject of Docker contexts and remote engines.

About the video

The embed is all of Better Stack’s “Docker Containers - How Hackers Can ESCAPE Them in Seconds!”, March 2025, 5 minutes. The chapters are “Docker Socket Vulnerability Walkthrough” and “The Dangers of Privileged Mode”, and the description says both are demonstrated on Docker Desktop so you can repeat them. It is the attack shown live. It doesn’t cover the docker group, the read-only myth, socket proxies or rootless mode, which the lesson adds. YouTube reports no audio language, so English was judged from the channel and English captions. John Hammond’s video in deeper walks the docker-group escalation step by step, in a 2020 CTF setting.

What was run: the API paths and the Binds, Privileged and PidMode fields were checked against Moby’s current OpenAPI file (API 1.57). The requests were not sent, since no daemon was available. The statements about trust are quoted from Moby’s threat model and Docker’s documentation, read 2026-11-28.

Where this fits

Closing the socket stops the easy path to root. Next, take root away from the container itself: running as non-root and rootless Docker.

Check yourself

  1. 01A monitoring container gets -v /var/run/docker.sock:/var/run/docker.sock:ro. Why doesn't :ro make it safe?

    The read-only flag applies to the socket file on the filesystem. Connecting to a socket and sending requests isn't a write to the file, so the container can still call any API endpoint, including container create.

  2. 02Why is adding a developer to the docker group equivalent to giving them sudo?

    The group grants access to the daemon's socket, and through it they can start a container that mounts / from the host and run as root inside it, editing any host file. No password or audit trail is involved.

  3. 03A tool only needs to list containers and read their labels. What is a safer way to give it that than the raw socket?

    Put a socket proxy in front of the daemon that allows only GET requests to the containers endpoints, and give the tool the proxy's address instead of the socket. Container create and exec stay blocked.

  4. 04You find dockerd started with -H tcp://0.0.0.0:2375 on a cloud VM. What is the risk, and the fix?

    Anyone who can reach port 2375 controls the daemon without authentication, so effectively has root on the VM. Remove the TCP listener, or require TLS client certificates on 2376, and use SSH contexts for remote use.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark