Interview Prep/Real-World Interview/Write-a-Dockerfile tasks

Write a Dockerfile for a Go service that produces a tiny image (under 20 MB).

IntermediateHands-onWrite-a-Dockerfile tasks
Answer
FROM golang:1.23 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/app ./cmd/app

FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build /out/app /app
USER nonroot:nonroot
ENTRYPOINT ["/app"]

Key trick → CGO_ENABLED=0 builds a static binary that needs no libc, so it runs on distroless/static (or scratch). -s -w strips debug symbols.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-a-Dockerfile tasks