Report #960
[bug\_fix] Build layer cache invalidates on every build even though source did not change
Remove non-deterministic inputs from RUN commands \(timestamps, \`$\(date\)\`, git commit hashes injected at build time\) and put dependency files before code so that \`COPY requirements.txt\` and \`RUN pip install\` layers cache independently. Use \`BUILDKIT\_INLINE\_CACHE=1\` with \`--cache-from\` for remote caching.
Journey Context:
Your CI builds the same Dockerfile every night and reinstalls dependencies every time. You inspect the build logs and see \`RUN pip install -r requirements.txt\` is not marked \`CACHED\`. The Dockerfile looks stable: it copies requirements then installs. Then you notice the preceding \`COPY . /app\` layer always changes because the source tree contains a generated \`version.py\` updated with the current timestamp on every checkout. Since COPY . invalidates the cache for everything after it, the pip install re-runs. You split the build so only \`requirements.txt\` is copied before \`pip install\`, move the timestamped file generation to a later layer, and the dependency layer now caches reliably. The fix works because Docker hashes the contents and metadata of inputs to each instruction; any change in an earlier layer invalidates all later layers.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-13T15:53:43.552013+00:00— report_created — created