Sub topic 1
How images and layers work
An image is a stack of read-only layers plus some metadata. This chapter explains how Docker builds that stack and why it determines image size.
Prerequisites
This chapter assumes you can:
- Use the command line.
- Start containers with
docker run. - Read a simple Dockerfile.
You don't need to write a Dockerfile from scratch yet.
What a layer is
A layer is a record of the filesystem changes made by one build step. Docker stores it as a compressed archive of added, modified and deleted files. Each layer is identified by a digest, which is a hash of its content.
The image is the ordered list of those layers plus configuration such as the default command, environment variables and working directory. When you start a container, Docker stacks the layers into one filesystem view. It adds a thin writable layer on top. Writes made by the running container go there and disappear when the container is removed. The image layers are never changed.
Which instructions create layers
Only instructions that change the filesystem add content:
| Instruction | Effect |
|---|---|
| `FROM` | Brings in the layers of the base image |
| `RUN` | Adds a layer holding whatever the command changed |
| `COPY`, `ADD` | Add a layer holding the copied files |
| `ENV`, `WORKDIR`, `CMD`, `ENTRYPOINT`, `USER`, `EXPOSE` | Change metadata only and add no file content |
Layers only add up
The size of an image is roughly the sum of its layers. A later layer can hide a file from an earlier layer, but it cannot remove the earlier layer's data. When you delete a file, Docker records a marker in the new layer. The container no longer sees the file, but the original bytes are still in the image.
You can see this with a short Dockerfile.
FROM alpine:3.20
RUN dd if=/dev/zero of=/big.bin bs=1M count=100
RUN rm /big.bin
CMD ["ls", "/"]Build it and look at the layers:
docker build -t layers:demo .
docker history layers:demoThe history lists a RUN dd ... layer of about 105 MB. The RUN rm /big.bin layer right above it is close to 0 B. Running the container shows no big.bin, yet the image is over 100 MB larger than plain alpine.
Sharing and reuse
Layers are addressed by content, so Docker stores and transfers each unique layer once. Two images built from the same base share the base layers on disk. When you pull an image, Docker downloads only the layers you don't already have.
This sharing is why a small change near the end of a Dockerfile is cheap to push and pull. Only the changed layers are new. The same content addressing lets Docker reuse layers during a build, which the next chapters cover.
Common mistakes
- Expecting
rmto shrink an image. As shown above, the data stays in the earlier layer. - Assuming metadata instructions cost nothing to think about.
ENVadds no file content, but a value set there is stored in the image configuration. Anyone who pulls the image can read it. - Counting only the final filesystem. The
docker image lssize covers all layers, including data hidden by later layers.
Exercise
A Dockerfile has RUN curl -o /tmp/tool.tar.gz https://example.com/tool.tar.gz in one step and RUN rm /tmp/tool.tar.gz in the next step. Does the final image contain the downloaded archive's bytes, and how would you avoid that without multi-stage builds?
Show answer
Yes. The first RUN layer stores the archive, and the second layer only marks it as deleted. To avoid this, download, use and remove the file in a single RUN command, for example RUN curl -o /tmp/tool.tar.gz ... && tar -xzf /tmp/tool.tar.gz -C /opt && rm /tmp/tool.tar.gz, so no layer keeps the archive.