Minimal runtime bases and scratch
A small example
FROM golang:1.26 AS compileWORKDIR /srcCOPY main.go .RUN mkdir /out && CGO_ENABLED=0 GO111MODULE=off go build -trimpath -o /out/greeting main.goFROM scratchCOPY --from=compile /out/greeting /greetingCMD ["/greeting"]This small Go program prints a line and has no external package dependency. The compile command disables cgo for this native Linux fixture, avoiding ordinary C-library linkage for this example; verify the actual artifact before relying on that property. The final stage copies the executable, not the compiler. Dynamically linked programs can require an ELF loader and shared libraries. Alpine normally uses musl, whereas many Linux applications target glibc; they are not interchangeable merely because both run Linux. Some applications also need CA certificates, timezone data or user records. Minimal images can lack a shell or troubleshooting utilities. Select a maintained base according to these requirements rather than size alone. Inspect the compiled ELF architecture and whether it requests a program interpreter or shared libraries in an approved offline environment. The scratch image has no shell; run the executable directly. The example does not set USER, so it is not a nonroot hardening example.
Try it in a local lab
Manual lab only. Use an approved fresh disposable Linux VM with blocked network egress, a local Engine and local Buildx docker driver. Check docker context show, docker version, docker buildx version and docker buildx ls. Confirm reviewed preloaded native busybox:1.37 and, for the scratch example, golang:1.26; verify actual digest, root build user, default entrypoint, required applets/compiler and frontend support. Tags are mutable; record actual identities. Stop if any dependency needs fetching or builder is remote. --network none limits RUN networking, not all builder traffic. Confirm all bm-v10 tags are unused, including checks and full. Execute commands individually in the specified fixture directory and stop on unexpected failure. All expected results are predictions. No secrets, host mounts or registry pushes. Before querying or building, confirm DOCKER_HOST, DOCKER_CONTEXT and BUILDX_BUILDER overrides and the effective endpoint are approved; do not contact an unapproved endpoint. docker build normally uses the Engine default builder, not necessarily the starred Buildx builder. Record the effective local docker driver and platform; review the synthetic context and ignore rules. After each build, require exit status zero before running its tag; an earlier tag may still exist after failure.
Open lab-fixtures/docker-multi-stage-images/04-minimal-runtime/ in the extracted archive. The Dockerfile and these files are supplied. Work on a disposable copy, not the original archive.
package main
import "fmt"
func main() { fmt.Println("ByteMind delivery ready")}docker build --load --network none --progress=plain -t bm-v10-minimal-runtime:lab .docker run --rm --pull=never --network none bm-v10-minimal-runtime:labdocker image inspect --format "{{.Size}}" bm-v10-minimal-runtime:labExpect ByteMind delivery ready. There is no shell installed by this Dockerfile in the scratch stage. The size command reports a value from your engine; no numerical size is predicted.
Confirm the tag belongs to this lab and no containers depend on it. Remove only that tag without forcing. Cached data may remain; dispose of the VM rather than clearing a shared cache. Restore any edited fixture or discard the working copy.
Common mistake
Do not assume that every Go binary is self-contained or that an empty base ensures security. Match architecture, required files and libraries, maintain the binary, and test the final image.
Manual practice
For a hypothetical HTTPS client, list the runtime assets it may need beyond the executable. Compare a maintained general-purpose base, a minimal distribution and scratch; justify one choice.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Minimal runtime bases and scratch — Knowledge check
Apply the multi-stage concept to a new scenario.
What does scratch provide by itself?
Why might an executable fail in a smaller base?
Is smaller always more secure?