Running Containers as Non-Root to Block Privileged Memory Reads
Short answer
A Adding the USER myappuser instruction runs the container as a non-root user, blocking privileged processes from reading host memory.
Question
Key Takeaways
Container escape and privilege-escalation defenses turn on the UID a container runs as. Adding USER myappuser in the Dockerfile drops the workload from root to an unprivileged account, so a privileged process can no longer reach host memory. Read-only filesystems, alpine:latest, and host daemon patching each target a different weakness and leave this memory-read exploit unaddressed.
Expert Analysis
Running containers as a non-root user (via
USER myappuser) prevents privileged processes from accessing host resources. When a container runs as root, and a process escapes the container, it has root privileges on the host. Running as a non-root user limits the damage even if a container escape occurs, as the process only has limited user permissions.
The deciding constraint in this scenario is the specific attack: a privileged process reading host memory, with the fix sought in the Dockerfile. Option A, adding USER myappuser, sets the default UID/GID for every subsequent Dockerfile layer and for the running container, so the workload drops from UID 0 to an unprivileged account. Because the container's process is no longer root, the kernel-level access that root confers — including the ability to reach into host memory that a privileged process could otherwise map — is denied. The change is baked into the image, which is exactly where the engineer is told to look, and it applies to every future build derived from that Dockerfile rather than to one running instance.
Option B, read-only filesystem configuration, addresses a different axis of containment: it stops a process from modifying files under / (useful against persistence or tampering), but a memory read never writes to the filesystem, so the flag is silent against this exploit. Option C, swapping FROM alpine:3.17 for FROM alpine:latest, is about base-image currency: latest is an unpinned moving tag that resolves to whatever Alpine publishes at pull time, which actually reduces build reproducibility and does not change the effective UID of the container process. Option D, patching the host running the Docker daemon, is a legitimate hardening step but it is the wrong layer for this question's instruction to review the Dockerfile, and it does not give the container image itself a non-root identity; it also does nothing to stop a privileged process inside the container from reading memory it is already entitled to see.
One mechanism this question leans on without stating it: Docker's USER directive does not create the account — it only names a UID that must already exist (or be creatable) in the image, and it shifts the process's effective credentials for all later RUN, CMD and ENTRYPOINT instructions. In practice myappuser is typically created earlier in the same Dockerfile with an explicit UID, and any files the build chowns to that UID remain readable/writable for it while root-only files stay out of reach. That UID is what the container runtime hands to the kernel, and it is why a memory-access request that root would be granted is refused for myappuser.
The pattern here generalizes: match the mitigation to the exploit's primitive (memory read → privilege drop, filesystem write → read-only mount, image drift → pinned digest) and to the artifact the prompt names (the Dockerfile, not the host or the cluster).
Official Reference
https://docs.docker.com/build/building/best-practices/#use-a-non-root-user
This link opens the official vendor documentation in a new tab.
Exam Strategy
Anchor the mitigation to both the exploit primitive and the artifact the prompt names. Here the primitive is a memory read and the artifact is the Dockerfile, so only an in-image instruction that changes the process UID qualifies. When options mix image settings (USER, FROM) with runtime or host-level changes, prefer the one that ships inside the image and persists across every build.
Related Analysis
Practice All CV0-004 Questions
Access 100 questions with complete answers and detailed explanations.
View Full CV0-004 Practice Test →