Running as non-root and rootless Docker

Advanced · 4 min

Three ways to take root away: a non-root USER in the image, user-namespace remapping, and rootless Docker, where even the daemon is an ordinary user.

Before thisContainer isolation and its limitsFile ownership and permissions in mounts

Hack the Clown · 4 min

Running as non-root and rootless Docker

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Hack the ClownReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Without a user namespace, UID 0 in a container is UID 0 on the host. Most official images, including nginx, node, python, postgres and redis, start as root.
  • Set a non-root `USER` in your Dockerfile, with a fixed numeric UID, and copy files in with `COPY --chown`. `docker run --user` overrides it at run time.
  • Docker sets `net.ipv4.ip_unprivileged_port_start=0` in containers with their own network namespace, so a non-root process can bind port 80 without any capability.
  • userns-remap keeps a root daemon but maps container UIDs to a range of unused host UIDs from /etc/subuid, so container root has no power on the host.
  • Rootless mode runs the daemon and containers as your user inside a user namespace. Container root maps to your UID and other UIDs to your subordinate range.
  • Rootless mode trades features for safety: resource limits need cgroup v2 with systemd, AppArmor and overlay networks are unsupported, and ports below 1024 need extra setup.
  • These layers stack. A non-root USER inside a rootless daemon means an escape lands as an unprivileged UID owned by an unprivileged user.

Root inside is root outside

The isolation lesson listed what Docker puts around a container by default and one thing it leaves out: a user namespace. Without one, UID 0 in the container is UID 0 on the host. Docker drops most of its capabilities and filters its system calls, but any gap in those layers, a host directory mounted in, a kernel bug, a careless --cap-add, is a gap for real root.

There are three ways to fix that, and they stack.

Approach Container process Daemon Changed by
Non-root USER a normal UID root the image author
userns-remap root, mapped to a high host UID root the host admin
Rootless mode root, mapped to your UID your user the host admin or you

Level 1: a non-root user in the image

Check what an image runs as before you trust it. I pulled image configs from Docker Hub’s registry API. nginx, node:24, python:3.14-slim, postgres:18 and redis:8 all have an empty User field, which means root. postgres and redis are better than that looks: their entrypoint scripts start as root to fix directory ownership, then re-execute themselves as an unprivileged user. In postgres:18’s docker-entrypoint.sh the line is exec gosu postgres "$BASH_SOURCE" "$@". Google’s distroless images make the choice visible in the tag: gcr.io/distroless/static-debian13:nonroot has User: 65532, and :latest has User: 0.

For your own images:

FROM python:3.14-slim
RUN groupadd --system --gid 10001 app \
 && useradd --system --uid 10001 --gid app --no-create-home app
WORKDIR /app
COPY --chown=app:app . .
RUN pip install --no-cache-dir -r requirements.txt
USER 10001:10001
CMD ["gunicorn", "-b", "0.0.0.0:80", "app:wsgi"]

Three details matter. Use a numeric UID in USER, so tools can tell it isn’t 0 without reading /etc/passwd, and pick one unlikely to collide with real host users. Put USER after the steps that need root, such as installing packages. And use COPY --chown, because files copied without it belong to root and the app can’t write them.

That last point turns into the classic volume problem: the app runs as 10001, the mounted directory belongs to someone else. File ownership and permissions in mounts covers it.

Port 80 without root

Old advice says non-root processes can’t bind ports below 1024. In a Docker container they can. Moby’s daemon/oci_linux.go sets net.ipv4.ip_unprivileged_port_start=0 for every container with its own network namespace, with the comment “allow opening any port less than 1024 without CAP_NET_BIND_SERVICE”. So the Dockerfile above can bind port 80 as UID 10001.

docker run --user 10001:10001 image overrides USER at run time, which is useful for images that work as any user but don’t set one.

Level 2: userns-remap

Some images must run as root inside. userns-remap makes that root harmless on the host. In /etc/docker/daemon.json:

{ "userns-remap": "default" }

With default, Docker creates a dockremap user and maps container UIDs 0 to 65535 onto that user’s range in /etc/subuid. Container root becomes something like host UID 231072, which owns nothing and has no privileges outside the namespace.

