Sub topic 12
Running as a non-root user
This chapter assumes you can write a basic Dockerfile, build and run an image, and understand layers and COPY. It covers why containers should not run as root and how to set that up.
Why root is a problem
By default, the process in a container runs as root (user ID 0). That root is namespaced from the host's root, but it is still powerful inside the container. If an attacker exploits your app, they get root in the container. They can then modify any file in the image's filesystem, install tools, and probe the network. A misconfiguration, such as a mounted host directory or a container runtime bug, can turn that into access to the host.
Running as an unprivileged user limits the damage. It is one of the cheapest hardening steps you can take.
The USER instruction
USER sets the user (and optionally the group) for all later RUN, CMD, and ENTRYPOINT instructions. It also sets the user for the running container.
Many official images already include an unprivileged user. The node images ship with a user named node, so the Node.js Dockerfile from earlier needs only one line:
FROM node:20-slim
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
ENV NODE_ENV=production
EXPOSE 3000
USER node
CMD ["node", "server.js"]Place USER after the steps that need root, such as installing packages. Put it before CMD.
Creating your own user
If the base image has no suitable user, create one with RUN. The commands differ by distribution.
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 10001:10001
CMD ["python", "-m", "http.server", "8000"]Two details matter here:
- Use a fixed numeric ID. Kubernetes policies such as
runAsNonRootcan only verify a numeric user. A name likeappcannot be checked against the image. - Use a high ID (10000 or above) so it does not collide with a real user on the host if you bind-mount files.
On Alpine images, the equivalent commands are addgroup -S and adduser -S.
File ownership
Files copied with COPY are owned by root by default. A non-root process can read them if the permissions allow, but it cannot write to them. If the app needs to write to a directory, give that directory to the user.
FROM node:20-slim
WORKDIR /app
COPY --chown=node:node package*.json ./
RUN npm ci --omit=dev
COPY --chown=node:node . .
RUN mkdir -p /app/data && chown node:node /app/data
USER node
CMD ["node", "server.js"]Use COPY --chown instead of a separate RUN chown -R. A recursive chown in its own layer duplicates every file it touches into a new layer, which grows the image.
Read-only is often the better choice. Leave code owned by root so the app cannot rewrite itself, and make only the specific data directories writable.
Ports below 1024
An unprivileged user cannot bind to ports below 1024 by default. Listen on a high port such as 3000 or 8080 inside the container. You can still publish it as port 80 on the host with -p 80:8080.
Verifying the result
Check the user a container runs as:
docker run --rm myapp:1.0 idFor an image that sets ENTRYPOINT, override it to run the check:
docker run --rm --entrypoint id myapp:1.0The output should show a non-zero uid. You can also run docker inspect --format '{{.Config.User}}' myapp:1.0 to see the configured user.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `EACCES: permission denied` when writing a file | The directory is owned by root | Use `chown` or `COPY --chown` for that directory |
| `bind: permission denied` at startup | The app listens on a port below 1024 | Use a port of 1024 or higher |
| `npm install` or `apt-get` fails with permission errors | `USER` appears before the install step | Move `USER` after the root-level steps |
| Kubernetes rejects the pod with `runAsNonRoot` | `USER` is a name, not a number | Use a numeric ID such as `USER 10001` |
| Mounted volume is not writable | The host directory's owner differs from the container user's ID | Match the IDs or adjust the directory permissions |
Combining with multi-stage builds
In a multi-stage build, set the user in the final stage. The distroless nonroot tag from the Go example already runs as a non-root user (UID 65532), so no USER line is needed there. Run docker inspect to confirm what an image uses.
Exercise
A Dockerfile has USER node on line 3, followed by RUN apt-get update && apt-get install -y curl on line 4. The build fails with a permission error. Why, and how do you fix it?
Show answer
After USER node, every later RUN runs as the unprivileged node user, which cannot install system packages. Move the apt-get step before USER node so it runs as root, and keep USER node just before CMD.
Exercise
Why is COPY --chown=app:app . . better than COPY . . followed by RUN chown -R app:app /app?
Show answer
COPY --chown sets ownership as the files are added, so the files exist in one layer. The separate chown -R creates a new layer that holds a copy of every changed file, which increases the image size.