Sub topic 13
Running as a non-root user
This chapter assumes you can write a multi-stage Dockerfile, know that COPY --from moves files between stages, and can run containers with docker run.
Why root is a problem
Containers run as root (UID 0) unless you say otherwise. A container is isolated, but root inside it is still a risky default. If an attacker exploits your app, they get root inside the container. That makes it easier to modify files, install tools, and attack the kernel or a mounted volume.
The fix is the USER instruction. Every later RUN, CMD and ENTRYPOINT in that stage runs as the user you name. Multi-stage builds help here because the build stage can stay root, and only the final stage needs a restricted user.
Picking a user
What you write depends on the base image.
| Base image | Available user | What to do |
|---|---|---|
| `node` (any variant) | `node` (UID 1000) | `USER node` |
| `python:*-slim` | `nobody`, or your own | `USER nobody` or create one |
| Debian or Ubuntu | none by default | `groupadd` and `useradd` |
| Alpine | none by default | `addgroup` and `adduser` |
| distroless | `nonroot` (UID 65532) | use the `:nonroot` tag |
| `scratch` | none, and no `/etc/passwd` | use a numeric UID |
Here is a complete example that creates a user on a Debian-based slim image:
FROM python:3.12-slim
RUN groupadd --system --gid 10001 app \
&& useradd --system --uid 10001 --gid app --no-create-home app
WORKDIR /app
COPY --chown=app:app . .
USER app
CMD ["python", "app.py"]A fixed numeric UID is better than a generated one. Kubernetes policies such as runAsNonRoot can verify a number, but they cannot verify a name.
Setting file ownership
COPY creates files owned by root, even after a USER line. A non-root process can read them, but it cannot write to them. If your app writes to its own directory, add --chown, as the example does. This also works with COPY --from=build:
COPY --from=build --chown=app:app /app/dist ./distLeave files read-only to the app when it does not need to write. That is safer, because a compromised process cannot modify its own code.
Non-root with scratch
A scratch image has no /etc/passwd, so USER nobody fails. A numeric ID works without any user database. Add this line to the Go example from earlier, before ENTRYPOINT:
USER 65532:65532Checking the result
Look at the configured user, then ask a running container who it is:
docker inspect --format '{{.Config.User}}' myapp:small
docker run --rm myapp:small idid needs a shell tool inside the image, so it does not work on scratch or distroless. For those, rely on docker inspect.
Common errors
- Permission denied when writing. The app writes to a directory owned by root. Use
--chown, or create the directory andchownit beforeUSER. - Cannot bind to port 80. Ports below 1024 are privileged for non-root users in many setups. Listen on 8080 or higher and map it with
-p 80:8080. - Installing packages after
USER.apt-getandnpm install -gneed root. Do those steps before theUSERline. - Bind-mounted volumes fail. The host directory's owner must match the container's UID.
Exercise
A Go binary is built in a build stage and copied into a scratch final stage. A teammate adds USER appuser before ENTRYPOINT and the container fails to start. Why, and what is the fix?
Show answer
scratch contains no /etc/passwd, so the name appuser cannot be resolved. Use a numeric ID such as USER 65532:65532. If the app also needs to write files, copy them in with --chown=65532:65532.