Guides / Containers

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

intermediateUpdated 26 Nov 20261109 words

The Docker daemon runs as root and does what any client on its API asks: start a container, mount a host directory, change the network. Whoever can talk to the API can therefore act as root on the host. The API is reachable through the Unix socket /var/run/docker.sock, which members of the docker group can use, and through a TCP listener if you configured one. A few commands find both on a server, and SSH, a filtering proxy or rootless Docker replace most uses.

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 puts it this way: "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; 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

The :ro does not help. It makes the socket file read-only, but connecting to a 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'

For each of them, decide:

  • Does it need the socket at all? Many tools offer a file-based configuration. A reverse proxy can read its routes from a file instead of the labels of other containers.
  • Does it only read? Then 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.
  • Does it need to write (start containers, pull images)? Then it 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.

Since Docker Engine 27.0, the daemon requires TLS verification for every TCP address except tcp://localhost, according to the list of deprecated features. Older versions accept plain TCP.

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 TCP is unavoidable

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. For a single-purpose host, such as a CI runner, it is worth testing.

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 Patchbrief report both cases on every server.

Sources

Check your servers every day

The agent of Patchbrief reports the packages and settings of a server once a day; the digest names what to fix first.

Start free