Sub topic 2
User namespaces: root inside, unprivileged outside
This chapter explains the Linux feature that makes rootless Docker possible. It assumes you can run basic Docker commands, use the Linux command line, and understand users, groups, and file permissions.
What a user namespace is
A namespace is a Linux kernel feature that gives a process its own view of a system resource. A user namespace does this for user and group IDs. Inside the namespace, a process can see itself as UID 0 (root). The kernel keeps a mapping between that inside UID and a different UID outside.
Permission checks on the host use the outside UID. A process that is root inside a user namespace, but maps to an ordinary user outside, has no special rights over host files or other users' processes.
The ID mapping
Each user namespace has a map that translates ranges of IDs. A map entry has three numbers: the first ID inside, the first ID outside, and the length of the range.
| Inside the namespace | Outside, on the host | Meaning |
|---|---|---|
| 0 | 1000 (your user) | Container root is you |
| 1 to 65535 | 100000 to 165534 | Other container users get subordinate IDs |
The first row maps container root to your own account. The second row uses a range of extra IDs that the system administrator reserved for you. The next chapter covers those ranges.
Seeing it work
You can observe a user namespace without Docker. The unshare tool creates one, and --map-root-user maps your UID to 0 inside it.
id -u
unshare --user --map-root-user bash -c 'id -u; cat /proc/self/uid_map'On a host where your UID is 1000, the first command prints 1000. The second prints 0, followed by a map line similar to 0 1000 1. You are root inside, but the kernel still treats the process as UID 1000.
Now try an action that needs real root:
unshare --user --map-root-user bash -c 'cat /etc/shadow'This fails with "Permission denied". The file belongs to host root, and the process is not host root.
Why this matters for Docker
With a rootful daemon, root in a container is root on the host, and the only barriers are capabilities, seccomp, and similar filters. If an attacker escapes the container, they hold host root.
In rootless mode, the daemon and its containers all run inside a user namespace owned by your unprivileged user. Container root maps to you. An escape lands the attacker in your account, not in host root. That is the reduction in risk the whole tutorial builds on.
You can confirm it once rootless Docker is running. The container reports UID 0, while the host shows its process under your own user:
docker run -d --name idtest alpine sleep 300
docker exec idtest id -u
ps -o user=,pid=,cmd= -C sleep
docker rm -f idtestCommon misunderstandings
- Root inside is not powerless. Inside the namespace, the process can still mount filesystems it owns, change IDs within the map, and bind to addresses in its own network namespace. These powers stop at the namespace boundary.
- Capabilities are scoped. A process gets capabilities such as
CAP_NET_ADMINonly over resources owned by its namespace. It cannot use them to change host networking. - Files appear owned by root. Files you own on the host look root-owned inside a container. This is the mapping at work, not a permissions bug.
- The kernel must allow it. Some distributions restrict unprivileged user namespaces through sysctl settings or security modules. Chapter on host requirements covers the checks.
Exercise
A container process runs as UID 0 inside a rootless Docker container. Your host user has UID 1000. Which UID does the host kernel use when deciding whether that process may read /etc/shadow, and what is the result?
Show answer
The kernel uses UID 1000, the outside UID that container root maps to. Because that UID has no permission to read /etc/shadow, the read is denied.