Skip to content

Multi-stage builds: compile big, ship small

Select an icon to see what it is.

Step 1 of 6

One Dockerfile, two stages

The Dockerfile has two FROM lines. The first, named builder, uses a full Go image. The second starts from alpine, and only what it copies in will ship.

$ docker build -t web:1.0 .
PrevNext

Dockerfile (read by BuildKit through the build session)

FROM golang:1.22 AS builder
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /out/web .
FROM alpine
COPY --from=builder /out/web /web
CMD ["/web"]