What is a multi-stage build?medium
Answer
Multi-stage builds use multiple FROM instructions - early stages compile or build the application, and the final stage copies only the artifacts into a minimal base image, discarding build tools.
Explanation
A Go or Java app might need a full SDK to compile but only a minimal runtime in production. Without multi-stage: final image includes compilers, source code, and test dependencies - hundreds of MB larger. With multi-stage: build in FROM golang:1.22 AS builder , then FROM scratch and COPY --from=builder /app/binary / - resulting image is just the binary. Smaller images are faster to pull, have a smaller attack surface, and are cheaper to store.
Follow-upHow do you pass build arguments between stages?