Including build tools in final image wastes space. Multi-stage builds separate build and runtime.
Single-Stage (Large):
FROM node:18 WORKDIR /app COPY . . RUN npm install # Includes dev dependencies RUN npm run build CMD ["node", "dist/server.js"] # Result: 1.2 GB (includes npm, build tools)
Multi-Stage (Small):
# Stage 1: Build FROM node:18 AS builder WORKDIR /app COPY package*.json ./ RUN npm install COPY . . RUN npm run build # Stage 2: Production FROM node:18-slim WORKDIR /app COPY --from=builder /app/dist ./dist COPY --from=builder /app/node_modules ./node_modules CMD ["node", "dist/server.js"] # Result: 180 MB (only runtime files)
85% smaller image!
