Multi-stage build

A multi-stage build uses multiple FROM instructions in one Dockerfile to separate compilation from application execution. In this course, the first stage uses golang:1.27 to compile a Go binary, while the final stage copies only that binary into a minimal runtime image.

Minimal example

dockerfile
FROM golang:1.27 AS builder
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o /out/api ./cmd/api
FROM gcr.io/distroless/static:nonroot
COPY --from=builder /out/api /api

Learn this concept

Lesson 7.1 · Multi-stage Dockerfiles for Go

Used in exercises

Exercise 7.2 · Production container stack
Prerequisite conceptsGo modules