Nonroot runtime and artifact permissions
A small example
FROM busybox:1.37 AS prepareRUN printf "approved report\n" > /report.txt && chmod 400 /report.txtFROM busybox:1.37WORKDIR /appCOPY --from=prepare --chown=10001:10001 /report.txt /app/report.txtUSER 10001:10001CMD ["cat", "/app/report.txt"]The generated file is readable only by its owner. The copy changes its owner and group in the final image to the same numeric IDs used by USER. The program can read it but has no write bit on the file. The runtime does not need an account name for this simple numeric-ID example; some applications need user records or a home directory. A nonroot image user is distinct from a rootless daemon and does not replace capabilities, mount policy or other isolation controls. The file mode does not prevent replacement through a writable parent directory; check directory permissions separately when protecting integrity. The variation asserts both UID and GID, then checks file access.
Try it in a local lab
Manual lab only. Use an approved fresh disposable Linux VM with blocked network egress, a local Engine and local Buildx docker driver. Check docker context show, docker version, docker buildx version and docker buildx ls. Confirm reviewed preloaded native busybox:1.37 and, for the scratch example, golang:1.26; verify actual digest, root build user, default entrypoint, required applets/compiler and frontend support. Tags are mutable; record actual identities. Stop if any dependency needs fetching or builder is remote. --network none limits RUN networking, not all builder traffic. Confirm all bm-v10 tags are unused, including checks and full. Execute commands individually in the specified fixture directory and stop on unexpected failure. All expected results are predictions. No secrets, host mounts or registry pushes. Before querying or building, confirm DOCKER_HOST, DOCKER_CONTEXT and BUILDX_BUILDER overrides and the effective endpoint are approved; do not contact an unapproved endpoint. docker build normally uses the Engine default builder, not necessarily the starred Buildx builder. Record the effective local docker driver and platform; review the synthetic context and ignore rules. After each build, require exit status zero before running its tag; an earlier tag may still exist after failure.
Open lab-fixtures/docker-multi-stage-images/05-nonroot-runtime/ in the extracted archive. The Dockerfile and these files are supplied. Work on a disposable copy, not the original archive.
docker build --load --network none --progress=plain -t bm-v10-nonroot-runtime:lab .docker run --rm --pull=never --network none bm-v10-nonroot-runtime:labdocker run --rm --pull=never --network none bm-v10-nonroot-runtime:lab sh -c 'test "$(id -u)" = 10001 && test "$(id -g)" = 10001 && test -r /app/report.txt && test ! -w /app/report.txt && id -u && id -g'Expect approved report, then two lines each containing 10001 and a zero exit status. File access is checked under the default identity, not all overrides or directory permissions.
Confirm the tag belongs to this lab and no containers depend on it. Remove only that tag without forcing. Cached data may remain; dispose of the VM rather than clearing a shared cache. Restore any edited fixture or discard the working copy.
Common mistake
Do not use chmod 777 or switch back to root just to solve a permissions problem. Grant the minimum needed access to the intended identity and verify it.
Manual practice
Predict what happens if you remove --chown but keep mode 400 and USER 10001. Explain why fixing ownership is more appropriate than making the file world-writable.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Nonroot runtime and artifact permissions — Knowledge check
Apply the multi-stage concept to a new scenario.
What sets the default runtime identity?
Why is --chown useful here?
Does nonroot USER imply rootless Docker Engine?