Project 1 โ€บ 04 Add a non-root USERGitHub โ†—
๐Ÿค– AI Prompt โ€” stuck? paste this

โ€œUpdate my Dockerfile to create a non-root user called appuser and switch to it with USER before the CMD. Explain why running as root is a security risk.โ€

04 โ€” Add a non-root USER in the Dockerfile

Category: Dockerfile ยท Priority: High ยท Step: 5

Create a user like appuser and switch to it before CMD. Never run containers as root in production.

๐ŸŽฏ What is this task?

By default, everything inside a Docker container runs as the root user โ€” which is like running your whole app as a system administrator. This is a security risk. If someone hacks your app, they'd have full control inside the container. The fix is simple: create a regular user inside the Dockerfile and switch to it before starting the app.

๐Ÿค– AI Prompt

"Update my multi-stage Node.js Dockerfile to add a non-root user called appuser. Create the user in the final runtime stage, and switch to it using the USER instruction before the CMD line. Show me the full updated Dockerfile."

๐Ÿ“‹ Step-by-step

Step 1 โ€” Update the runtime stage of your Dockerfile

Find the # ---- Stage 2: Runtime ---- section and add the user creation lines:

# ---- Stage 2: Runtime ----
FROM node:20-alpine

WORKDIR /app

# Copy from builder
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/index.js ./index.js
COPY --from=builder /app/package.json ./package.json

# Create a non-root user and group
RUN addgroup -S appgroup && adduser -S appuser -G appgroup

# Change ownership of the /app folder to that user
RUN chown -R appuser:appgroup /app

# Switch to the non-root user
USER appuser

EXPOSE 3000

CMD ["node", "index.js"]

Step 2 โ€” Rebuild

docker build -t my-docker-app:v3 .

Step 3 โ€” Verify the container runs as a non-root user

docker run -p 3000:3000 --rm my-docker-app:v3

In a second terminal, while the container is running:

docker ps
docker exec <container_id> whoami

You should see appuser printed โ€” not root.

Step 4 โ€” Test the app still works

Open http://localhost:3000 โ€” should still respond correctly.

โœ… Done when

  • Dockerfile has RUN adduser and USER appuser lines
  • docker exec <container_id> whoami returns appuser
  • App still works at http://localhost:3000

๐Ÿ“ Quiz

Answer all 3 questions correctly to complete this lesson.

1. By default, processes inside a container run as which user?
2. Where should the USER instruction go?
3. Why avoid running as root in production?
โ† PreviousPass the quiz to completeNext โ†’