9

尝试docker buildx与 GitLabs 共享运行器一起使用来构建可以在我的 Raspberry Pi 上运行的 Docker 映像。作业失败,说 git 不在 PATH 中,但 git 安装在image: docker:stable-git. 是否有任何已知的修复程序或更好的方法来构建与 ARM64 兼容的映像,而无需在 Raspberry Pi 本身上构建?(由于 CPU 使用率,RPi 在构建时变得不可用)

deploy:
  image: docker:stable-git
  stage: deploy
  services:
    - docker:dind
  before_script:
    - export DOCKER_BUILDKIT=1
    - export DOCKER_CLI_EXPERIMENTAL=enabled
    - docker build --platform=local -o . git://github.com/docker/buildx
    - mv buildx ~/.docker/cli-plugins/docker-buildx
  script:
    - docker buildx build --platform linux/amd64,linux/arm64 -t $CI_REGISTRY_IMAGE .
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    - docker push $CI_REGISTRY_IMAGE
$ export DOCKER_BUILDKIT=1
$ export DOCKER_CLI_EXPERIMENTAL=enabled
$ docker build --platform=local -o . git://github.com/docker/buildx
#1 [internal] load git source git://github.com/docker/buildx
#1 ERROR: failed to init repo at /var/lib/docker/overlay2/xxx/diff: exec: "git": executable file not found in $PATH
------
 > [internal] load git source git://github.com/docker/buildx:
------
failed to solve with frontend dockerfile.v0: failed to resolve dockerfile: failed to build LLB: failed to load cache key: failed to init repo at /var/lib/docker/overlay2/xxx/diff: exec: "git": executable file not found in $PATH
ERROR: Job failed: exit code 1
4

1 回答 1

17

看起来问题是由于未安装 git 而导致构建失败docker:dind

能够通过在单独的阶段克隆Docker/BuildXdocker build ,在其上运行,然后使用工件将其导出到部署阶段来解决该问题。

buildx:
  image: docker:19.03-git
  stage: buildx
  variables:
    GIT_STRATEGY: none
  artifacts:
    paths:
      - buildx
    expire_in: 1 hour
  services:
    - docker:19.03-dind
  script:
    - export DOCKER_BUILDKIT=1
    - git clone git://github.com/docker/buildx ./docker-buildx
    - docker build --platform=local -o . ./docker-buildx

deploy:
  image: docker:19.03
  stage: deploy
  services:
    - name: docker:19.03-dind
      command: ["--experimental"]
  before_script:
    - mkdir -p ~/.docker/cli-plugins
    - mv buildx ~/.docker/cli-plugins/docker-buildx
    - docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
  script:
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    - docker buildx create --use --name mybuilder
    - docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 --push -t $CI_REGISTRY_IMAGE .

由于 GitLab 对免费帐户所做的更改以及j0nl1Yajo发布的评论,我决定使用有关在 CircleCI 中使用 BuildX 的信息来更新我的答案

您可以通过执行以下操作下载 BuildX 二进制文件并在您的 CircleCI 作业中使用它

jobs:
  build-and-push:
    executor: docker/machine
    steps:
      - checkout
      - run:
          name: Remove old installation
          command: |
            sudo systemctl stop docker.service
            sudo apt remove docker-ce docker-ce-cli containerd.io
      - docker/install-docker:
          install-dir: /usr/bin
      - run:
          name: Restart docker daemon with experimental features
          command: |
            sudo bash -c 'echo "{\"experimental\":true}" > /etc/docker/daemon.json'
            if [[ $EUID == 0 ]]; then export SUDO=""; else export SUDO="sudo"; fi
            $SUDO systemctl unmask docker.service
            $SUDO systemctl unmask docker.socket
            $SUDO systemctl start docker.service
      - run:
          name: Install buildx
          command: |
            BUILDX_VERSION="v0.4.1"

            curl -sSLo docker-buildx "https://github.com/docker/buildx/releases/download/$BUILDX_VERSION/buildx-$BUILDX_VERSION.linux-amd64"
            chmod a+x docker-buildx
            mkdir -p ~/.docker/cli-plugins
            mv docker-buildx ~/.docker/cli-plugins/docker-buildx

            docker version
            docker buildx install
      - docker/check:
          registry: $DOCKER_REGISTRY
      - run:
          name: Create buildx profile
          command: |
            docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
            docker buildx create --use --name mybuilder
      - docker/build:
          registry: $DOCKER_REGISTRY
          image: username/repo
          tag: latest
          extra_build_args: --platform linux/amd64,linux/arm64,linux/arm/v7 --push
于 2019-10-30T04:37:03.157 回答