Interview Prep/Docker/Build Performance & CI

How do you build a Go image under 15 MB?

BeginnerProductionBuild Performance & CI
Answer

Compile a static binary and ship on distroless/static or scratch.

FROM golang:1.23 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/app ./cmd/app
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]
All Docker questions Roadmap: Docker & containers

More on Build Performance & CI