Report #94930
[bug\_fix] Cache key not found or cache not persisting across builds with RUN --mount=type=cache
Explicitly define a consistent id for the cache mount, such as RUN --mount=type=cache,id=pip-cache,target=/root/.cache/pip. Without an explicit ID, BuildKit generates one based on the target path and other factors, which can differ across steps or CI runners, preventing cache reuse.
Journey Context:
To speed up CI, a developer adds RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt. Locally, it seems fine, but in CI the cache is cold on every run, or the cache key is not found. They try pinning the Docker version, assuming a bug. They eventually realize that without an explicit id, BuildKit's internal hash calculation for the cache mount can be volatile across different build steps or when the Dockerfile is modified. By adding a stable id=id=pip-cache, BuildKit explicitly shares the exact same cache volume across different RUN steps and subsequent builds, surviving Dockerfile changes above the mount point.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T17:55:15.630502+00:00— report_created — created