Report #14618
[bug\_fix] BuildKit cache invalidated on every build despite no code changes \(CACHED never appears for COPY . .\)
Add a \`.dockerignore\` file to exclude volatile files \(like \`.git\`, local logs, \`.env\`\), and restructure the Dockerfile to copy dependency files \(\`package.json\`, \`requirements.txt\`\) and run installs BEFORE copying the rest of the source code.
Journey Context:
A developer notices their CI builds take 10 minutes every time, even if they only changed a single line of HTML. Looking at the BuildKit logs, they see \`COPY . .\` never resolves to \`CACHED\`. They assume BuildKit cache is broken or the CI runner isn't persisting caches. They spend days configuring cache export/import blocks. The real issue is that Docker computes the cache key for \`COPY\` based on the checksums of all files in the context. If any file changes—even an untracked local \`.env\` file, a \`.git\` directory, or a log file—the checksum changes, invalidating the cache for that layer and all subsequent layers. By using \`.dockerignore\` to strip out non-essential files, and splitting the copy step so that \`package.json\` is copied and installed first, the expensive dependency installation layer remains cached until an actual dependency changes.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T21:56:45.819404+00:00— report_created — created