Sub topic 8
Layers and the build cache
This chapter assumes you can write a basic Dockerfile and run docker build. You should also know what FROM, RUN, and COPY do. Here you will learn how Docker turns instructions into layers and how it decides whether to reuse them.
How instructions become layers
Each RUN and COPY instruction (and ADD) creates a new image layer. A layer is a stored set of filesystem changes. The final image is the base image's layers with your layers stacked on top. Instructions such as CMD and ENTRYPOINT only add metadata, so they add no filesystem content.
Docker keeps each layer after a build. On the next build, it checks whether it can reuse a stored layer instead of running the instruction again. This is the build cache.
The cache rules
For each instruction, Docker applies these checks in order:
- For
RUN, it compares the instruction text with a cached layer built from the same parent layer. Docker does not check whether the command's result would change. ARUN apt-get updateis reused even if the package index has changed since. - For
COPY, it compares the checksums of the files being copied, with the instruction text. Changing a file's content invalidates the cache. Changing only its modification time does not. - Once one instruction misses the cache, every later instruction runs again. A changed layer changes the parent of all layers after it.
The third rule is the one that matters most when you order instructions.
Ordering for cache reuse
Dependencies change rarely. Source code changes constantly. Put the slow, stable steps first and the fast, volatile steps last:
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]Only requirements.txt is copied before the install. When you edit app.py, the first COPY still matches its checksum, so the pip install layer is reused. Only COPY . . and later steps run again. If you wrote COPY . . first, any source edit would force a full reinstall.
You can see the effect in the build output. Cached steps are marked CACHED:
docker build -t myapp .
# edit app.py, then build again
docker build -t myapp .On the second build, the pip install step reports CACHED, and only the final copy runs again.
Common errors
- Stale package indexes. Putting
RUN apt-get updateon its own line lets Docker cache an old index. A laterRUN apt-get installmay then fail or install old versions. Combine them in oneRUNinstead, for exampleRUN apt-get update && apt-get install -y curl. - Cleanup in a later layer. Deleting files in a separate
RUNdoes not shrink the image, because the earlier layer still holds them. Delete in the sameRUNthat created the files. - A noisy build context. Files such as
.gitor logs copied byCOPY . .change often and invalidate the cache. A.dockerignorefile keeps them out of the context. - Unwanted cache hits. To force every step to run again, use
docker build --no-cache.
Exercise
A Dockerfile has COPY . . on line 3 and RUN pip install -r requirements.txt on line 4. You change one line in a source file and rebuild. Is the pip install layer reused? How would you change the Dockerfile so it is?
Show answer
No. The COPY . . layer changed because a file's content changed, so every later instruction, including pip install, runs again. Add COPY requirements.txt . and the RUN pip install before COPY . .. Then a source edit only invalidates the final copy, and the install layer stays cached until requirements.txt changes.