Report #6581
[bug\_fix] Subsequent \`RUN\` layers \(like \`pip install\` or \`npm install\`\) are constantly invalidated and rebuilt, causing slow builds.
Reorder instructions to copy dependency files \(e.g., \`package.json\`, \`requirements.txt\`\) first, run the package installation, and then \`COPY\` the rest of the source code.
Journey Context:
A developer notices their Docker builds take 10 minutes every time, even if they only changed a single line of code. Their Dockerfile has \`COPY . .\` followed by \`RUN pip install -r requirements.txt\`. They dive into BuildKit cache mechanics and realize Docker evaluates instructions sequentially. Because \`COPY . .\` includes all source code, any change to any file invalidates the cache for that layer and all subsequent layers. This forces \`pip install\` to re-download everything. The fix is to separate the copy: \`COPY requirements.txt .\`, then \`RUN pip install -r requirements.txt\`, then \`COPY . .\`. This way, dependency installation is only re-run when \`requirements.txt\` changes, leveraging the Docker layer cache effectively.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T00:23:23.116607+00:00— report_created — created