Good Code
The good version creates a dedicated user, assigns ownership during copy, and switches to that user before the process starts.
Lesson 06
Run application processes as a non-root user and make file ownership match the runtime user.
FROM node:22-alpine AS runtime
WORKDIR /app
RUN addgroup -S app && adduser -S app -G app
COPY --chown=app:app package.json package-lock.json ./
RUN npm ci --omit=dev
COPY --chown=app:app server.js ./server.js
USER app
CMD ["node", "server.js"]FROM node:22-alpine
WORKDIR /app
COPY . .
RUN npm install
CMD ["node", "server.js"]The good version creates a dedicated user, assigns ownership during copy, and switches to that user before the process starts.
The bad version runs as the default root user. A compromised process gets more privileges inside the container than it needs.