Report #61257
[bug\_fix] Docker image rebuilds dependencies from scratch on every code change, completely ignoring the cache and taking 10\+ minutes
Reorder the Dockerfile instructions: copy only the dependency manifests \(package.json, requirements.txt\) first, run the dependency installation step, and then copy the rest of the source code.
Journey Context:
A developer notices their CI pipeline takes forever. They inspect the build logs and see 'RUN npm install' or 'RUN pip install' downloading and compiling packages on every single push, even if only a single line of application code changed. They try tweaking .dockerignore or using --cache-from, missing the core issue. Docker builds images layer by layer, and a layer is rebuilt if its instructions or preceding layers have changed. Because the developer wrote 'COPY . /app' before 'RUN npm install', any change to any source file invalidates the COPY layer, which in turn invalidates the npm install layer, busting the cache. By splitting the copy into two steps—copying only package.json and package-lock.json first, then running the install, and finally copying the source—the expensive dependency installation layer is only invalidated when the dependencies themselves change.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T09:18:10.159165+00:00— report_created — created