Go’s static binaries and low runtime dependency footprint make it container-friendly.
# build stage
FROM golang:1.22 AS build
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/mytool
# runtime stage
FROM gcr.io/distroless/static:nonroot
COPY --from=build /out/app /app
USER nonroot:nonroot
ENTRYPOINT ["/app"]
If you call HTTPS endpoints, you need CA certs in the image (distroless includes them; scratch does not by default).
CGO_ENABLED=0For CI jobs, a containerized CLI can be useful (pin tool + dependencies). For end-user machines, prefer direct binary releases.