GitLab CI - 尝试使用 docker buildx 为 ARM64 构建

GitLab CI - Trying to use docker buildx to build for ARM64

尝试使用 docker buildx 与 GitLabs 共享 运行ners 构建一个 Docker 图像,可以 运行 在我的 Raspberry Pi 上。作业失败说 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

看起来问题是构建失败是因为 git 没有安装在 docker:dind

能够通过在单独的阶段克隆 Docker/BuildX,在其上 运行 docker 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 对免费帐户所做的更改以及 j0nl1 and Yajo

发表的评论,我决定使用有关在 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