The Docker build cache: why one edit rebuilds everything after it
By Christophe Paka · Facts checked on 7 November 2026
You change one line of code and run docker build. It takes minutes. You reinstalled nothing on purpose, but Docker downloads your dependencies all over again.
The docker build cache explains it. Docker reuses each step until the first one that changed, and then it reruns that step and every step after it. The figure below builds the same five steps twice, so you can see exactly where the cascade starts.

Use this diagram on your site
Free to share in your blog post, docs, course or slides, as long as the credit link stays. Paste the code below where the figure should go.
How to read this figure
Build 1: nothing changed. The left column is a build with no edits. All five steps say cached, and the build finishes in about a second. Docker kept every finished step from the last build.
Build 2: steps 1 and 2 still hit. On the right you edited
main.c.FROMand theRUN apt-getstep are still cached. RUN matches on its command text, and the command string did not change.Find the first miss. Step 3,
COPY main.c Makefile /src/, is the first red box. COPY matches on a checksum of the files' content, and the content changed. This is the cache invalidation point.Follow the arrow down. Steps 4 and 5 are rebuilt because they sit downstream of the change. They would build nothing differently, but they still rerun. The cache is a prefix match: hits from the top, then one miss, then everything executes.
Use the rule on your own Dockerfile. Put steps that rarely change near the top and steps that change often near the end. Then an edit lands late and little reruns. To shrink the image as well, see the multi stage build.
Key takeaways
- In a build log, the first step not marked CACHED is where your edit landed.
- Copy the dependency list and install it before you copy your code, so a code edit never reinstalls Flask.
- Saving a file without changing it busts nothing; only content and permissions count, never the date.
Worked example: four edits to myapp and what reruns
myapp's Dockerfile copies requirements.txt, runs pip install, then copies app.py. Here is what each action does to the cache.
- You edit
app.py. FROM, WORKDIR, COPY requirements.txt and RUN pip install are all cached. COPY app.py and every later step rerun. The cost is seconds. - You change
flask==3.1.3inrequirements.txt. COPY requirements.txt misses, so the pip install and everything after it rerun. It takes longer, because Flask downloads again. - You put
COPY . .beforeRUN pip install. Every code edit changes that one COPY, so every edit reinstalls Flask. This is the failure case. - You run
touch app.pywithout changing it. Nothing reruns, because the modification time is not part of the checksum.
Result: On Docker's sample app, the first build took 20.0 seconds, an unchanged rebuild about 1.0 second, and a code edit 1.2 seconds once the dependency step came first.
Questions readers ask
Why does Docker rebuild everything after a small change?
The cache matches from the top. The first changed step reruns, and so does every step after it, even if they would build nothing differently. Put slow, stable steps first and your code last.
Does touch or saving a file unchanged bust the cache?
No. COPY and ADD match on a checksum of the files' content and permissions, never their date. Saving a file unchanged busts nothing; changing its content does.
How do I force fresh packages in a Docker build?
RUN matches on its command text, so pip install keeps last month's download until the text changes. Run docker build --no-cache to rerun every step, and add --pull to fetch a newer base image.
Why is my image still big after I deleted a file in a later step?
Deleting a file in a later step never shrinks an image, because the file still sits in the lower layer. A multi stage build is the real fix.
Sources: Build cache overview (Docker docs); Build cache invalidation (Docker docs); Optimize cache usage (Docker docs).
This figure is from Docker Made Crystal Clear, chapter 6.
The chapter builds the idea step by step, with recipes you can follow, a Try it to check yourself, and every source.
Get the book


