Guides / Containers

Docker socket security: docker.sock mounts, the docker group and the TCP API

intermediateUpdated 26 Nov 20261169 words

The Docker daemon runs as root and does what any client of its API asks, including starting a container with the root directory of the host mounted. Whoever can talk to the API can act as root on the host. The API listens on the Unix socket /var/run/docker.sock, open to members of the docker group, and on a TCP port if you configured one.

Why API access equals root

One command is enough to read or change any file of the host:

docker run --rm -it -v /:/host alpine:3.24 chroot /host sh

The container gets the root directory of the host, and chroot turns the shell into a root shell on the host file system. The Docker documentation says "only trusted users should be allowed to control your Docker daemon", and on the post-installation page: "The docker group grants root-level privileges to the user."

The socket and the docker group

On Linux the API listens on a Unix socket owned by root and the group docker:

ls -l /var/run/docker.sock
srw-rw---- 1 root docker 0 Oct 31 06:41 /var/run/docker.sock

Every member of docker can use it without sudo. List them:

getent group docker

Treat this list like the list of sudo users. Remove accounts that do not need it with sudo gpasswd -d alice docker (the change applies at the next login of that user). Deploy users of CI systems often end up in this group, and if a CI secret leaks, the attacker gets root on that host.

Containers that mount the socket

Reverse proxies with automatic configuration, dashboards, update tools and CI runners often ask you to mount the socket:

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

:ro makes the socket file read-only, but connecting to the socket and sending requests still works. In a test on Docker 29.9, a container with the socket mounted :ro received OK from the API:

docker run --rm --user 0 -v /var/run/docker.sock:/var/run/docker.sock:ro \
  curlimages/curl:8.11.1 -s --unix-socket /var/run/docker.sock http://localhost/_ping

A bug in such a container, or a malicious update of its image, gives the attacker the whole host. Find the containers that have the socket:

docker ps -q | xargs -r docker inspect --format \
  '{{.Name}}{{range .Mounts}}{{if eq .Destination "/var/run/docker.sock" "/run/docker.sock"}} has the socket{{end}}{{end}}' \
  | grep 'has the socket'

Tools that can do without it

Many tools can read their configuration from a file. A reverse proxy can read its routes from a file instead of the labels of other containers.

Tools that only read

Put a filtering proxy between the tool and the socket. The docker-socket-proxy project runs HAProxy in front of the socket and answers 403 to every API section you did not enable. With POST=0 only GET and HEAD pass, so the API becomes read-only. Put the proxy and the tool on their own internal network and never publish its port.

Tools that write

A tool that starts containers or pulls images has root on the host whatever you do. Keep its image pinned and updated, and run it only on hosts where that is acceptable, such as a dedicated CI runner host.

The Docker API on TCP

The daemon can also listen on TCP, with -H tcp://0.0.0.0:2375 in the service unit or a hosts entry in /etc/docker/daemon.json. Without TLS client certificates, anyone who reaches that port is root on the host, with one HTTP request.

Check how the daemon is started:

ps -o args= -C dockerd
systemctl cat docker.service | grep -E '^ExecStart'
grep -s hosts /etc/docker/daemon.json
sudo ss -tlnp | grep dockerd

The docker-ce package of Docker Engine starts it as /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock (seen on Debian 13 with Docker 29.9), which means the socket only. A tcp:// in any of these outputs, or a line from ss with dockerd, is a TCP listener.

Docker deprecated TCP without TLS in Engine 26.0. The list of deprecated features says TLS will be required for every TCP address except tcp://localhost and names 28.0 as the target release, but Docker Engine 29.9.2 still starts with a plain TCP listener. In a test, dockerd -H tcp://0.0.0.0:2375 logged a deprecation notice, delayed its start by 16 seconds and then answered API requests without authentication. With --tls=false, or on 127.0.0.1, the delay was 1 second.

Remove the TCP listener

  1. Delete the -H tcp://... part from the drop-in that added it, usually /etc/systemd/system/docker.service.d/override.conf, or the tcp:// entry from hosts in daemon.json.
  2. Reload and restart: sudo systemctl daemon-reload && sudo systemctl restart docker.
  3. Check: sudo ss -tlnp | grep dockerd prints nothing.

Use SSH for remote access

The Docker CLI can reach a remote daemon over SSH, with the keys and the firewall rules you already have:

docker context create prod --docker host=ssh://deploy@203.0.113.10
docker --context prod ps

or for a single command DOCKER_HOST=ssh://deploy@203.0.113.10 docker ps. The remote user needs access to the socket on that host, which again means root-level rights there.

If you need TCP

Use TLS with client certificates on port 2376 (--tlsverify --tlscacert --tlscert --tlskey), as described in "Protect the Docker daemon socket" in the Docker documentation, and allow the port in the firewall only from the addresses that need it. Anyone who holds a client certificate is root on the host, so keep those keys like root SSH keys.

Rootless Docker

Rootless mode runs the daemon and the containers as an ordinary user. An attacker who gets the API then has the rights of that user, not root. Each user has a separate daemon with its own data directory (~/.local/share/docker), so images and containers are not shared. Ports below 1024 need extra setup (CAP_NET_BIND_SERVICE on rootlesskit, or the sysctl net.ipv4.ip_unprivileged_port_start), and dockerd-rootless-setuptool.sh install needs the uidmap package and subordinate ID ranges for the user. Try it on single-purpose hosts such as CI runners.

Check the result

  1. getent group docker lists only people and services that should be root on the host.
  2. The docker inspect loop above prints nothing, or only containers you decided to accept.
  3. sudo ss -tlnp | grep dockerd prints nothing, or a TLS port that only known addresses can reach.

The daily checks docker.socket_mount and net.docker_api_tcp of Vulnpocalypse report both cases on every server.

Sources

Check your servers every day

The Vulnpocalypse agent reports the packages and settings of a server once a day, and the digest lists what to fix, most urgent first.

Start free