Skip to content

Docker user namespaces — remap container root to a host UID

Enable user namespace remapping to keep container root from being host root. The daemon config, the trade-offs around volumes, and when to use rootless Docker instead.

Inside a Docker container, the default uid 0 maps to uid 0 on the host. If a container with --user 0 escapes the namespace boundary (a kernel exploit, a volume mount with wrong permissions, the docker socket), the attacker has actual root on the host. User namespace remapping breaks this — container uid 0 maps to a high, unprivileged host uid like 100000. A container “root” can do nothing as host root unless a separate escalation is in play.

How to verify

Check whether the daemon has userns-remap configured and what the mapping looks like:

docker info | grep -iE "userns|user namespace"
cat /etc/docker/daemon.json | jq '."userns-remap"'
cat /etc/subuid /etc/subgid

# What uid does a "root" container actually run as on the host?
docker run --rm -d --name test alpine sleep 60
ps -eo uid,pid,comm | grep sleep
docker rm -f test

If docker info reports userns: <none>, remapping is off and container root is host root. If subuid/subgid show a range for a dockremap user (or whichever name you configured), remapping is set up but you still have to confirm the daemon was restarted to pick it up.

What’s happening

A Linux user namespace lets a process see a different uid mapping than the rest of the system. When Docker is configured for userns-remap, it creates a per-daemon namespace where container uid 0 maps to some high uid on the host (e.g. 100000). Files inside the container owned by uid 0 are owned by uid 100000 from the host’s perspective. The container’s root user has full power inside the namespace but no power at all outside it.

The kernel uses two configuration files — /etc/subuid and /etc/subgid — to declare which uid/gid ranges a given user is allowed to map. Each user gets a contiguous range (typically 65536 uids/gids). Docker reads the range allocated to the dockremap user (or whatever you configure) and uses it to set up the remapping.

The trade-offs are real: volumes mounted from the host get permission complications (files owned by your host user are owned by some nobody from the container’s view), some kernel features and capabilities don’t cross namespaces cleanly, and the daemon can no longer share images across remapped/non-remapped configurations — Docker rebuilds the image store under a remapped directory.

The procedure

  1. Configure the subuid/subgid ranges. Docker can auto-create a dockremap user, or you can use any existing user with a range:
# Let Docker manage the dockremap user
sudo tee -a /etc/docker/daemon.json << 'EOF'
{
  "userns-remap": "default"
}
EOF

# Or explicitly use a named user
sudo useradd --system --shell /sbin/nologin --no-create-home dockermap
# Add a uid range — 65536 ids starting at 100000
echo "dockermap:100000:65536" | sudo tee -a /etc/subuid
echo "dockermap:100000:65536" | sudo tee -a /etc/subgid
sudo tee /etc/docker/daemon.json << 'EOF'
{
  "userns-remap": "dockermap"
}
EOF

The "default" keyword tells Docker to create and use a dockremap user automatically — easiest if you don’t have other constraints.

  1. Stop the daemon, move the existing storage out of the way (it must be rebuilt under the remapped layout), and restart:
sudo systemctl stop docker
# IMPORTANT: existing images/containers will not be visible under remap.
# Either accept the loss for this dev host, or back up first.
sudo systemctl start docker
docker info | grep -i userns   # should now show your remap user
  1. Verify the remapping is active:
docker run --rm -d --name probe alpine sleep 300
ps -eo uid,pid,comm | grep sleep
# Expected: uid is 100000 (or whatever your range starts at), not 0
ls -ln /var/lib/docker/100000.100000/containers
# Expected: directory exists, owned by uid 100000
docker rm -f probe

Docker creates a directory like /var/lib/docker/<host-uid>.<host-gid>/ and stores the remapped daemon’s images, containers, and volumes under it. The default /var/lib/docker/ (without the uid prefix) is left empty.

  1. Working with bind mounts under userns-remap. The container sees the file owners differently than the host:
# Host file owned by you (uid 1000)
ls -ln /home/me/config.json
# -rw-r--r-- 1 1000 1000 ... config.json

# Container mounts it; container sees uid 1000 as "nobody"
docker run --rm -v /home/me/config.json:/config.json alpine \
  ls -ln /config.json
# -rw-r--r-- 1 nobody nobody ... /config.json

The container can read this file (because the host permissions are world-readable), but if the container needs to write to the bind mount, host permissions need to allow uid 100000 (the remapped container root) to write. Two patterns:

  • chown the host path to the remapped UID range before starting the container: sudo chown 100000:100000 /var/data/app
  • use a named volume instead of a bind mount — volumes are managed by the daemon and don’t have this collision.
  1. Some configurations don’t work under userns-remap:
    • --network host — incompatible with userns remapping.
    • --privileged — partially incompatible; many capabilities don’t cross the namespace.
    • --userns host (per-container) — opts a single container out of remapping. Useful for the docker socket sidecar that legitimately needs host UID.
    • Some --cap-add flags don’t apply across namespaces.

If you depend on any of these, userns-remap may not be a fit — consider rootless Docker instead (different model, different trade-offs).

  1. Rootless Docker is the modern alternative: the daemon itself runs as an unprivileged user. There’s no host-level root involved at all. Setup is more involved (cgroup v2 required, may need slirp4netns or RootlessKit for networking, some advanced features unavailable):
# Per-user install of rootless dockerd
curl -fsSL https://get.docker.com/rootless | sh
systemctl --user enable docker
systemctl --user start docker
export DOCKER_HOST=unix:///run/user/$(id -u)/docker.sock

The whole daemon plus all containers run as your user. Compromise of a container can at worst escalate to your user, not root.

Common pitfalls

  • Forgetting that turning on userns-remap effectively hides all existing images and containers — they’re under /var/lib/docker/ (non-remapped path) while the new daemon is under /var/lib/docker/100000.100000/. Plan a re-pull.
  • Bind-mounting a host path with no chown and watching the container fail to write because the remapped UID has no permissions. Either chown to the range or use a named volume.
  • Combining --network host or --privileged with userns-remap and getting confusing failures — those modes opt out of namespace remapping.
  • Trying to share a single daemon between remapped and non-remapped containers — Docker doesn’t do this. Use per-host configuration; for mixed workloads, use rootless on the host that needs unremapped containers.
  • Treating userns-remap as security against everything. It blocks container-uid-0 → host-uid-0 escalation. It does not block a process inside the container from doing anything else inside the container, including reading mounted secrets or hitting the network.

For most production environments where containers should never have host root, we recommend userns-remap (or rootless Docker if the workloads tolerate it) as part of the host hardening baseline. It’s one of the discrete controls our managed operations checklist verifies before a host goes into the live rotation. For the broader container-security context, see Docker image security scanning.