Report #72338
[bug\_fix] Docker layer cache is invalidated on every build despite no changes to the dependency files \(e.g., package.json\).
Ensure the COPY instruction for dependency files \(like package.json or requirements.txt\) is placed BEFORE the COPY instruction for the rest of the source code. This prevents source code changes from invalidating the dependency installation cache layer.
Journey Context:
A developer notices their Docker builds take 10 minutes every time, even though they only changed a single line of application code. They check their Dockerfile and see they have COPY . /app followed by RUN pip install -r requirements.txt. They fall into a rabbit hole investigating BuildKit cache garbage collection and registry caching. They eventually realize the fundamental rule of Docker layer caching: if a layer changes, all subsequent layers are rebuilt. Because COPY . /app includes all source code, any code change invalidates the COPY layer, which in turn invalidates the RUN pip install layer, forcing a full dependency re-download. They fix it by splitting the COPY: first COPY requirements.txt /app/, then RUN pip install, and finally COPY . /app. Now, source code changes only invalidate the final COPY layer, and the heavy pip install layer is cached.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T04:00:03.893637+00:00— report_created — created