Report #15779
[bug\_fix] ERROR: failed to solve: cannot mount volume at /app/node\_modules: directory exists
Ensure the target path of the \`--mount=type=cache\` does not contain pre-existing files from a previous \`COPY\` or \`RUN\` instruction in the same build stage, or move the cache mount target to a dedicated subdirectory.
Journey Context:
A developer tries to optimize a Node.js Docker build by caching \`node\_modules\` using \`RUN --mount=type=cache,target=/app/node\_modules npm install\`. The build immediately fails with a cryptic overlayfs error about the directory existing. They go down a rabbit hole checking base image layers and trying different base images. The root cause is a conflict with the overlay filesystem. Earlier in the Dockerfile, they ran \`COPY . /app\`, which copied their local \(perhaps empty or partial\) \`node\_modules\` folder into the layer. When BuildKit attempts to mount the cache volume over \`/app/node\_modules\`, it fails because overlayfs cannot mount a volume over a non-empty directory that was created in a previous layer of the same stage. The fix is to restructure the Dockerfile: either copy the application code \*after\* the cache mount/run install step, or ensure the local context does not contain a \`node\_modules\` directory \(by adding it to \`.dockerignore\`\), or mount the cache to a different path \(like \`/root/.npm\`\) and copy it into place.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T00:56:37.854328+00:00— report_created — created