📖 Scenario: You are deploying a simple web application using Docker. For security reasons, you want the container to run as a non-root user instead of the default root user.
🎯 Goal: Learn how to create a Dockerfile that sets up a non-root user and runs the container with that user.
📋 What You'll Learn
Create a Dockerfile with a base image
Add a non-root user to the Dockerfile
Set the container to run as the non-root user
Verify the container runs as the non-root user
💡 Why This Matters
🌍 Real World
Running containers as non-root users improves security by limiting permissions inside the container, reducing risks if the container is compromised.
💼 Career
Many DevOps and security roles require knowledge of container security best practices, including running containers as non-root users.
Progress0 / 4 steps
1
Create a Dockerfile with a base image
Create a file named Dockerfile and write a line to use the python:3.12-slim base image.
Docker
Hint
The FROM instruction sets the base image for your Dockerfile.
2
Add a non-root user
Add lines to the Dockerfile to create a new user named appuser with user ID 1001 and create a home directory for this user.
Docker
Hint
Use RUN useradd -m -u 1001 appuser to add the user with a home directory.
3
Set the container to run as the non-root user
Add a line to the Dockerfile to switch the user to appuser so the container runs as this user.
Docker
Hint
The USER instruction sets the user for running the container.
4
Build and run the container to verify the user
Write the Docker commands to build the image named nonrootapp from the current directory and run a container that prints the current user with whoami.
Docker
Hint
Use docker build -t nonrootapp . to build and docker run --rm nonrootapp whoami to check the user.
Practice
(1/5)
1. Why is it recommended to run Docker containers as a non-root user?
easy
A. To enable root access inside the container
B. To make containers run faster
C. To improve security by limiting container permissions
D. To allow containers to access all host files
Solution
Step 1: Understand container user privileges
Running containers as root gives full permissions inside the container, which can be risky if the container is compromised.
Step 2: Recognize security benefits of non-root users
Using a non-root user limits what the container can do, reducing potential damage from attacks.
Final Answer:
To improve security by limiting container permissions -> Option C
2. Which Dockerfile instruction sets the user to run the container as non-root?
easy
A. RUN
B. CMD
C. ENTRYPOINT
D. USER
Solution
Step 1: Identify Dockerfile instructions for user control
The USER instruction sets the user and group for running commands inside the container.
Step 2: Differentiate from other instructions
RUN executes commands during build, CMD and ENTRYPOINT define container start commands but do not set user.
Final Answer:
USER -> Option D
Quick Check:
Set user = USER [OK]
Hint: USER sets container run user in Dockerfile [OK]
Common Mistakes:
Using RUN to set user (it runs commands)
Confusing CMD or ENTRYPOINT with user setting
Omitting USER instruction for non-root
3. What is the effect of running this command?
docker run --user 1001 alpine id
medium
A. Shows the user ID 1001 inside the container
B. Runs the container as root user
C. Fails because user 1001 does not exist
D. Shows the container's hostname
Solution
Step 1: Understand the --user flag
The --user 1001 option runs the container process as user ID 1001 instead of root.
Step 2: Analyze the command output
The id command inside the container prints the current user info, so it will show UID=1001.
Final Answer:
Shows the user ID 1001 inside the container -> Option A
Quick Check:
--user 1001 + id = UID 1001 [OK]
Hint: --user sets UID; id shows current user info [OK]
Common Mistakes:
Assuming container runs as root despite --user
Thinking id shows hostname
Believing command fails if user not in /etc/passwd
4. You wrote this Dockerfile snippet:
FROM alpine
USER appuser
CMD ["sh"]
But when running the container, you get a permission denied error. What is the likely cause?
medium
A. The CMD syntax is incorrect
B. The user 'appuser' does not exist in the image
C. The base image alpine does not support USER
D. The container needs root to run sh
Solution
Step 1: Check if user exists in image
The USER appuser requires that 'appuser' is created in the image, otherwise permission errors occur.
Step 2: Verify other options
CMD syntax is correct, alpine supports USER, and sh can run as non-root if permissions allow.
Final Answer:
The user 'appuser' does not exist in the image -> Option B
Quick Check:
User must exist = B [OK]
Hint: Create user before USER instruction to avoid permission errors [OK]
Common Mistakes:
Assuming USER works without creating user
Blaming CMD syntax for permission issues
Thinking alpine disallows USER instruction
5. You want to run a container as a non-root user with UID 2000 and GID 3000, but the image has no such user. Which is the best way to do this securely?
hard
A. Add a user with UID 2000 and GID 3000 in the Dockerfile, then use USER
B. Run the container as root and change permissions inside
C. Use --user 2000:3000 without creating the user in the image
D. Modify the host system to add user 2000
Solution
Step 1: Understand user creation in Dockerfile
To run as a specific non-root user, create that user and group inside the image with matching UID and GID.
Step 2: Use USER instruction to switch user
After creating the user, use the USER instruction to run container processes as that user securely.
Step 3: Evaluate other options
Running as root is insecure, using --user without user in image may cause permission issues, and modifying host users is unrelated.
Final Answer:
Add a user with UID 2000 and GID 3000 in the Dockerfile, then use USER -> Option A
Quick Check:
Create user + USER = secure non-root run [OK]
Hint: Create user in Dockerfile before USER for secure non-root run [OK]
Common Mistakes:
Running as root to avoid user setup
Using --user without user in image causing errors
Changing host users instead of container users
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.