User Namespaces

Difficulty: Advanced

Question

What is user namespace remapping in Docker, and how does it protect the host if a container is compromised?

Answer

User namespace remapping (userns-remap) makes the root user inside a container map to an unprivileged, high-numbered UID on the host. A process that is UID 0 in the container is really something like UID 100000 on the host, so if it breaks out of the container it lands as a nobody with no rights over host files, devices or other users' processes.

Why container root is dangerous by default

Containers are just Linux processes isolated with namespaces and cgroups. Without user namespaces, the UID inside the container is the same number on the host kernel. Root in the container is UID 0 on the host, with its capabilities trimmed by Docker's default capability set, seccomp profile and AppArmor/SELinux. That is a lot of protection, but every one of those layers assumes the kernel and runtime have no exploitable bug. Past CVEs such as the 2019 runc overwrite bug (CVE-2019-5736) let a malicious container root rewrite the host's runc binary. User namespaces add a layer where, even after an escape, the attacker holds no real privileges.

How the UID mapping works

  1. You set "userns-remap": "default" in /etc/docker/daemon.json and restart the daemon.
  2. Docker creates a user called dockremap and reads its subordinate ranges from /etc/subuid and /etc/subgid, for example dockremap:100000:65536.
  3. Every container now runs in a user namespace where container UIDs 0-65535 map to host UIDs 100000-165535.
  4. Inside the container, id still prints uid=0(root), and root can still do root things within its own namespace (install packages, bind low ports, chown its own files).
  5. On the host, ps shows the process as UID 100000, and files it creates on bind mounts are owned by 100000.

You can also name your own user ("userns-remap": "appuser") so you control the range.

Trade-offs and gotchas

  • Per-daemon, not per-container. Remapping is switched on for the whole daemon. You can opt a single container out with --userns=host, which you need for --privileged workloads.
  • Separate storage. Docker keeps images and containers for a remapped daemon under a new directory such as /var/lib/docker/100000.100000, so after enabling it your existing images seem to vanish and must be pulled again.
  • Bind mount ownership. A host directory owned by UID 1000 is not writable by container root, because container root is host UID 100000. You have to chown the directory to the mapped UID or use a volume.
  • Host namespace sharing breaks. Flags like --pid=host and --network=host and some volume or storage plugins do not combine cleanly with remapping.

userns-remap vs rootless mode vs USER

ApproachWhat runs unprivilegedProtects against
USER 1001 in DockerfileThe app processApp bugs gaining root inside the container
userns-remapContainer processes, as seen by the hostContainer escape gaining host root
Rootless DockerThe daemon and all containersA daemon compromise and container escape

These stack. A good production setup runs the app as a non-root USER and adds user namespaces on top. Docker Desktop's Enhanced Container Isolation and Kubernetes pods with hostUsers: false apply the same kernel feature, so the idea transfers directly to clusters.

How container root becomes an unprivileged host user. The kernel translates UIDs at the user namespace boundary, so container root never holds host root.
  1. Process runs as UID 0: Inside the container it looks and acts like root.
  2. User namespace boundary: Mapping comes from /etc/subuid and /etc/subgid.
  3. Kernel sees UID 100000: All host permission checks use this number.
  4. Escape lands unprivileged: No access to host files, devices or root processes.

Code examples

Enable remapping on the daemon

# /etc/docker/daemon.json
echo '{ "userns-remap": "default" }' | sudo tee /etc/docker/daemon.json
sudo systemctl restart docker

# Docker created the dockremap user and its subordinate ranges
grep dockremap /etc/subuid /etc/subgid

# Inside the container we are still root
docker run --rm -d --name probe alpine sleep 300
docker exec probe id

# On the host the same process belongs to a high UID
ps -o user,pid,cmd -C sleep

The container reports uid=0, but the host kernel sees UID 100000, which owns nothing outside its own namespace.

Opting one container out

# A privileged tool that genuinely needs host root
docker run --rm --userns=host --privileged alpine dmesg | head -n 3

`--userns=host` disables remapping for this container only. Use it sparingly, since it removes the protection for that workload.

Key points

  • userns-remap maps container UID 0 to a high, unprivileged host UID taken from /etc/subuid and /etc/subgid.
  • It is a daemon-wide setting; individual containers can opt out with --userns=host.
  • Enabling it moves Docker's storage to a new directory, so existing images must be pulled again.
  • Bind-mounted host directories need ownership that matches the mapped UID range.
  • It complements, not replaces, running the app as a non-root USER and rootless Docker.

Common follow-up questions

  • How is rootless Docker different from userns-remap?
  • Why can a remapped container not write to a bind-mounted directory owned by your user?
  • What other defences limit container root (capabilities, seccomp, AppArmor)?
  • How does Kubernetes support user namespaces for pods?

Interview tips

  • Explain the threat model first: the goal is to make a container escape land as an unprivileged user.
  • Mention the practical pain points (bind mount ownership, separate storage, --privileged) to show you have actually used it.
  • Draw the three-way distinction between USER, userns-remap and rootless mode; interviewers often probe exactly that.

Concepts covered

User Namespaces, userns-remap, subuid/subgid, Container Escape, Rootless Docker

More Docker & Containers questions