Sub topic 10
Example: a static Go binary in a minimal image
This chapter builds a small Go service into a minimal image. It assumes you know how multi-stage builds work (FROM ... AS, COPY --from), can read a Dockerfile, and have Docker with BuildKit installed. You do not need to know Go well.
Why Go suits a minimal image
Go compiles to a single executable. If the binary is statically linked, it needs no shared libraries from the operating system. That means the final image can hold the binary and nothing else.
Go links statically only when it does not use C code. Setting CGO_ENABLED=0 turns off cgo and forces a pure Go build. Without it, some standard library packages, such as net, may link against the C library. The binary then fails on an empty image with an error like exec /app: no such file or directory. The file exists. The loader it needs does not.
A small program to build
Save this as main.go. It uses only the standard library.
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "hello from a small image")
})
log.Println("listening on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}Create the module files once:
go mod init example.com/helloThis writes go.mod. Because the program has no dependencies, no go.sum is created. The Dockerfile below copies go.sum, so create an empty one with touch go.sum or remove it from the COPY line.
The Dockerfile
FROM golang:1.22 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /out/app .
FROM scratch
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]Each part has a job:
COPY go.mod go.sum ./followed bygo mod downloadfetches dependencies before the source is copied. Source edits then reuse the cached download layer.CGO_ENABLED=0produces a statically linked binary.-ldflags="-s -w"strips the symbol table and debug information. This usually shrinks the binary by a quarter to a third.FROM scratchstarts from an empty image with no shell, no package manager and no files.ENTRYPOINT ["/app"]uses the exec form, which is required here because there is no shell to interpret a string command.
Build and compare
docker build -t hello:small .
docker build --target build -t hello:build .
docker image ls helloThe build image is typically over 800 MB because it contains the whole Go toolchain. The small image is usually a few megabytes, close to the size of the binary itself. Run it with:
docker run --rm -p 8080:8080 hello:smallWhat scratch leaves out
An empty image has nothing you did not put there. Check these before choosing scratch:
| Missing item | Symptom | Fix |
|---|---|---|
| CA certificates | `x509: certificate signed by unknown authority` on HTTPS calls | `COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/` |
| Time zone data | `unknown time zone` errors | Copy `/usr/local/go/lib/time/zoneinfo.zip`, or import `time/tzdata` in Go |
| Shell | `docker exec ... sh` fails | Use a distroless or Alpine base if you need debugging |
| Non-root user | Process runs as root | Add `USER 65532:65532` (a numeric ID works without `/etc/passwd`) |
Exercise
Your Go service builds fine, but the container starts and exits with exec /app: no such file or directory, even though /app was copied into the scratch image. What is the likely cause, and what change fixes it?
Show answer
The binary is dynamically linked, probably because cgo was enabled, so it needs a loader and C library that do not exist in scratch. Set CGO_ENABLED=0 in the build command to produce a static binary.
Exercise
The service now starts, but calls to an HTTPS API fail with x509: certificate signed by unknown authority. How do you fix this without leaving scratch?
Show answer
Copy a CA bundle from the build stage into the final image, for example COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/.