โ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
appuserand switch to it beforeCMD. 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 theUSERinstruction before theCMDline. 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 adduserandUSER appuserlines -
docker exec <container_id> whoamireturnsappuser - App still works at
http://localhost:3000
๐ Quiz
Answer all 3 questions correctly to complete this lesson.