A small Go service should be one of the easiest things to ship on a PaaS. The runtime is in the binary; there’s no interpreter to install. Yet you’ll find teams shipping 1.2 GiB images of a service whose actual binary is 18 MiB, because the Dockerfile starts FROM golang:1.23 and never throws away the build stage. The cost is real — pull time on every deploy, attack surface, scan time. The pattern that fixes it is a few lines.
What’s happening
Three reasonable Dockerfile strategies for Go, picked by trade-off:
| Strategy | Image size | Build time (cold) | Build time (warm) | Audit / debuggability |
|---|---|---|---|---|
Build inside golang:1.23 | 900 MiB+ | Fast | Fast | Easy (shell, tools) |
Multi-stage golang → alpine | 25-50 MiB | Same | Same | Reasonable |
Multi-stage golang → distroless | 20-30 MiB | Same | Same | Harder (no shell) |
Multi-stage golang → scratch | 10-20 MiB | Same | Same | Hard (no anything) |
Prebuilt binary in CI → scratch | 10-20 MiB | N/A (CI does it) | Fast | Same as scratch |
There’s no “best” — there’s a best for your operational reality. If your CI is reliable and you have artifact storage, prebuilt + scratch is the smallest, fastest deploy. If your team needs to debug in production occasionally and doesn’t have ephemeral debug containers, alpine is the pragmatic choice. Distroless is the middle ground: tiny, has CA certs and timezone data, but no shell — security teams love it.
The 1.2 GiB image isn’t a flaw of any of these strategies. It’s a Dockerfile that copied the whole golang toolchain into the final image.
How to verify
# How big is the image, and what's in it?
docker images myorg/api:latest
docker history myorg/api:latest --no-trunc | head -20
# What's inside?
docker run --rm --entrypoint /bin/sh myorg/api:latest -c 'du -sh /usr/local/go /go /tmp 2>/dev/null'
# If /usr/local/go is in the image, you've shipped the toolchain.
# Static linkage check — does the binary depend on glibc?
docker run --rm --entrypoint /bin/sh myorg/api:latest -c 'ldd /app/api 2>&1 || echo "static binary"'
If ldd reports not a dynamic executable or “static binary”, the binary will run on scratch. If it lists libc.so.6, you must use a base image that has glibc (alpine has musl, not glibc — your binary may or may not work).
The fix
Three patterns. Pick by your team’s debugging discipline and CI reliability.
Pattern A — multi-stage with distroless (recommended default):
# syntax=docker/dockerfile:1.7
FROM golang:1.23-alpine AS builder
# Build tools and CA certs (copied to the final image)
RUN apk add --no-cache git ca-certificates tzdata && \
update-ca-certificates
WORKDIR /src
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
go mod download
COPY . .
# CGO_ENABLED=0 produces a fully static binary.
# -trimpath strips the build host's path prefix from stack traces (reproducibility).
# -ldflags '-w -s' strips DWARF + symbol tables; saves 25-30% size at the cost of harder profiling.
# -ldflags '-X main.version=...' bakes in version metadata for /version handlers.
ARG VERSION=dev
ARG COMMIT=unknown
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build \
-trimpath \
-ldflags "-w -s -X main.version=${VERSION} -X main.commit=${COMMIT}" \
-o /out/api \
./cmd/api
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=builder /out/api /app/api
USER nonroot:nonroot
EXPOSE 8080
ENTRYPOINT ["/app/api"]
The resulting image is ~25 MiB. It contains the binary, CA certs, timezone data, and nothing else — no shell, no package manager, no Go toolchain. Distroless images are immutable from a debugging perspective; if you need to poke around, deploy a separate debug image side-by-side, don’t bake busybox into production.
Two non-obvious points:
--mount=type=cache,target=/go/pkg/modcaches the module download across builds. On a CI build that warms the cache, a 30-secondgo mod downloadbecomes a 2-second hit. Without this, every build downloads everything again.-trimpathis required if you want reproducible builds across machines. Without it, the binary embeds the build host’s file paths, which break content-addressable image hashes and confuse stack traces in alerts.
Pattern B — prebuilt in CI, scratch final image:
CI builds the binary and uploads it to artifact storage; the Dockerfile just copies it in.
# Dockerfile.scratch — built by CI after `go build` produced ./bin/api
FROM scratch
COPY --from=ca-image /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY bin/api /api
USER 1001:1001
EXPOSE 8080
ENTRYPOINT ["/api"]
The corresponding CI step:
# .github/workflows/build.yml (excerpt)
- uses: actions/setup-go@v5
with:
go-version: '1.23'
- name: Restore Go cache
uses: actions/cache@v4
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
- name: Build
env:
CGO_ENABLED: 0
GOOS: linux
GOARCH: amd64
run: |
go build -trimpath \
-ldflags "-w -s -X main.version=${{ github.sha }}" \
-o bin/api ./cmd/api
- name: Test the binary boots
run: ./bin/api --version
- name: Build and push image
uses: docker/build-push-action@v5
with:
context: .
file: Dockerfile.scratch
push: true
tags: registry.example.com/api:${{ github.sha }}
The image is ~15 MiB. Push and pull are 5-10× faster than Pattern A’s already-small distroless build. The trade is that everything happens in CI — if CI is broken, you can’t ship, full stop. Pattern A still works locally with docker build.
Pattern C — alpine, for teams that need a shell in production:
FROM golang:1.23-alpine AS builder
RUN apk add --no-cache git ca-certificates
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -trimpath -ldflags '-w -s' -o /out/api ./cmd/api
FROM alpine:3.20
RUN apk add --no-cache ca-certificates curl tini tzdata && \
addgroup -g 1001 -S app && \
adduser -u 1001 -S app -G app
COPY --from=builder /out/api /app/api
USER app
EXPOSE 8080
HEALTHCHECK --interval=10s --timeout=2s --start-period=10s \
CMD curl -fsS http://127.0.0.1:8080/healthz || exit 1
ENTRYPOINT ["/sbin/tini", "--", "/app/api"]
~50 MiB. You get a shell, basic networking tools, and apk add if you really need to install something for triage. The trade against distroless is a larger attack surface and one more package manager to keep patched.
CGO and when it matters
CGO_ENABLED=0 produces a fully static binary that runs anywhere. The cost: you lose access to a few stdlib features that use cgo — most notably net.LookupHost falls back to a pure-Go resolver, which behaves slightly differently for /etc/hosts and nsswitch.conf. For most services this is fine and even preferable (no dynamic library dependencies in production).
If you need cgo (you’re using SQLite via mattn/go-sqlite3, or a C library wrapper), you cannot use scratch or distroless/static. Use distroless/base-debian12 instead — it has glibc.
FROM golang:1.23-bookworm AS builder
RUN apt-get update && apt-get install -y --no-install-recommends build-essential && rm -rf /var/lib/apt/lists/*
WORKDIR /src
COPY . .
RUN CGO_ENABLED=1 go build -trimpath -ldflags '-w -s' -o /out/api ./cmd/api
FROM gcr.io/distroless/base-debian12:nonroot
COPY --from=builder /out/api /app/api
USER nonroot:nonroot
EXPOSE 8080
ENTRYPOINT ["/app/api"]
A pragmatic rule
If your team can answer “yes” to “do we have a separate debug image we deploy when we need a shell,” use Pattern A or B. If the answer is “we just exec into the running container with apk add bash,” use Pattern C and don’t pretend otherwise — distroless is a real shift in operational practice, not just a Dockerfile change.
We bake build-cache discipline, reproducible image hashes, and the right base image for the workload into deploys on Cloud PaaS, with image-size and pull-time tracked in our deployment management plane. If your Go images are bigger than they should be, start a deploy review.