How they compare: userns-remap vs Rootless Docker
Standard Docker runs containers without a separate user namespace: /proc/<pid>/uid_map reads 0 0 4294967295.
A process running as uid 0 inside a container shows up as root in ps on the host. Settings from
Hardening containers in Compose (cap_drop, read_only, no-new-privileges) strip what that process can do, but don’t
change its host UID.
A user namespace shifts the ID map: container uid 0 becomes, say, 100000 on the host, uid 1 becomes 100001,
and so on across 65,536 IDs. Inside the container the program still sees itself as root; to the host kernel it’s an
unprivileged user with no access to system files.
Docker offers two ways to enable this:
| Standard Docker | userns-remap |
Rootless Docker | |
|---|---|---|---|
Who runs dockerd |
root |
root |
regular user |
Container uid 0 on host |
0 (root) |
100000 (from /etc/subuid) |
user’s UID plus their /etc/subuid |
| Ports 80 and 443 | out of the box | out of the box | needs ip_unprivileged_port_start = 80 |
Access to docker.sock |
root on host (docker.sock) |
root on host |
privileges of that user only |
| Per-container opt-out | — | userns_mode: host |
none |
| Real client IP in container | visible | visible | sees 127.0.0.1 without extra setup (slirp4netns) |
Which one to pick:
userns-remap— for a VPS running Traefik and CrowdSec: ports 80 and 443 work immediately, real client IPs come through intact, and a host-level container like a monitoring agent can opt out withuserns_mode: host.- Rootless Docker — when
dockerditself must not run asroot(a shared dev box, or a server where an unprivileged user runs containers withoutsudo).
Always start container hardening with Hardening containers in Compose: those options don’t touch existing volumes or images.
Enable userns-remap or Rootless Docker on a fresh server before deploying stacks.
Option 1: enable userns-remap
Add userns-remap to /etc/docker/daemon.json
If
/etc/docker/daemon.jsonalready configures log limits (Docker on Ubuntu), add the line to the same JSON object:{ "userns-remap": "default" }Restart Docker and check the assigned UID range
sudo systemctl restart docker grep dockremap /etc/subuid /etc/subgid # /etc/subuid:dockremap:100000:65536 # /etc/subgid:dockremap:100000:65536Docker creates the
dockremapsystem user automatically and allocates a 65,536-ID range (the start offset is usually100000or165536— check your output). Containeruid 0now maps to host100000, anduid 33(www-data) maps to100033.Update ownership on bind-mounted host directories
Docker creates named volumes with the right owner already. Host bind mounts (
volumes: - ./data:/data) need the shifted UID:sudo chown -R 100000:100000 /opt/site/dataIf the service inside runs as non-root (say
uid 33or999), add the/etc/subuidbase offset to the process UID:100000 + 33 = 100033.Keep an exception for a host-networked container
A container with
network_mode: hostwon’t start underuserns-remap. If one service (such as a monitoring agent) needs host networking, disable remapping for that container only:services: agent: image: ghcr.io/example-user/agent:1.0 network_mode: host userns_mode: host
Option 2: Rootless Docker
Rootless Docker requires Docker from the official repository (docker-ce and docker-ce-rootless-extras, as in
Docker on Ubuntu): Ubuntu’s docker.io package doesn’t ship the rootless scripts.
Install dependencies and stop the system daemon
sudo apt install -y uidmap dbus-user-session slirp4netns docker-ce-rootless-extras sudo systemctl disable --now docker.service docker.socket sudo rm -f /var/run/docker.sockIf
/var/run/docker.sockstays accessible, the setup script aborts withAborting because rootful Docker (/var/run/docker.sock) is running and accessible.Your user needs entries in
/etc/subuidand/etc/subgid(Ubuntu creates them for new accounts; check withgrep "^$USER:" /etc/subuid /etc/subgid).Ubuntu 24.04 enables
kernel.apparmor_restrict_unprivileged_userns = 1, and its built-in/etc/apparmor.d/rootlesskitprofile already allows user namespaces for/usr/bin/rootlesskit. Installingdocker-ce-rootless-extrasviaaptrequires no AppArmor edits; you only need a custom profile if a script installedrootlesskitinto your home directory (~/bin/rootlesskit).Install the user daemon and enable linger
Run as the regular user (without
sudo) in a direct SSH login — not viasu, or the user systemd session won’t start:dockerd-rootless-setuptool.sh install sudo loginctl enable-linger "$USER"The script creates
~/.config/systemd/user/docker.service, starts the daemon, and switches the CLI to therootlesscontext.loginctl enable-lingerstarts the daemon on reboot and keeps it running after you close SSH.For tools that don’t read Docker contexts, append the user socket path to
~/.bashrc:export DOCKER_HOST=unix:///run/user/$(id -u)/docker.sockOpen ports 80 and 443 if containers need them
By default
net.ipv4.ip_unprivileged_port_startis1024, soports: - "80:80"and"443:443"fail withpermission denied. If this server runs a containerized reverse proxy, lower the threshold:echo 'net.ipv4.ip_unprivileged_port_start = 80' | sudo tee /etc/sysctl.d/99-rootless-ports.conf sudo sysctl --system
If it doesn’t work
After enabling userns-remap, all images and containers disappeared — docker images and docker ps -a are empty
- Cause
- Docker switched its data directory to
/var/lib/docker/<uid>.<gid>/(for example,/var/lib/docker/100000.100000/). The old images and containers still sit in the root of/var/lib/docker/; nothing was deleted. - Fix
- Pull images and bring stacks up in the new namespace (adjusting ownership on host data directories for the UID offset). To revert, remove
userns-remapfrom/etc/docker/daemon.jsonand restartdocker.
Container can’t write to a bind-mounted host directory: Permission denied
- Cause
- Container
uid 0maps to an unprivileged host UID (such as100000), while the directory is owned byroot:rootwith mode755. For a service running asuid 33, the host owner is100033. - Fix
- Set ownership using the offset from
/etc/subuid:sudo chown -R 100000:100000 /opt/site/data(or100000 + container_uid).
docker: Error response from daemon: cannot share the host's network namespace when user namespaces are enabled
- Cause
- Containers using
--network host(ornetwork_mode: hostin Compose) aren’t compatible with UID remapping. - Fix
- Opt that single container out of remapping:
--userns=hostindocker runoruserns_mode: hostincompose.yaml.
Rootless Docker can’t publish ports 80 and 443: cannot expose privileged port 80 ... bind: permission denied
- Cause
- The daemon runs as a regular user, and Linux blocks unprivileged processes from binding ports below
1024(net.ipv4.ip_unprivileged_port_start). - Fix
- Lower the threshold in
/etc/sysctl.d/99-rootless-ports.conf(net.ipv4.ip_unprivileged_port_start = 80) and runsudo sysctl --system.
On Ubuntu 24.04, Rootless Docker fails with Operation not permitted when creating a user namespace
- Cause
- Ubuntu 24.04 sets
kernel.apparmor_restrict_unprivileged_userns = 1. The built-in/etc/apparmor.d/rootlesskitprofile permits only/usr/bin/rootlesskit; if the binary sits elsewhere (such as~/bin/rootlesskit), AppArmor blocks it. - Fix
- Install
docker-ce-rootless-extrasfromapt(which places the binary at/usr/bin/rootlesskit), or add an/etc/apparmor.d/profile withflags=(unconfined)anduserns,for your path and runsudo systemctl reload apparmor.
How to check
For userns-remap, start a test container and compare its process inside and on the host:
docker run -d --rm --name test-userns busybox:1.37 sleep 30
docker exec test-userns id
# uid=0(root) gid=0(root)
PID=$(docker inspect -f '{{.State.Pid}}' test-userns)
ps -o pid,uid,gid,user,comm -p "$PID"
# PID UID GID USER COMMAND
# 6670 100000 100000 100000 sleep
cat /proc/"$PID"/uid_map
# 0 100000 65536
docker rm -f test-userns
Inside the container it’s uid=0, on the host it’s UID 100000, and uid_map maps 0 to 100000 across 65,536 IDs.
For Rootless Docker, verify that SecurityOptions lists rootless and both dockerd and container processes run as
the regular user (container uid 0 maps to that user’s UID, while uid 1 and up map into /etc/subuid):
docker info --format '{{.SecurityOptions}}'
# [name=seccomp,profile=builtin name=rootless name=cgroupns]
ps -eo user,uid,comm | grep -E 'dockerd|rootlesskit'
# example-user 1000 rootlesskit
# example-user 1000 dockerd