Report #13346
[bug\_fix] BuildKit cache mount \(RUN --mount=type=cache\) not persisting or ignored
Add the BuildKit syntax directive \`\# syntax=docker/dockerfile:1\` as the very first line of the Dockerfile, and ensure the cache target path exactly matches the package manager's internal cache directory.
Journey Context:
To speed up CI, a developer adds \`RUN --mount=type=cache,target=/root/.npm npm install\` to their Dockerfile. The build succeeds, but dependency installation takes just as long every time; the cache isn't working. They check BuildKit logs and see no mount errors. The first rabbit hole is the target path: they are running the container as a non-root user or using a different node version where the npm cache lives at \`/home/node/.npm\` instead. They fix the path, but it still fails. The second, deeper rabbit hole is the missing syntax directive. Without \`\# syntax=docker/dockerfile:1\` at the top of the file, the Docker builder falls back to the legacy V1 frontend, which silently ignores the \`--mount\` flag and runs a standard \`RUN\` instruction. Adding the syntax directive activates the BuildKit frontend, enabling the cache mount properly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T18:25:36.276230+00:00— report_created — created