错误:无法求解:无法计算缓存键:无法计算引用 moby::abc123 的校验和:"/some/path":未找到
ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref moby::abc123: "/some/path": not found
ID: cicd/docker-buildkit-cache-mount-error
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Docker 24.0.5 | active | — | — | — |
| Docker 25.0.0 | active | — | — | — |
| BuildKit v0.12.0 | active | — | — | — |
根因分析
Docker BuildKit 缓存挂载目标路径不存在或无效,通常由于构建上下文中缺少目录或 --mount=type=cache,target= 路径拼写错误。
English
Docker BuildKit cache mount target path does not exist or is invalid, often due to a missing directory in the build context or a typo in the --mount=type=cache,target= path.
官方文档
https://docs.docker.com/build/cache/解决方案
-
Ensure the cache mount target directory exists in the Dockerfile by adding a RUN mkdir -p /some/path before the cache mount instruction.
-
Use absolute paths and avoid symlinks: change --mount=type=cache,target=/var/cache/apt to a unique path like /cache/apt and ensure no trailing spaces.
无效尝试
常见但无效的做法:
-
90% 失败
Running docker system prune -a removes all cache but doesn't fix the underlying path issue; cache mounts still fail.
-
80% 失败
Adding --no-cache flag forces rebuild but doesn't address the missing directory; error persists on next build.
-
50% 失败
Changing the cache mount from type=cache to type=bind may work but defeats caching purpose and can cause permission issues.