3 Hours

7. Multi-Stage Builds: Discarding Build Tools

Advanced

Building Once, Running Lean

Multi-stage builds let you use a complete build environment (compilers, package managers, source code) while keeping your final production image tiny. This module teaches the pattern for every major language.

The Multi-Stage Pattern

Multi-stage builds use multiple `FROM` statements in a single Dockerfile. Each `FROM` starts a new stage. You can name stages with `AS stage_name`. Later stages can copy files from earlier stages using `COPY --from=stage_name`. The final stage determines what is included in the final image — earlier stages are discarded. This means you can install a full Go SDK (1.2GB) in the builder stage, compile your binary, and then copy only the 12MB binary to a scratch-based final stage. The Go SDK never appears in the final image. The pattern works for any language: build stage has the compiler/toolchain, runtime stage has only the compiled output and runtime dependencies.

Text
# Basic multi-stage pattern
FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o myapp .

FROM scratch
COPY --from=builder /app/myapp /myapp
ENTRYPOINT ["/myapp"]

# Result: ~1.2GB build stage discarded, 12MB final image

Language-Specific Multi-Stage Patterns

Each language has its own optimal pattern for multi-stage builds. For Go, the build stage uses the full Go SDK with CGO disabled for static linking, and the final stage uses `scratch`. For Rust, the build stage uses the rust image, and the final stage uses `debian:bookworm-slim` (Rust binaries usually need glibc). For Java, the build stage uses `maven` or `gradle`, and the final stage uses a minimal JRE (like `eclipse-temurin:17-jre-alpine`). For Node.js, the build stage can include TypeScript compiler, and the final stage copies only the compiled JavaScript and production dependencies. For Python, multi-stage is less beneficial but can still separate build dependencies (compilers for native modules) from runtime dependencies.

Text
# Go — to scratch
FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY . .
RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o app .
FROM scratch
COPY --from=builder /app/app /app
ENTRYPOINT ["/app"]

# Rust — to slim Debian
FROM rust:1.74 AS builder
WORKDIR /app
COPY . .
RUN cargo build --release
FROM debian:bookworm-slim
COPY --from=builder /app/target/release/myapp /usr/local/bin/
CMD ["myapp"]

# Java/Maven — to JRE
FROM maven:3.9-eclipse-temurin-17 AS builder
WORKDIR /app
COPY pom.xml .
RUN mvn dependency:go-offline
COPY src ./src
RUN mvn package -DskipTests
FROM eclipse-temurin:17-jre-alpine
COPY --from=builder /app/target/*.jar app.jar
CMD ["java", "-jar", "app.jar"]

# Node.js/TypeScript — two-stage build
FROM node:20-alpine AS builder
WORKDIR /build
COPY package*.json ./
RUN npm ci
COPY tsconfig.json ./
COPY src ./src
RUN npm run build

FROM node:20-alpine
WORKDIR /app
COPY --from=builder /build/dist ./dist
COPY --from=builder /build/node_modules ./node_modules
CMD ["node", "dist/server.js"]

Named Stages and Stage Reuse

You can name stages and reuse them. This is useful when you have multiple similar stages (like testing and building). You can also use a stage as the base for another stage with `FROM stage_name`. This creates a hierarchy of stages. The final stage can copy from multiple earlier stages, allowing you to assemble the final image from various build artifacts. For example, you might have a `deps` stage that downloads dependencies, a `build` stage that compiles code, and a `test` stage that runs tests. The final `production` stage copies artifacts from `deps` and `build` but not from `test`.

Text
# Reusing stages
FROM node:20-alpine AS base
WORKDIR /app

FROM base AS deps
COPY package*.json ./
RUN npm ci

FROM base AS builder
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build

FROM base AS runner
COPY --from=deps /app/node_modules ./node_modules
COPY --from=builder /app/dist ./dist
USER node
CMD ["node", "dist/server.js"]

Knowledge Check

Ready to test your understanding of 7. Multi-Stage Builds: Discarding Build Tools?

Back