Good Code
The good version keeps install and build tooling in earlier stages and copies only runtime artifacts into the final image.
Lesson 04
Separate build tooling from the final runtime image so production containers contain only what they need to run.
FROM node:22-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
FROM deps AS build
COPY . .
RUN npm run build
FROM node:22-alpine AS runtime
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build /app/.next/standalone ./
COPY --from=build /app/.next/static ./.next/static
CMD ["node", "server.js"]FROM node:22-alpine
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build
CMD ["npm", "start"]The good version keeps install and build tooling in earlier stages and copies only runtime artifacts into the final image.
The bad version ships source files, dev dependencies, and build tools in the same image that runs production.