How would you run containers as a non-root user for security hardening?
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.