Compare build and runtime image sizes
A small example
FROM busybox:1.37 AS fullRUN dd if=/dev/zero of=/build-only.bin bs=1024 count=1024 && printf "release ready\n" > /report.txtCMD ["cat", "/report.txt"]FROM busybox:1.37 AS runtimeCOPY --from=full /report.txt /report.txtCMD ["cat", "/report.txt"]The full target contains a synthetic one-MiB build-only file as well as the report. The runtime target takes only the report. Build both on the same builder and native platform; compare engine-reported image sizes in bytes using inspect. Shared layers mean the sum of image sizes is not the physical disk space reclaimed by deleting one tag. A zero-filled file compresses well, so registry transfer savings cannot be inferred directly. Record the base digest, engine version, platform, commands, outputs and sizes. If performance matters, measure it separately under identical workload conditions. Identify each size by image ID or tag and platform. Check that the full variant actually contains the synthetic file, and the runtime variant does not, before reporting the difference.
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/06-size-comparison/ in the extracted archive. The Dockerfile and these files are supplied. Work on a disposable copy, not the original archive.
docker build --load --network none --progress=plain -t bm-v10-size-comparison:lab .docker run --rm --pull=never --network none bm-v10-size-comparison:labdocker build --load --network none --target full -t bm-v10-full:lab .docker run --rm --pull=never --network none bm-v10-full:labdocker run --rm --pull=never --network none bm-v10-full:lab sh -c "test -s /build-only.bin"docker image inspect --format "{{.Id}} {{.Size}}" bm-v10-full:lab bm-v10-size-comparison:labdocker run --rm --pull=never --network none bm-v10-size-comparison:lab sh -c "test ! -e /build-only.bin"docker image rm bm-v10-full:labBoth runs should print release ready; the runtime variant should lack the build-only file. Record the two actual sizes, subtract runtime from full and report units; do not assume any measured result in advance.
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 report an invented percentage reduction or assume image size equals download size, cache usage or runtime memory. Verify behavior before comparing.
Manual practice
Prepare a table with columns for target, expected output, actual output, bytes, platform and digest. Fill actual columns only after an approved run. If Docker is unavailable, explain the expected direction without claiming measurements.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Compare build and runtime image sizes — Knowledge check
Apply the multi-stage concept to a new scenario.
What must be verified before comparing variants?
Why might transfer savings differ from inspect size differences?
What is a valid result when Docker is unavailable?