Guides / Containers

Run Docker containers as a non-root user

beginnerUpdated 26 Nov 20261009 words

"The default user within a container is root (uid = 0)", says the Docker documentation. Without user namespace remapping, that is the same UID 0 as root on the host, with fewer capabilities. If an attacker gets code execution in your application, they start as root inside the container: they can change every file of the image, read every mounted secret, and any escape through a kernel or runtime bug lands them as root on the host. Running the process as an ordinary user removes that head start. It is usually a two-line change.

Find out who runs your containers

The user configured for each running container:

docker ps -q | xargs -r docker inspect --format '{{.Name}} user={{.Config.User}}'

An empty user= or user=0 means root, as far as Docker knows. Some images start as root and switch to an unprivileged user in their entrypoint script; the official redis and postgres images work this way. Check what the processes run as:

docker top <container> -o user,pid,comm
USER                PID                 COMMAND
999                 187728              redis-server

docker top runs ps on the host, so it shows the name of the host user with that UID, or the number if the host does not have one. In this test with redis:8.10.2-alpine3.23, Config.User was empty but the server ran as UID 999.

Set the user in the Dockerfile

Create a user with a fixed UID and switch to it after the steps that need root (package installation, copying files):

FROM debian:bookworm-slim
RUN apt-get update \
 && apt-get install -y --no-install-recommends ca-certificates \
 && rm -rf /var/lib/apt/lists/*
RUN groupadd --gid 10001 app \
 && useradd --uid 10001 --gid app --no-create-home --shell /usr/sbin/nologin app
COPY --chown=10001:10001 ./app /app
USER 10001:10001
ENTRYPOINT ["/app/server"]

On Alpine the BusyBox tools take other flags:

FROM alpine:3.24
RUN addgroup -S -g 10001 app \
 && adduser -S -D -H -u 10001 -G app -s /sbin/nologin app
COPY --chown=10001:10001 ./app /app
USER 10001:10001
ENTRYPOINT ["/app/server"]

Details that save time:

  • Give the group too. "When the user doesn't have a primary group then the image (or the next instructions) will be run with the root group", says the Dockerfile reference.
  • Numeric IDs in USER and COPY --chown do not depend on /etc/passwd in the image. With names in COPY --chown, BuildKit looks them up there and the build fails when they are missing.
  • A fixed UID lets you prepare volumes on the host: sudo chown -R 10001:10001 /srv/app-data.
  • Files the process only reads can stay owned by root; it needs write access only where it writes (a data directory, /tmp).
  • Several official images already contain a user. The node images have node (UID 1000), so USER node is enough. Distroless images have a nonroot tag that runs as UID 65532.

Set the user without changing the image

For an image you do not build, set the user at run time:

docker run -d --user 10001:10001 -v /srv/app-data:/data example/app:1.4.2
services:
  app:
    image: example/app:1.4.2
    user: "10001:10001"
    volumes:
      - /srv/app-data:/data

The program must cope with it: files it writes in the image must be writable by that UID. If it fails with "Permission denied", look at the path in the message and fix the ownership of that volume, or mount a tmpfs for scratch files.

Ports below 1024

An unprivileged process inside a container can bind port 80. Docker sets the sysctl net.ipv4.ip_unprivileged_port_start to 0 in the network namespace of the container (the host keeps its default of 1024). In a test on Docker 29.9, a process with UID 65534 and no capabilities at all listened on port 80. You do not need NET_BIND_SERVICE for that, and the published port on the host can be anything.

Go further: read-only and without capabilities

With a non-root user in place, a few more settings cost little:

services:
  app:
    image: example/app:1.4.2
    user: "10001:10001"
    read_only: true
    tmpfs:
      - /tmp
    cap_drop: [ALL]
    security_opt:
      - no-new-privileges:true

read_only makes the file system of the image read-only, so an attacker cannot replace your code or drop tools into it. cap_drop: [ALL] removes the 14 capabilities Docker grants by default, and no-new-privileges blocks setuid programs from regaining them. In the test container, CapEff was 0000000000000000, NoNewPrivs was 1, writes to / failed with "Read-only file system" and /tmp stayed writable.

User namespace remapping

userns-remap makes the daemon map root in every container to an unprivileged UID range on the host. Root in the container is then nobody special on the host, even for images you cannot change. Enable it in /etc/docker/daemon.json:

{
  "userns-remap": "default"
}

With default, Docker creates the user dockremap and uses its ranges in /etc/subuid and /etc/subgid. Know the costs before you switch a host:

  • "Enabling userns-remap effectively masks existing image and container layers", says the documentation. Images and containers from before are not visible, and the ones created with it are not accessible after you turn it off. On Docker 29 with the containerd image store, dockerd --validate also warns that it turns off the containerd snapshotter.
  • The documentation lists --pid=host and --network=host as incompatible with it, and --privileged needs --userns=host on the container.
  • Bind-mounted files must be owned by the remapped UIDs to be writable.

For a new host, it is worth trying. On a running host, the per-container USER is less disruptive.

Check the result

  1. The inspect loop shows a non-empty, non-zero user= for your own images.
  2. docker top <container> -o user,pid,comm does not show root for the main processes.
  3. The application still writes where it should (uploads, caches, logs).

The daily check docker.root_user of Patchbrief lists the containers that run as root.

Sources

Check your servers every day

The agent of Patchbrief reports the packages and settings of a server once a day; the digest names what to fix first.

Start free