解决失败:无法计算缓存键:无法计算引用'cache-mount-target'的校验和:未找到
failed to solve: failed to compute cache key: failed to calculate checksum of ref 'cache-mount-target': not found
ID: cicd/docker-buildkit-cache-mount
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Docker 25.0 | active | — | — | — |
| BuildKit 0.12.0 | active | — | — | — |
根因分析
Docker BuildKit缓存挂载目标目录在构建上下文或基础镜像中不存在,导致在Dockerfile中使用`--mount=type=cache,target=/path`时校验和计算失败。
English
Docker BuildKit cache mount target directory does not exist in the build context or base image, causing checksum calculation to fail when using `--mount=type=cache,target=/path` in a Dockerfile.
官方文档
https://docs.docker.com/build/cache/解决方案
-
Create the target directory in the Dockerfile before the cache mount is used: add `RUN mkdir -p /var/cache/apt` before the RUN instruction that uses `--mount=type=cache,target=/var/cache/apt`
-
Use a different base image that already contains the cache directory, or ensure the directory is included in the build context by creating it locally before build
无效尝试
常见但无效的做法:
-
70% 失败
Removing the cache mount will break the build if the cache is required for package downloads (e.g., apt, npm); the build may fail due to network timeouts or missing dependencies
-
50% 失败
Disabling BuildKit removes cache mount support entirely, and the legacy builder may not support the same Dockerfile syntax, causing a different error