Sub topic 12
Protecting the Docker Daemon and Socket; Rootless Mode and User Namespaces
This chapter assumes you can build and run containers, use the Linux command line, and understand Linux users, file permissions, and processes. It also assumes you know that containers share the host kernel, which earlier chapters covered.
Why the daemon is the real target
The Docker daemon (dockerd) runs as root on the host. The docker command is only a client that sends requests to the daemon through a Unix socket, /var/run/docker.sock. Anyone who can write to that socket can ask the daemon to start any container they like.
That means access to the socket is equal to root on the host. A user can run this and get a root shell on the host filesystem:
docker run --rm -it -v /:/host alpine chroot /host shTwo habits follow from this:
- Treat membership in the
dockergroup as equal to passwordless root. Add only trusted administrators to it. - Never mount
/var/run/docker.sockinto a container unless the container is meant to control the host. A compromised container with the socket can create a privileged sibling container and escape.
Never expose the TCP API without TLS
The daemon can also listen on a TCP port. An unprotected port such as 2375 gives root to anyone who can reach it, and internet scanners look for it constantly. If you need remote access, use one of these:
- SSH, which needs no daemon changes:
docker -H ssh://user@host ps. - TLS with mutual authentication, using
--tlsverifywith a CA, a server certificate, and client certificates. Port 2376 is the conventional port.
Restrict the port with a firewall as well.
Reducing what a daemon compromise costs
Two features lower the impact of a container breakout. They solve different problems.
User namespace remapping
With user namespace remapping, container root (UID 0) maps to an unprivileged UID range on the host. If a process escapes, it holds no host privileges. The daemon still runs as root. Enable it in /etc/docker/daemon.json:
{
"userns-remap": "default",
"no-new-privileges": true
}The default value makes Docker create a dockremap user and add matching ranges to /etc/subuid and /etc/subgid. Restart the daemon after the change. Docker then keeps images and containers in a separate storage directory for that mapping, so existing images appear to vanish until you pull them again.
The no-new-privileges setting makes it the default for new containers, so setuid binaries cannot raise privileges.
Remapping has limits. It is incompatible with --privileged, with --pid=host, and with --network=host unless you opt out per container using --userns=host. Bind mounts need their host ownership to match the mapped range.
Rootless mode
Rootless mode runs the daemon itself, and the containers, as an unprivileged user. A daemon compromise then yields only that user's rights. Install and start it with:
dockerd-rootless-setuptool.sh install
systemctl --user start docker
export DOCKER_HOST=unix://$XDG_RUNTIME_DIR/docker.sock
docker info --format '{{.SecurityOptions}}'The last command should list rootless. The socket now lives under your user's runtime directory, so it grants only your own privileges.
Rootless mode has trade-offs:
- Ports below 1024 need extra configuration, such as setting
net.ipv4.ip_unprivileged_port_start. - Networking runs through a user-space stack, which is slower than the normal bridge.
- Some features, such as certain storage drivers and AppArmor, are limited.
Choosing between them
| Aspect | userns-remap | Rootless mode |
|---|---|---|
| Daemon runs as | root | unprivileged user |
| Container root maps to | unprivileged host range | unprivileged host user |
| Protects against daemon compromise | No | Yes |
| Setup effort | Low | Moderate |
| Feature limits | Some modes disabled | More limits, including ports and networking |
Choose rootless mode when the workload tolerates its limits. Use remapping when you must keep a root daemon but want container root to be unprivileged.
Exercise
A teammate mounts /var/run/docker.sock into a monitoring container with the :ro flag and says this makes it safe. Is this correct, and what should they use instead?
Show answer
No. The :ro flag only blocks changes to the socket file. The container can still send any API call, such as creating a privileged container that mounts the host filesystem, so it effectively has root on the host. They should avoid mounting the socket, or use a socket proxy that allows only specific read-only API calls, or run the daemon in rootless mode so the socket grants only an unprivileged user's rights.
Exercise
You enable userns-remap and restart the daemon. Your images are no longer listed. Why, and is the data lost?
Show answer
Docker stores data for a remapped daemon in a separate directory named after the mapped UID and GID range. The old data is still in the original directory and returns if you disable remapping. With remapping on, you must pull or rebuild the images.