Sub topic 1
Images, layers, and what a base image contributes
Prerequisites
This chapter assumes you can read a Dockerfile (FROM, COPY, RUN, ENTRYPOINT) and run docker build and docker run. You should be able to build a small program in a compiled language such as Go. You should also know that Linux programs can load shared libraries at runtime. The example program is the minimal Go HTTP server used throughout this tutorial. It answers /healthz on port 8080.
What an image is
A container image is an ordered stack of read-only layers plus metadata. The metadata includes the default command, environment variables, and the user to run as. Each layer is a set of file changes, stored as a compressed archive and identified by a hash of its content.
Most Dockerfile instructions that change the filesystem (COPY, ADD, RUN) create one layer. Instructions such as ENTRYPOINT, USER, and EXPOSE only change metadata. When you run the image, the runtime merges the layers into one filesystem and adds a thin writable layer on top.
Layers are cached by content. If an instruction and its inputs are unchanged, Docker reuses the existing layer. Put rarely changing steps first, such as go mod download, and frequently changing steps last, such as copying source code.
What the base image contributes
FROM chooses the first layers. Everything in them ships with your application, whether or not your program uses it. A typical single-stage Go image looks like this:
FROM golang:1.22
WORKDIR /src
COPY . .
RUN go build -o /app .
ENTRYPOINT ["/app"]This image contains your binary, but it also contains the following:
- A full Debian userland, including a shell,
apt, and core utilities. - The Go toolchain and its standard library sources.
- Whatever the build left behind, such as the module cache.
Your program needs none of these at runtime. A compiled binary needs only its own file, plus whatever it loads dynamically. Later chapters cover what that means for linking.
Check the layers of any image you have built with docker history:
docker build -t hello-fat .
docker history hello-fat
docker image ls hello-fatThe history lists each layer with the instruction that created it and its size. The golang base layers are by far the largest entries, while your binary is a few megabytes.
Layers never shrink an image
Layers are additive. Deleting a file in a later layer hides it from the merged view, but the earlier layer still holds the bytes. This RUN rm saves nothing:
COPY big-archive.tar /tmp/
RUN tar -xf /tmp/big-archive.tar -C /opt && rm /tmp/big-archive.tarThe COPY layer still contains the archive, so anyone who pulls the image downloads it. To remove build-time material from the final image, you must never put it in the final image's layers. Multi-stage builds, covered in a later chapter, do exactly that.
Where this leads
You now have the working model: an image is stacked, additive layers, and the base image supplies the bottom of the stack. The next chapter looks at why a large inherited stack costs you in size and in security exposure.
Exercise
You add RUN rm -rf /root/.cache/go-build as the last step of a single-stage Dockerfile whose earlier RUN go build step created that cache. Does the image get smaller, and why?
Show answer
No. The build step's layer still contains the cache files. The later rm only adds a layer that hides them from the merged filesystem, so the image keeps the original bytes and is slightly larger.