Build layers and image size
A small example
FROM busybox:1.37RUN dd if=/dev/zero of=/tmp/demo.bin bs=1024 count=64RUN rm /tmp/demo.binCMD ["sh", "-c", "test ! -e /tmp/demo.bin && echo absent"]Think of two transparent sheets: the first adds a temporary file, the second marks it deleted. The merged view lacks the file, but the earlier change still exists. This fixture deliberately writes only 64 KiB of zeros, so compression may make the registry size quite different from uncompressed size. A better recipe creates and removes a temporary file in the same RUN. Metadata-only instructions are not necessarily filesystem layers. Image history is useful for comparison but does not enumerate every file. A layer history describes instructions and size information, not a proof of which bytes survive. Creating and deleting a file in one RUN avoids committing that temporary file if no other operation persists it.
Try it in a local lab
Manual lab, not a ByteMind executable exercise. Use a new approved disposable Linux VM with blocked network egress and a local Engine and Buildx docker driver. Check docker context show, docker version, docker buildx version and docker buildx ls before doing anything. Confirm the reviewed, preloaded busybox:1.37 for the native platform, expected default user, no entrypoint, required applets and bundled frontend support for RUN mounts. Record its actual digest and versions. If a dependency needs to be fetched or the builder is remote, stop. --network none restricts RUN networking, not all builder traffic. Confirm all bm-v09 tags are unused. Run commands individually in the named fixture directory, stopping on any unexpected failure. All values are synthetic; do not use real credentials. Expected results are predictions, not observed output. 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-builds-and-caching/01-layers/ 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-v09-layers:lab .docker run --rm --pull=never --network none bm-v09-layers:labThe container should print absent. Inspect history for the separate creation and deletion steps; no exact byte count is promised. Compare with a single RUN containing creation and deletion.
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 copy credentials into an image and delete them later: the earlier layer or cache can retain them. Use the secret-mount lesson instead.
Manual practice
Write a second recipe that creates and deletes the same synthetic file in one RUN. Predict the difference and explain why the runtime result is unchanged.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Build layers and image size — Knowledge check
Apply the build concept to a new scenario.
Where may deleted bytes remain?
Which change avoids committing the temporary file?
Does history list every file?