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

时间:2019-10-29 03:11:23

标签: git docker gitlab gitlab-ci docker-in-docker

尝试与GitLabs共享运行程序一起使用docker buildx来构建可以在我的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

1 个答案:

答案 0 :(得分:1)

问题似乎是由于docker:dind

中未安装git而导致构建失败

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

buildx:
  image: docker:stable-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:stable
  stage: deploy
  services:
    - name: docker: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 buildx create --use --name mybuilder
    - docker buildx build --platform linux/arm64 --load -t $CI_REGISTRY_IMAGE .
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    - docker push $CI_REGISTRY_IMAGE