Difficulty: Advanced
What is user namespace remapping in Docker, and how does it protect the host if a container is compromised?
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.
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.
"userns-remap": "default" in /etc/docker/daemon.json and restart the daemon.dockremap and reads its subordinate ranges from /etc/subuid and /etc/subgid, for example dockremap:100000:65536.0-65535 map to host UIDs 100000-165535.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).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.
--userns=host, which you need for --privileged workloads./var/lib/docker/100000.100000, so after enabling it your existing images seem to vanish and must be pulled again.chown the directory to the mapped UID or use a volume.--pid=host and --network=host and some volume or storage plugins do not combine cleanly with remapping.| Approach | What runs unprivileged | Protects against |
|---|---|---|
USER 1001 in Dockerfile | The app process | App bugs gaining root inside the container |
userns-remap | Container processes, as seen by the host | Container escape gaining host root |
| Rootless Docker | The daemon and all containers | A 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.
# /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.
# 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.
User Namespaces, userns-remap, subuid/subgid, Container Escape, Rootless Docker