You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

启用BuildKit但缺失buildx组件,Docker构建报错求助

Docker --mount缓存构建失败:BuildKit组件缺失的解决方法

问题详情

使用Dockerfile中的--mount缓存指令构建镜像:

RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
apt-get update && \
apt-get install -y --no-install-recommends python3 g++ build-essential && \
rm -rf /var/lib/apt/lists/*

执行构建时触发报错:

##[error]DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
##[error]            Install the buildx component to build images with BuildKit:
##[error]            https://docs.docker.com/go/buildx/
##[error]the --mount option requires BuildKit. Refer to https://docs.docker.com/go/buildx/ to learn how to build images with BuildKit enabled
##[error]The process '/usr/bin/docker' failed with exit code 1

尝试通过以下方式启用BuildKit后,仍报错:

  • 临时环境变量:
    export DOCKER_BUILDKIT=1 
    docker build .
    
  • 命令行临时变量:
    DOCKER_BUILDKIT=1 docker build .
    
  • Azure Pipeline变量配置:
    variables:
      DOCKER_BUILDKIT: 1
    

新报错信息:

##[error]ERROR: BuildKit is enabled but the buildx component is missing or broken.
##[error]       Install the buildx component to build images with BuildKit:
##[error]       https://docs.docker.com/go/buildx/
##[error]The process '/usr/bin/docker' failed with exit code 1

环境信息:

  • Docker版本:24.0.5
  • 操作系统:Ubuntu 22.04
  • 部署场景:Backstage Docker部署流程

解决步骤

1. 安装docker-buildx-plugin

Ubuntu 22.04下直接通过apt安装官方插件:

sudo apt update
sudo apt install -y docker-buildx-plugin

2. 验证安装状态

执行命令确认buildx可用:

docker buildx version

输出包含版本信息即为安装成功。

3. 使用buildx执行构建

启用BuildKit并通过buildx构建:

DOCKER_BUILDKIT=1 docker buildx build .

Azure Pipeline中修改构建步骤:

steps:
  - script: |
      DOCKER_BUILDKIT=1 docker buildx build -t your-image-tag .
    displayName: 'Build Docker image with BuildKit'

4. (可选)设置buildx为默认构建器

执行以下命令后,docker build将自动使用buildx:

docker buildx install

说明

Docker 23.0及以上版本中,buildx不再作为CLI内置组件,需单独安装插件包。Ubuntu官方仓库提供的docker-buildx-plugin可直接解决组件缺失问题。

内容的提问来源于stack exchange,提问作者pf12345678910

火山引擎 最新活动