The costs, from Docker’s docs: it is daemon-wide, it hides existing images and containers because they live under a new storage directory, and bind-mounted host files need ownership that matches the shifted UIDs. A single container can opt out with --userns=host, which also opts it out of the protection. The daemon itself still runs as root, so socket access is still root.

Level 3: rootless mode

Rootless mode runs dockerd, containerd and the containers as an ordinary user, inside a user namespace created by RootlessKit. Escaping the container, or even compromising the daemon, gives an attacker your user, not root.

The prerequisites are newuidmap and newgidmap (the uidmap package) and at least 65,536 subordinate IDs for your user in /etc/subuid and /etc/subgid. Setup is one script:

dockerd-rootless-setuptool.sh install
export DOCKER_HOST=unix://$XDG_RUNTIME_DIR/docker.sock
systemctl --user enable --now docker

The mapping differs from userns-remap. Docker’s UID/GID mapping page says that in rootless mode container UID 0 maps to your UID, and container UID n maps to subuid + (n - 1). So files your host user owns appear as root-owned inside the container, which makes bind mounts of your own project directory behave nicely.

The limitations, from Docker’s rootless pages:

  • Resource limits (--memory, --cpus, --pids-limit) work only with cgroup v2 and systemd. Otherwise they are silently ignored and docker info shows Cgroup Driver: none.
  • AppArmor, checkpointing, overlay networks and SCTP ports are not supported.
  • Ports below 1024 on the host need CAP_NET_BIND_SERVICE on the rootlesskit binary, or a sysctl change.
  • --cap-add only grants powers over resources inside the user namespace.

Networking changed this year. Engine 29.5 made gvisor-tap-vsock the default rootless network driver, and Docker’s packages no longer install slirp4netns. The same release made --net=host behave properly in rootless mode. If you follow an older rootless guide, expect those parts to differ.

Which to use

Always do level 1 for images you build. It costs a few lines and removes root from the common case. Add rootless mode on developer machines and CI hosts where people run untrusted builds. userns-remap fits servers where you can’t change the images and can accept its daemon-wide effects. Podman gives you rootless containers by default, compared in Docker alternatives.

About the video

The embed is all of Hack the Clown’s “The Concept Behind Rootless Docker”, September 2026, 4 minutes 7 seconds. The description frames the problem exactly as this lesson does, a root daemon plus a user in the docker group, and says it shows how rootless mode changes that “using Linux user namespaces and subordinate UIDs”. The chapters are “Rootless Docker 101”, “Docker fail” and “subuid”. It is short and conceptual. The USER instruction, userns-remap and the current limitations come from the lesson. English is confirmed by YouTube’s “English (US) original” audio flag. Liz Rice’s talk in deeper builds user namespaces from scratch in Go, for the mechanism underneath.

What was run: image configs for nginx, node, python, postgres, redis and two distroless variants were pulled from their registries, and the postgres:18 entrypoint script was extracted from its layer and read. Moby’s source was read for the unprivileged-port sysctl. Nothing was run in a container, since no daemon was available. Rootless and userns-remap details are from Docker’s documentation and the Engine 29 release notes, checked 2026-11-28.

Where this fits

A non-root user still has 14 capabilities and hundreds of system calls available. Next, trim those: capabilities, seccomp and read-only containers.

Check yourself

  1. 01Your image has no USER line and the app has a remote code execution bug. With default Docker, what UID does the attacker's shell have on the host?

    UID 0. There is no user namespace by default, so container root is host root, limited only by dropped capabilities, seccomp and the LSM.

  2. 02Why use USER 10001 rather than USER app in a Dockerfile?

    A numeric UID can be checked without reading the image's /etc/passwd, so platforms such as Kubernetes can verify the container isn't root, and it avoids clashing with UIDs that already exist on the host or in the base image.

  3. 03In rootless mode your host UID is 1000 and /etc/subuid gives you 231072:65536. What host UID does container UID 33 (www-data) get?

    231104. In rootless mode container UID 0 is 1000, and container UID n for n of 1 or more maps to 231072 + (n - 1).

  4. 04A rootless container ignores --memory 512m. What is the likely cause?

    The host isn't using cgroup v2 with systemd as the cgroup driver (docker info shows Cgroup Driver: none). Rootless mode then ignores cgroup-related flags.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark