Sub topic 6
Running Your Application as a Non-Root User
This chapter assumes you can write a Dockerfile, run containers, and read Linux file permissions (owner, group, mode bits). It builds on the isolation chapter, where you saw that container processes share the host kernel.
Why root in a container matters
By default, the process in a container runs as UID 0 (root). Namespaces and dropped capabilities limit what that root can do, but the limits are thin. If an attacker escapes through a kernel bug, a mounted socket, or a misconfigured volume, they arrive on the host as UID 0. A process running as an unprivileged UID has far less to work with. It cannot write to most system paths, bind low ports, or change ownership of files.
Running as non-root is a cheap second layer. It costs a few lines in the Dockerfile.
Setting the user in a Dockerfile
Create the account while you are still root, then switch with USER. Every later RUN, CMD, and ENTRYPOINT uses that account.
# syntax=docker/dockerfile:1
FROM python:3.12-slim
RUN groupadd --gid 10001 app \
&& useradd --uid 10001 --gid app --no-create-home --shell /usr/sbin/nologin app
WORKDIR /app
COPY --chown=app:app . .
USER 10001:10001
CMD ["python", "main.py"]Three details matter here:
- Use a numeric UID. Kubernetes policies such as
runAsNonRootcan only verify a numeric user. A name likeappcannot be checked without reading/etc/passwd. - Pick a high UID. UIDs above 10000 are unlikely to match a real account on the host.
- Use
--chownonCOPY. Files copied without it belong to root. The app can read them but not write to them, which is usually what you want for code. Give write access only to the directories that need it.
Some images ship a user already. The node images include a node user, and the distroless :nonroot tags use UID 65532. In those cases, USER node or USER nonroot:nonroot is enough.
Overriding the user at run time
You can set or change the user without rebuilding the image:
docker run --rm --user 10001:10001 myapp:1.0 idThis prints uid=10001 gid=10001. The --user flag is useful for testing an image that you suspect still needs root, and it lets operators enforce non-root even on images you did not build.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `permission denied` writing to `/app/data` | Directory is owned by root | `RUN mkdir /app/data && chown 10001:10001 /app/data` before `USER` |
| `permission denied` binding port 80 | Ports below 1024 are privileged | Listen on 8080 and map it with `-p 80:8080` |
| Bind-mounted files are unreadable | Host file UID does not match the container UID | Match ownership on the host, or choose the UID to fit |
| `apt-get` fails after `USER` | Package installs need root | Run installs before `USER` |
A named volume copies the ownership of the image directory the first time it is created. A bind mount uses the host's ownership as is, which is the usual source of mismatch errors.
Verifying the result
Check the image configuration and the live process:
docker inspect --format '{{.Config.User}}' myapp:1.0
docker exec web idAn empty result from the first command means the image runs as root.
Exercise
Your container fails at startup with "permission denied" when writing to /var/cache/app. The Dockerfile ends with USER 10001:10001. What is the likely cause, and how do you fix it without switching back to root?
Show answer
The directory was created by root (or by an earlier root step), so UID 10001 cannot write to it. Before the USER line, create it and set ownership with RUN mkdir -p /var/cache/app && chown 10001:10001 /var/cache/app. Alternatively, mount a tmpfs or volume at that path with the right ownership.