Sub topic 4
Running the process as a non-root user
This chapter assumes you can run containers with docker run, understand Linux users and file permissions, and read simple YAML. It covers why the default user is a risk, how to change it, and which errors to expect.
Why root inside a container matters
By default, a container process runs as user ID 0, which is root. Container root is not the same as host root. Namespaces, capabilities, and seccomp limit it. But it is still UID 0 on a kernel shared with the host.
That has three consequences:
- Files that root owns, including files on bind mounts, are readable and writable by the process.
- Some kernel checks pass for UID 0 that fail for other users.
- A kernel bug or a container escape often gives the attacker the same UID the process had. Escaping as root is worse than escaping as an unprivileged user.
Running as a non-root user removes these advantages. It also reinforces the controls from the previous chapters, because several of them work best when the process is not UID 0.
Setting the user with Docker
Use the --user flag with a numeric UID and GID:
docker run --rm --user 10001:10001 alpine idThe output shows uid=10001 gid=10001. Prefer numeric IDs over names. A name must exist in the image's /etc/passwd, and an image may not define the name you expect. Numeric IDs also let Kubernetes verify that the user is not root.
Choose an ID above 10000. Low IDs often collide with system accounts on the host, which matters if a bind mount or user namespace maps IDs between the two.
Setting the user in the image
Setting the user at run time works, but the safest default is to bake it into the image with a USER instruction:
FROM python:3.12-slim
RUN groupadd --gid 10001 app \
&& useradd --uid 10001 --gid 10001 --no-create-home --shell /usr/sbin/nologin app
WORKDIR /app
COPY --chown=10001:10001 . /app
USER 10001:10001
CMD ["python", "main.py"]Two details matter here. COPY --chown gives the application ownership of its files. Without it, files copied in are owned by root, and the app can read them but not change them, which is usually what you want for code. Grant write access only to directories the app must modify. Placing USER after all the RUN steps that need root, such as package installs, keeps the build working.
Combining the user with other controls
A non-root user works with the controls you have already seen. Capability checks that guard privileged actions fail for non-root users unless a capability was granted. The no-new-privileges flag, covered next, stops the process from regaining privilege through setuid binaries.
docker run --rm \
--user 10001:10001 \
--cap-drop ALL \
--security-opt no-new-privileges:true \
alpine sh -c 'id && grep Cap /proc/self/status'For a non-root user, the effective capability mask (CapEff) is already zero. Dropping capabilities then makes sure the bounding set is empty too, so no later step can bring them back.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `Permission denied` writing to a directory | The directory is owned by root | Use `chown` in the image, or mount a volume owned by the app UID |
| `bind: permission denied` on port 80 | Ports below 1024 were privileged on older kernels | Listen on a port above 1024, such as 8080, and map it |
| `unable to find user app` | The user name is not in `/etc/passwd` | Create the user in the image or pass a numeric UID |
| Kubernetes error `container has runAsNonRoot and image will run as root` | The image has no numeric non-root `USER` | Set `runAsUser` or add a numeric `USER` |
Exercise
Exercise
A container image sets USER app and your Kubernetes pod has runAsNonRoot: true. The pod fails to start with "image will run as root". Why does it fail, and what is the fix?
Show answer
Kubernetes cannot confirm that the name app maps to a non-zero UID, so it rejects the container. Use a numeric user, either USER 10001 in the Dockerfile or runAsUser: 10001 in the pod's securityContext.
Exercise
A container started with --user 10001:10001 gets "Permission denied" when it writes to a bind-mounted host directory owned by root. What are two safe fixes?
Show answer
Change the host directory's owner to UID 10001 (for example with chown 10001:10001), or use a named volume that the app user can write to. Running the container as root is not a safe fix.