docker compose / cache-from cache-to / buildx: 容器名称 /buildx_buildkit 已经在使用中。

huangapple go评论71阅读模式
英文:

docker compose / cache-from cache-to / buildx: The container name /buildx_buildkit is already in use

问题

Motivation
我有一个Docker Compose,其中包含多个服务,这些服务可能因为从注册表加载而导致CI时间显著增加,从而影响到构建和启动时间。为了减少构建和启动时间,我正在为许多服务使用 --cache-to 和 --cache-from。

使用缓存需要 Docker Buildx。

Problem
docker compose build 导致了一种构建x的并行执行,以及容器名称 "/buildx_buildkit_*" 的名称冲突。

滚动到解决方案部分查看底部的错误消息。

Solution
请参阅 fbjorn 的答案。同时注意以下内容:在撰写本文时,通常使用映像 docker:20.10.16-dind 用于CICD dind流水线。为了使这种情况正常工作,我不得不使用 docker:dind(即最新的dind)。

# dind.yml
version: '3.9'

services:
  dind:
    image: docker:dind
    privileged: true
    restart: always
    volumes:
      - /dockerbuild:/build/dockerbuild
# docker-compose.yml
version: '3.9'

services:
  my_service:
    build:
      context: .
      cache_from:
        - type=local,src=./${CACHE_DIR:-build-cache}/my_service
      cache_to:
        - type=local,dest=./${CACHE_DIR:-build-cache}/my_service
      dockerfile: Dockerfile
    image: ${MY_IMAGE_TAG:-dockerbuild:latest}
    command: tail -f /dev/null

  your_service:
    build:
      context: .
      cache_from:
        - type=local,src=./${CACHE_DIR:-build-cache}/your_service
      cache_to:
        - type=local,dest=./${CACHE_DIR:-build-cache}/your_service
      dockerfile: Dockerfile
    image: ${MY_IMAGE_TAG:-dockerbuild:latest}
    command: tail -f /dev/null
# Dockerfile
FROM alpine:latest

CMD ["/bin/sh"]
# 从gitlab-ci中的片段 - 我在dind容器中运行这些命令。
script:
    - cd /build/dockerbuild
    - docker buildx create --use --name my_builder_1
    # my_builder_1
    - docker compose build
    # /build/dockerbuild # docker compose build
    # [+] Building 38.4s (2/2) FINISHED
    #  => CANCELED [your_service internal] booting buildkit
    #  => => pulling image moby/buildkit:buildx-stable-1
    #  => => creating container buildx_buildkit_my_builder_10
    #  => ERROR [my_service internal] booting buildkit
    #  => => pulling image moby/buildkit:buildx-stable-1
    #  => => creating container buildx_buildkit_my_builder_10  s
    # ------
    #  > [my_service internal] booting buildkit:
    # ------
    # Error response from daemon: Conflict. The container name "/buildx_buildkit_my_builder_10" is already in use by container "...".
    # You have to remove (or rename) that container to be able to reuse that name.
英文:

Motivation
I have a docker compose which has several services which potentially are so large that they impact ci times significantly due to loading from registry.
To reduce build & start times I I'm using --cache-to & --cache-from for many of the services.

Using caches requires docker buildx.

Problem
docker compose build leads to somewhat a parallel execution of buildx and a name conflict for the container name "/buildx_buildkit_*"

Scroll past the Solution section to see the files I'm using with the error message on the bottom

Solution
See fbjorn's answer
Also pay attention to the following: at the time of writing, usually the image docker:20.10.16-dind is being used for cicd dind pipelines. To make this scenario work, I had to use docker:dind (ie latest dind)

# dind.yml
version: '3.9'

services:
  dind:
    image: docker:dind
    privileged: true
    restart: always
    volumes:
      - /dockerbuild:/build/dockerbuild
# docker-compose.yml
version: '3.9'

services:
  my_service:
    build:
      context: .
      cache_from:
        - type=local,src=./${CACHE_DIR:-build-cache}/my_service
      cache_to:
        - type=local,dest=./${CACHE_DIR:-build-cache}/my_service
      dockerfile: Dockerfile
    image: ${MY_IMAGE_TAG:-dockerbuild:latest}
    command: tail -f /dev/null
    
  your_service:
    build:
      context: .
      cache_from:
        - type=local,src=./${CACHE_DIR:-build-cache}/your_service
      cache_to:
        - type=local,dest=./${CACHE_DIR:-build-cache}/your_service
      dockerfile: Dockerfile
    image: ${MY_IMAGE_TAG:-dockerbuild:latest}
    command: tail -f /dev/null
    
  # sqlserver:
  #   build: 
  #     context: ./Dockerfile_sqlserver
  #     cache_from:
  #       - type=local,src=./${CACHE_DIR:-build-cache}/mssql
  #     cache_to:
  #       - type=local,dest=./${CACHE_DIR:-build-cache}/mssql
  #   image: mcr.microsoft.com/mssql/server:2019-CU19-ubuntu-20.04
    
# Dockerfile
FROM alpine:latest

CMD ["/bin/sh"]
# snippet from gitlab-ci - I'm running these commands in the dind container.
script:
    - cd /build/dockerbuild
    - docker buildx create --use --name my_builder_1 
      # my_builder_1
    - docker compose build
      # /build/dockerbuild # docker compose build
      # [+] Building 38.4s (2/2) FINISHED
      #  => CANCELED [your_service internal] booting buildkit
      #  => => pulling image moby/buildkit:buildx-stable-1
      #  => => creating container buildx_buildkit_my_builder_10
      #  => ERROR [my_service internal] booting buildkit
      #  => => pulling image moby/buildkit:buildx-stable-1
      #  => => creating container buildx_buildkit_my_builder_10  s
      # ------
      #  > [my_service internal] booting buildkit:
      # ------
      # Error response from daemon: Conflict. The container name "/buildx_buildkit_my_builder_10" is already in use by container "...". 
      # You have to remove (or rename) that container to be able to reuse that name.

答案1

得分: 3

我曾面临类似问题,通过一次构建多个镜像。它还说,“容器名称“/buildx_buildkit_*”已被容器使用<..>”。

对我来说,解决方法是在创建构建器时使用--bootstrap标志引导它。尝试将您的命令更改为:

docker buildx create --use --bootstrap --name my_builder_1 

然后您的构建命令不会尝试引导容器,因为它已经完成。

英文:

I was facing the similar problem by building multiple images at one. It also said The container name &quot;/buildx_buildkit_*&quot; is already in use by container &lt;..&gt;.

Solution for me was to bootstrap the builder when creating it by appending --bootstrap flag. Try changing your command to:

docker buildx create --use --bootstrap --name my_builder_1 

Then your build commands won't try to bootstrap the container because it's already done.

huangapple
  • 本文由 发表于 2023年5月18日 02:41:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275277.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定