Sub topic 6
Shipping a binary on scratch
This chapter assumes you can write a basic Dockerfile and run docker build and docker run. It also assumes you know what a Linux shared library is, and that you have a statically linked binary from the previous chapter. We ship that binary on scratch.
What scratch is
scratch is a reserved, empty base image. It has no files, no shell, no package manager, and no libc. The final image contains only what you COPY into it, so a static Go binary yields an image about the size of the binary.
scratch is not something you pull. FROM scratch tells Docker to start with an empty filesystem.
The program
The example is a small Go HTTP server with a health endpoint.
package main
import (
"log"
"net/http"
)
func main() {
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
log.Fatal(http.ListenAndServe(":8080", nil))
}Confirm the binary is static
A binary on scratch cannot load shared libraries, because none exist. Build with cgo disabled and check the result.
CGO_ENABLED=0 go build -o app .
file app
# expected to report a statically linked executable
CGO_ENABLED=1 go build -o app-cgo . && ldd app-cgoThe file output for app should say the executable is statically linked. The cgo build lists shared libraries under ldd. That build would fail on scratch.
The multi-stage Dockerfile
FROM golang:1.22 AS build
WORKDIR /src
COPY go.mod ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/app .
FROM scratch
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=build /out/app /app
USER 65532:65532
EXPOSE 8080
ENTRYPOINT ["/app"]The first stage compiles. The second stage starts empty and receives only the pieces the program needs. Each part has a purpose:
| Line | Purpose |
|---|---|
| `COPY go.mod ./` then `RUN go mod download` | Caches dependencies in their own layer, so source edits do not trigger a re-download. |
| `-trimpath` | Removes local build paths from the binary. |
| `-ldflags="-s -w"` | Strips the symbol table and debug information to shrink the file. |
| `COPY --from=build ... ca-certificates.crt` | Lets the program verify HTTPS servers when it makes outbound calls. |
| `USER 65532:65532` | Runs as a numeric non-root user. |
| `ENTRYPOINT ["/app"]` | Exec form, which scratch requires. |
The /healthz server makes no outbound TLS calls, so the certificate bundle is not needed to run it. It is included because most real services call other HTTPS endpoints. The next chapter covers what else scratch leaves out.
Build and run
docker build -t app:scratch .
docker run --rm -p 8080:8080 app:scratchIn a second terminal, run curl -i localhost:8080/healthz. A 200 OK response means the binary started and served a request.
Common errors
exec /app: no such file or directory: the binary is dynamically linked, so the loader it names is missing. Rebuild withCGO_ENABLED=0. The message names/app, but the missing file is the loader.exec format error: the binary targets a different OS or CPU architecture than the host running the container. SetGOOSandGOARCHto match./bin/sh: no such fileor a failure with shell-formCMD: scratch has no shell. Use exec form, with a JSON array.- TLS errors such as
x509: certificate signed by unknown authority: the CA bundle was not copied.
Exercise
You build a Go program with CGO_ENABLED=1 and run it on FROM scratch. The container exits with exec /app: no such file or directory, though /app exists in the image. What is the cause, and what is the fix?
Show answer
The binary is dynamically linked, so the kernel looks for its dynamic loader (and glibc), which scratch does not contain. The error refers to the missing loader, not to /app. Rebuild with CGO_ENABLED=0 to produce a static binary, or run the cgo binary on an image that supplies glibc, such as distroless base.