Report #11875
[bug\_fix] RUN pip install / npm install / bundle install re-executes on every build even when dependencies haven't changed
Reorder the Dockerfile to COPY only dependency manifest files \(requirements.txt, package.json, package-lock.json, Gemfile, etc.\) before the RUN install step, then COPY the remaining source code after. This ensures the install layer's cache key only changes when dependency manifests change, not when application source code changes.
Journey Context:
A developer notices their Docker builds are slow — every time they change a single line of application code, the entire dependency installation step re-runs, taking 5\+ minutes. They check that BuildKit is enabled and layer caching works for other layers. They examine the Dockerfile and find COPY . . followed by RUN pip install -r requirements.txt. The problem is that COPY . . copies all source files, changing the layer's content hash on every code edit, which invalidates the cache for all subsequent layers including the pip install. The fix is the well-known Dockerfile pattern: COPY requirements.txt first, RUN pip install, then COPY the rest of the source. This way, the pip install layer's cache key depends only on requirements.txt, which changes infrequently. The developer restructures the Dockerfile and confirms that subsequent code-only changes no longer invalidate the dependency installation layer.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T14:26:23.415222+00:00— report_created — created