🐳Docker Learning Hub
Lesson 14 • Multi-stage builds
Lesson 14

Multi-stage builds

This page shows how Docker can use one stage for building and another stage for running, which keeps the final image cleaner.

Builder stage

The first stage installs tools and creates the final build output.

Runtime stage

The final stage copies only what is needed to run the app and leaves build clutter behind.

Why it matters

This is one of the best ways to create smaller, cleaner production images.

Example structure

FROM node:18 AS builder WORKDIR /app COPY . . RUN npm install RUN npm run build FROM node:18-alpine WORKDIR /app COPY --from=builder /app/dist /app/dist CMD ["node", "dist/server.js"]

Main benefits

  • Smaller final images
  • Cleaner runtime environment
  • Build-only tools stay out of the final stage

Common mistakes

  • Copying too much from the builder stage
  • Installing unnecessary runtime tools again in the final stage
  • Not understanding what files the app actually needs at startup

Next page: Lesson 15 explains Docker Hub and registries so you can tag, push, and pull your own images properly.