Tag Archives: Configure the pit

Constructing docker image of multi arch

sometimes we want to build images that can run on machines with different hardware architectures (mainly arm architectures) to better meet the requirements of a heterogeneous distributed environment. Previously, the usual solution was to prepare multiple machines with different hardware architectures and compile and distribute them separately, but now Docker has a new (albeit experimental) buildx feature that solves this problem nicely.

buildx can be described in the relevant information, and it will not be repeated here. If you want to run locally, you can refer to this article: BUILDING DOCKER IMAGES FOR KUBERNETES RUNNING ON ARM. However, it is difficult to complete the configuration locally according to the actual measurement based on the current network environment, because it needs to connect with Docker.io in the whole process and need to download a large amount of data, so it is likely to disconnect halfway… So take a detour, use some third-party platform to complete the build, and release it to a mirrored warehouse.

I chose github actions because I saw that there were written actions on github, which greatly simplifies the configuration. With this script, you can build an image across the hardware architecture and publish it to docker hub:

  docker:
    name: Publish Docker Image
    runs-on: ubuntu-18.04
    env:
      REPO: ${{ secrets.ENTITY_DOMAIN_REPO }}

    steps:
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v1
        with:
          platforms: all

      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v1
        with:
          version: latest

      -
        name: Available platforms
        run: echo ${{ steps.buildx.outputs.platforms }}
        # 可以在这里看到可以兼容哪些平台

      - name: Login to Docker Registry
        run: echo '${{ secrets.DOCKERHUB_PASS }}' | docker login -u ${{ secrets.DOCKERHUB_USER }} --password-stdin

      - name: Build Docker Image
        run: docker buildx build -t $REPO:latest --platform linux/amd64,linux/arm64 --push .
        # 在--platform后面指定需要兼容的平台列表