How would you run containers as a non-root user for security hardening?

Hard Topic: Docker May 24, 2026

Running containers as root is a significant security risk. If an attacker escapes the container, they have root on the host. Harden your images:

FROM node:20-alpine

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

# Set working directory and permissions
WORKDIR /app
COPY --chown=appuser:appgroup . .

# Switch to non-root user
USER appuser

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

Also enforce this at the Kubernetes level with a SecurityContext: runAsNonRoot: true.

← Previous How do Docker volumes differ from bind mounts? Next → What is Docker Compose and when would you...

Practice Similar Questions

Back to Docker Topics