Sub topic 4
How RootlessKit puts the daemon in a user namespace
This chapter assumes you know what a user namespace is and how /etc/subuid and /etc/subgid define subordinate ID ranges. The previous chapters covered both. Here you will see the tool that applies them to the Docker daemon.
The problem RootlessKit solves
A user namespace gives a process a private view of user IDs. Creating one is easy. The hard part is that an unprivileged user can map only their own UID into it. Giving the daemon the 65536 subordinate IDs it needs for container users requires the setuid helpers newuidmap and newgidmap, and several other pieces must be set up around them.
RootlessKit is a small program that does this setup. Docker's rootless scripts use it to start dockerd.
What RootlessKit does
When dockerd-rootless.sh runs, it calls rootlesskit with dockerd as the command to run. RootlessKit then does these things:
- It creates a new user namespace and a new mount namespace for the daemon. It also creates a network namespace, unless you turn that off.
- It calls
newuidmapandnewgidmapto write the ID maps. Your own UID becomes UID 0 inside the namespace. Your subordinate range becomes UIDs 1 and up. - It sets up networking for the new network namespace, because that namespace starts with no connection to the outside.
- It starts
dockerdinside the namespaces. The daemon sees itself as root.
Looking at the result
You can confirm that the daemon lives in a different user namespace from your shell. Run this as the rootless user:
pid="$(pgrep -u "$(id -u)" -x dockerd)"
# Different values mean different user namespaces
readlink /proc/self/ns/user "/proc/$pid/ns/user"
# The ID map as seen from the daemon's own namespace
cat "/proc/$pid/uid_map"A typical uid_map looks like this:
0 1000 1
1 100000 65536Each line has three columns: the first UID inside the namespace, the matching UID outside, and the length of the range. Inside, UID 0 is host UID 1000, which is your user. Inside UIDs 1 to 65536 are host UIDs 100000 to 165535.
Networking and port forwarding
The daemon's network namespace cannot reach the host network directly, since an unprivileged user cannot create real network devices on the host. RootlessKit therefore uses two helpers:
- A network driver gives the namespace outbound connectivity. The common default is
slirp4netns, which implements a TCP/IP stack in user space. Other drivers such asvpnkitandlxc-user-nicexist, and recent releases also supportpasta. - A port driver forwards connections from the host into the namespace. The default
builtindriver does this inside RootlessKit itself.
Because traffic passes through user-space code, rootless networking is slower than rootful bridge networking. The builtin port driver also does not preserve the client's source IP address, so containers see connections as coming from inside the namespace.
You can change the drivers with environment variables read by dockerd-rootless.sh, for example DOCKERD_ROOTLESS_ROOTLESSKIT_NET and DOCKERD_ROOTLESS_ROOTLESSKIT_PORT_DRIVER. Set them in the systemd user unit's environment, then restart the service.
Common errors
newuidmap: write to uid_map failed: Operation not permittedusually means thenewuidmapbinary is not setuid, or your user has no entry in/etc/subuid.- Containers have no network after startup usually means the network helper (
slirp4netnsor your chosen driver) is missing from the host.
Exercise
Why can RootlessKit create a user namespace with 65536 mapped IDs when the user is unprivileged, and what would happen if it tried to write the map directly?
Show answer
Writing a map that includes more than your own UID needs privilege. RootlessKit calls the setuid helpers newuidmap and newgidmap, which check /etc/subuid and /etc/subgid and write the map on its behalf. Writing the map directly would fail with a permission error, because an unprivileged process may map only its own UID.