Run Docker containers as a non-root user
"The default user within a container is root (uid = 0)", according to the Docker documentation. Without user
namespace remapping, that is the same UID 0 as root on the host, with fewer capabilities. An attacker who gets
code execution in your application starts as root inside the container: they can change every file of the
image and read every mounted secret, and an escape through a kernel or runtime bug lands them as root on the
host. In most images, running as an ordinary user is 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"]
Notes:
- Give the group too. From the Dockerfile reference: "When the user doesn't have a primary group then the
image (or the next instructions) will be run with the
rootgroup". - Numeric IDs in
USERandCOPY --chowndo not depend on/etc/passwdin the image. With names inCOPY --chown, BuildKit looks them up there and the build fails when they are missing. - With a fixed UID you can 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
nodeimages havenode(UID 1000), soUSER nodeis enough. Distroless images have anonroottag 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
Files the program writes 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 without any capabilities listened on
port 80. It does not need NET_BIND_SERVICE for that, and the published port on the host can be anything.
Read-only and without capabilities
With a non-root user in place, three 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 an unprivileged user on the host, also for images you cannot change. Enable it in
/etc/docker/daemon.json:
{
"userns-remap": "default"
}
The daemon reads it at start: sudo systemctl restart docker. With default, Docker creates the user
dockremap and uses its ranges in /etc/subuid and /etc/subgid.
The costs, before you switch a host:
- From the documentation: "Enabling
userns-remapeffectively masks existing image and container layers". 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 --validatealso warns that it turns off the containerd snapshotter. - The documentation lists
--pid=hostand--network=hostas incompatible with it, and--privilegedneeds--userns=hoston the container. A container with--userns=hostis not remapped, and root in it is root on the host. - Bind-mounted files must be owned by the remapped UIDs to be writable.
Try it on a new host. On a host that already runs containers, a USER per container changes less.
Check the result
- The inspect loop shows a
user=that is neither empty nor zero for your own images. docker top <container> -o user,pid,commdoes not showrootfor the main processes.- The application still writes where it should (uploads, caches, logs).
The daily check docker.root_user of Vulnpocalypse lists the containers that run as root.
Sources
- Docker docs: Running containers, User: default user root,
--userformats. - Docker docs: Dockerfile reference, USER
- Docker docs: Isolate containers with a user namespace
- Distroless README:
nonroottags; UID 65532 read from the configuration ofgcr.io/distroless/static-debian13:nonroot. - Node.js Docker image documentation:
the
nodeuser.
Check your servers every day
The Vulnpocalypse collector reports the packages and settings of a server once a day, and the digest lists what to fix, most urgent first.
Vulnpocalypse