Building Deterministic Container Images with Multi-Stage Distroless Builds

· Daniel Lin dockersecurityci-cd

Half of the vulnerability alerts in a typical container scanner come from tools the application never executes at runtime: curl, apt, bash, and unused shared libraries. Moving compiled Go and Rust services to static multi-stage distroless (or scratch) images removes the entire class of OS-level drift.

Minimal Reproducible Dockerfile

FROM golang:1.23-alpine AS builder
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64     go build -trimpath -ldflags="-s -w" -o /out/app ./cmd/server

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

Debugging Without a Shell

The most common objection to distroless images is live debugging. In Kubernetes, ephemeral debug containers (kubectl debug -it pod/foo --image=nicolaka/netshoot --target=app) attach directly into the target process namespace when you actually need diagnostic binaries—without shipping those binaries to every production node.

← Tuning Linux TCP BBR and Fair Queueing on Cloud Virtual NICsPostmortem: How an Innocent DNS TTL Change Caused a Cascading Cache Stampede →