docker build_error ai_generated true

求解失败:计算缓存键失败:挂载/cache:已在构建阶段挂载

failed to solve: failed to compute cache key: mount /cache: already mounted in build stage

ID: docker/build-cache-mount-conflict

其他格式: JSON · Markdown 中文 · English
85%修复率
82%置信度
1证据数
2024-04-20首次发现

版本兼容性

版本状态引入弃用备注
Docker 25.0.3 active
BuildKit 0.12.5 active

根因分析

Dockerfile中的RUN --mount=type=cache指令尝试在同一构建阶段两次挂载相同的缓存目录,导致冲突。

English

A RUN --mount=type=cache directive in Dockerfile tries to mount the same cache directory twice in the same build stage, causing a conflict.

generic

官方文档

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

解决方案

  1. 将两个RUN命令合并为一个使用单一--mount=type=cache的指令,或使用不同的挂载路径:RUN --mount=type=cache,target=/cache/apt ... && ...
  2. 重构Dockerfile,为每个缓存挂载使用单独的构建阶段:FROM base AS builder1使用/cache/apt,然后FROM base AS builder2使用/cache/pip。

无效尝试

常见但无效的做法:

  1. 70% 失败

    Removing all --mount directives without understanding the conflict loses cache benefits and may break the build.

  2. 80% 失败

    Using different cache IDs for the same mount path still causes conflict because the path is already mounted.