Sub topic 2
Layers: how images are stacked
This chapter assumes you can run containers with docker run, read a basic Dockerfile, and know that an image is the read-only template a container starts from. It covers how layers stack, how copy-on-write works, and the mistakes that bloat images.
What a layer is
An image is an ordered stack of layers. Each layer is a set of filesystem changes: files added, modified, or deleted relative to the layer below it. Layers are read-only. When Docker starts a container, it mounts the layers together as one filesystem and adds a thin writable layer on top.
Layers are shared. If two images start from the same base, Docker stores the base layers once on disk and downloads them once from a registry.
Which instructions create layers
In a Dockerfile, RUN, COPY, and ADD create filesystem layers. Instructions such as ENV, WORKDIR, and ENTRYPOINT only change image metadata. You can view the layers of an image with docker history:
docker history myapp:1.0Each row shows the instruction that produced the layer and its size. Rows with a size of 0B are metadata-only.
Copy-on-write
When a container modifies a file that lives in a lower layer, the storage driver copies the file up into the writable layer and changes the copy. The original stays untouched, so other containers using the same image are not affected. Reading an unchanged file costs nothing extra. Writing to a large file for the first time costs a full copy.
Deleting a file works differently. The writable layer records a marker that hides the file, and the data remains in the lower layer.
Layers never shrink
Because a lower layer is never edited, deleting a file in a later layer does not reduce the image size. The data is still stored below. This is the most common cause of bloated images.
# Wrong: the 500 MB archive stays in the first RUN layer
FROM debian:12-slim
RUN apt-get update && apt-get install -y curl ca-certificates
RUN curl -o /tmp/data.tar.gz https://example.com/data.tar.gz
RUN tar -xzf /tmp/data.tar.gz -C /opt && rm /tmp/data.tar.gz# Better: download, extract, and delete in the same layer
FROM debian:12-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl ca-certificates \
&& rm -rf /var/lib/apt/lists/* \
&& curl -o /tmp/data.tar.gz https://example.com/data.tar.gz \
&& tar -xzf /tmp/data.tar.gz -C /opt \
&& rm /tmp/data.tar.gzIn the second version the archive never gets committed to a layer, so it takes no space.
Common errors
- Deleting files in a separate
RUNand expecting a smaller image. - Running
apt-get updatein its ownRUN, which can leave stale package lists cached. - Changing file ownership with a separate
RUN chown, which duplicates every file in a new layer. - Counting layers instead of bytes. Many small layers are fine. Large duplicated content is the problem.
Exercise
A Dockerfile runs COPY big.iso /data/ in one instruction and RUN rm /data/big.iso in the next. Does the final image get smaller than it would without the rm? Why or why not?
Show answer
No. The COPY layer already contains big.iso, and layers are read-only. The rm adds a layer that only hides the file, so the image keeps the full size. To avoid it, never copy the file in, or fetch and remove it within a single RUN.