Report #62654
[bug\_fix] Docker layer cache invalidated on every build, causing dependency installation to re-run
Reorder Dockerfile instructions: copy dependency manifests \(package.json, requirements.txt\) first, run the dependency installation, and then copy the rest of the source code.
Journey Context:
A developer notices CI builds take 10 minutes every time, even if they only changed a single line of frontend code. They check the build logs and see 'CACHED' for the base image, but the 'npm install' or 'pip install' step is always running fresh. They initially blame the CI system's Docker cache mechanism or BuildKit cache. They try running the build locally and notice the same behavior. They trace the order of operations in the Dockerfile: COPY . . copies all source code, followed by RUN npm install. Because Docker creates a layer checksum based on the files copied, any changed file in the source code invalidates the COPY . . layer and all subsequent layers. Since source code changes frequently, npm install is forced to re-run every time. By splitting the copy into two steps—first copying only package.json and package-lock.json, then running npm install, and finally copying the rest of the app—the dependency installation layer is only invalidated when the dependencies themselves change, restoring the cache hit.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T11:39:01.922738+00:00— report_created — created