Cloud Intermediate
Multi-stage Docker Builds¶
DockerMulti-stageBuild 3 min read
Optimizing Docker images with multi-stage builds. Smaller images, more secure production.
Principle¶
One Dockerfile, multiple FROM instructions. Build dependencies stay in the build stage, only the runtime goes into the final image.
Go Example¶
# Build — 1.2 GB
FROM golang:1.22 AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o server .
# Runtime — 15 MB!
FROM alpine:3.19
RUN apk add --no-cache ca-certificates
COPY --from=builder /app/server /server
USER nobody
ENTRYPOINT ["/server"]
Rust Example¶
FROM rust:1.76 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"]
Summary¶
Multi-stage = dramatic image size reduction. Go: 1.2GB → 15MB. Node: 1GB → 150MB. Always use them.
Need Help with Implementation?¶
Our team has experience designing and implementing modern architectures. We’re happy to help.