3.3.2.4 Go Runtime Images
A focused guide to Go Runtime Images, connecting core concepts with practical Docker and container operations.
Go runtime images provide a base for building and running Go applications, though Go's ability to produce statically linked binaries means the "runtime image" concept looks different than for languages like Python or Node — the build environment and the final running environment are often entirely different, minimal images.
Building With a Full Go Image
The official Go image includes the complete toolchain needed to compile Go source code, used during the build stage of a multi-stage build rather than as the basis for the final running container.
FROM golang:1.22 AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o app .
Running From a Minimal or Empty Image
Because a statically compiled Go binary needs no separate runtime installed, the final stage can use an extremely minimal base, or no base at all, since the binary itself does not depend on anything the image would otherwise provide.
FROM scratch
COPY --from=build /src/app /app
ENTRYPOINT ["/app"]
This produces an image containing essentially nothing but the compiled binary, since Go's static linking removes the need for a traditional runtime environment.
When a Minimal OS Base Is Still Useful
Even with a statically linked binary, a minimal base such as Alpine or Debian-slim is sometimes preferred over scratch specifically to retain basic utilities like CA certificates or a shell for debugging, at a small cost in image size.
FROM alpine:3.19
RUN apk add --no-cache ca-certificates
COPY --from=build /src/app /app
ENTRYPOINT ["/app"]
Including CA certificates is commonly necessary if the application needs to make outbound HTTPS requests, since scratch provides no certificate store at all.
Why Go's Approach to Runtime Images Differs
Go's static linking model means the traditional notion of a "runtime image" providing an interpreter or virtual machine does not really apply — instead, the final image's job is simply to provide whatever minimal supporting files (certificates, timezone data) the binary needs, if anything at all.
docker images myapp
Why This Matters
Understanding that Go applications can be packaged into dramatically smaller final images than interpreted-language applications is a direct consequence of static linking, and it is one of the most commonly cited practical advantages of choosing Go for containerized services where minimal image size matters.