Explain Multi-stage Builds in Docker and how they reduce production image sizes.
expand_more
FROM instructions in a single Dockerfile. You use a heavy build environment to compile code, and copy only the compiled binaries or static assets into a lightweight runtime image:
FROM node:20 AS builder
WORKDIR /app
COPY . .
RUN npm run build
FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
CMD ["node", "dist/main.js"]
This keeps compilers and source files out of the production image, reducing sizes by up to 90%.