Sub topic 1
Why the Docker daemon runs as root, and why that matters
Prerequisites
This chapter assumes you have:
- Basic Docker usage: images, containers, and
docker run. - Comfort with the Linux command line.
- A working idea of Linux users, groups, and file permissions.
- Basic familiarity with systemd services.
Why the daemon runs as root
Docker has two parts. The docker command is a client. It sends requests to the Docker daemon, dockerd, which does the real work: it pulls images, creates containers, sets up networks, and mounts storage.
In a standard installation, dockerd runs as root (UID 0). It needs that privilege to do several things the Linux kernel restricts:
- Create namespaces and cgroups for containers.
- Mount filesystems, including the layered image storage.
- Create network interfaces, bridges, and firewall rules.
- Bind to any port, including ports below 1024.
- Switch to arbitrary user IDs inside containers.
This is called rootful mode. It is the default, and it works well. The cost is that one process holds full host privilege and accepts commands from anyone who can reach its socket.
What the root daemon exposes
The client talks to the daemon over a Unix socket, /var/run/docker.sock. The socket is owned by root and the docker group. Anyone in that group can send the daemon any request.
A request can ask for a container that mounts the host's root filesystem. The daemon creates it as root, so the container can read and write anything on the host.
# Run by a user in the docker group, with no sudo
docker run --rm -v /:/host alpine cat /host/etc/shadowThe command prints the host's password hashes. The user never needed sudo. For this reason, membership in the docker group is equivalent to root access on the host.
Two attack paths
Rootful mode creates two separate risks.
- Daemon access. Anyone or anything that can write to the socket gets root. This includes a compromised account in the
dockergroup. It also includes a container that has the socket mounted into it, which CI systems and management tools often do. - Container escape. Containers share the host kernel. If an attacker breaks out of a container through a kernel bug, a runtime flaw, or a misconfiguration, the escaped process can still be root on the host. That holds if the container's root user was also host root.
Rootful Docker reduces these risks with capability dropping, seccomp profiles, and AppArmor or SELinux. These are useful layers. They restrict what a root process can do, but the process is still root.
How rootless mode changes the outcome
Rootless mode runs the daemon and the containers as an ordinary user. If an attacker takes over the daemon or escapes a container, they gain that user's privileges, not host root. They can read that user's files, but they cannot edit /etc, install kernel modules, or read other users' data.
| Row | Rootful | Rootless |
|---|---|---|
| Daemon runs as | root | an unprivileged user |
| Root in a container maps to | host root | an unprivileged host user |
| Result of daemon compromise | full host control | limited to that user |
| Socket access grants | host root | that user's privileges |
Rootless mode does not stop container escapes. It limits what an escape is worth.
The next chapters explain the kernel feature behind this, user namespaces, and the tools that put the daemon inside one.
Exercise
A teammate says adding a developer to the docker group is safe because the developer does not have sudo. What is wrong with that reasoning, and what in rootless mode changes the situation?
Show answer
The daemon runs as root and does whatever a client asks. A group member can start a container that mounts / from the host and read or change any file as root, so the group grants root-equivalent access. In rootless mode the daemon runs as an unprivileged user, so the same request only reaches files that user can already access.