BUILDKIT_CACHE_MOUNT_NOT_FOUND cicd build_error ai_generated true

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

Also available as: JSON · Markdown · 中文
90%Fix Rate
83%Confidence
1Evidence
2024-06-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Docker 25.0 active
BuildKit 0.12.0 active

Root Cause

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.

generic

中文

Docker BuildKit缓存挂载目标目录在构建上下文或基础镜像中不存在,导致在Dockerfile中使用`--mount=type=cache,target=/path`时校验和计算失败。

Official Documentation

https://docs.docker.com/build/cache/

Workarounds

  1. 95% success 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`
    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`
  2. 85% success 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
    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

中文步骤

  1. 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`
  2. 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

Dead Ends

Common approaches that don't work:

  1. 70% fail

    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

  2. 50% fail

    Disabling BuildKit removes cache mount support entirely, and the legacy builder may not support the same Dockerfile syntax, causing a different error