Goal
Build the same small Go web service twice: first the "easy" way in one stage, then the professional way with a multi-stage build, a pinned base image, a non-root user, labels and a health check. You measure the size, the number of packages and the CVEs of both images, so you can show the difference in numbers.
Before you start
docker versionworks. Optional: install Trivy (trivy --version) for the scan in task 6.- Make the folder
~/img-labwith two files:main.gopackage main import ( "fmt" "net/http" "os" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "hello from", os.Getenv("HOSTNAME")) }) http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("ok")) }) http.ListenAndServe(":8080", nil) }go.modmodule imglab go 1.23
Tasks
Tick a task when it is done. Only your browser remembers it.
- Write
Dockerfile.fat: one stageFROM golang:1.23, copy the code,go build, run the binary. Build it asimglab:fat. - Write
Dockerfile: a build stage (golang:1.23-alpine) that makes a static binary (CGO_ENABLED=0,-ldflags="-s -w"), and a final stageFROM gcr.io/distroless/static-debian12:nonrootthat copies only the binary. Build it asimglab:slim. - Pin the final base image by digest, add OCI labels (
org.opencontainers.image.source,.version) andUSER nonroot:nonroot. - Compare the sizes of both images.
- Run the slim image with a read-only file system, no capabilities and no new privileges. Test
/healthz. - Scan both images with Trivy and compare the number of HIGH and CRITICAL findings.
- Add a
.dockerignoreso.gitand local files never go into the build context.
Check your work
docker images imglab
docker inspect imglab:slim --format '{{.Config.User}} {{index .Config.Labels "org.opencontainers.image.version"}}'
curl -s localhost:8080/healthz; echo
docker run --rm --entrypoint sh imglab:slimimglab:fatis about 850–900 MB;imglab:slimis under 20 MB.- The user is
nonroot:nonrootand the version label is shown. /healthzanswersok.- The last command fails: there is no shell in the slim image (smaller attack surface).
- Trivy shows many findings for
fatand (almost) none forslim.
Hints
Show the hints
COPY --from=build /out/app /appcopies only from the build stage.- Get a digest:
docker buildx imagetools inspect gcr.io/distroless/static-debian12:nonroot. - Distroless images have no shell, so a
HEALTHCHECKwithcurldoes not work there; check health from outside (Docker Compose, Kubernetes probes) or build a tiny check into the app. - Safe run flags:
--read-only --cap-drop ALL --security-opt no-new-privileges.
Solution
Show the solution
- The fat image.Dockerfile.fat
FROM golang:1.23 WORKDIR /src COPY . . RUN go build -o /app . CMD ["/app"]docker build -f Dockerfile.fat -t imglab:fat . - The multi-stage image (replace the digest with the one you got).Dockerfile
# syntax=docker/dockerfile:1 FROM golang:1.23-alpine AS build WORKDIR /src COPY go.mod ./ RUN go mod download COPY . . RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/app . FROM gcr.io/distroless/static-debian12:nonroot@sha256:<digest> LABEL org.opencontainers.image.source="https://github.com/s0sta/img-lab" \ org.opencontainers.image.version="1.0.0" COPY --from=build /out/app /app USER nonroot:nonroot EXPOSE 8080 ENTRYPOINT ["/app"].dockerignore.git *.md Dockerfile*docker build -t imglab:slim . - Compare.
docker images imglab --format 'table {{.Tag}}\t{{.Size}}' TAG SIZE slim 9.8MB fat 889MB - Run it locked down and test.
- Scan both.
My notes
No notes yet.