Good Code
The good version uses a specific runtime base and keeps production dependency installation explicit.
Lesson 02
Choose a narrow base image and pin versions deliberately so rebuilds do not change underneath you without review.
FROM node:22.11.0-alpine3.20 AS runtime
WORKDIR /app
ENV NODE_ENV=production
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY server.js ./server.js
CMD ["node", "server.js"]FROM node:latest
WORKDIR /app
COPY . .
RUN npm install
CMD ["node", "server.js"]The good version uses a specific runtime base and keeps production dependency installation explicit.
The bad version depends on latest, so rebuilding the same commit can silently pull a different Node version and operating system packages.