dockergithub-actions

Building both amd64 and arm64 with GitHub workflows from different files


What is the correct way to setup GitHub workflow for the multi-platform (amd64+arm64) Docker image build? I have two different docker files, one for each platform (see MapLibre/Martin project), and I tried to do it with a matrix configuring both platform and file params, but for some reason it publishes linux/amd64 image with the linux/arm64 metadata, and another image has unknown/unknown.

jobs:
  docker:
    name: Build ${{ matrix.platform }} docker image
    strategy:
      fail-fast: true
      matrix:
        include:
          - platform: linux/amd64
            file: Dockerfile
          - platform: linux/arm64
            file: arm64.Dockerfile
    runs-on: ubuntu-latest
    steps:

      - name: Checkout sources
        uses: actions/checkout@v3

      - name: Docker meta
        id: docker_meta
        uses: docker/metadata-action@v4
        with:
          images: ghcr.io/maplibre/martin

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2.1.0
        with:
          platforms: linux/arm64,linux/amd64

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2.5.0
        with:
          install: true
          platforms: ${{ matrix.platform }}

      - name: Build the Docker image
        id: docker_build
        uses: docker/build-push-action@v4
        with:
          file: ${{ matrix.file }}
          push: false
          load: true
          tags: ${{ steps.docker_meta.outputs.tags }}
          labels: ${{ steps.docker_meta.outputs.labels }}
          platforms: ${{ matrix.platform }}

      - name: Login to GitHub Docker registry
        uses: docker/login-action@v2
        if: ${{ github.actor != 'dependabot[bot]' && !github.event.pull_request.head.repo.fork }}
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Push the Docker image
        if: ${{ github.actor != 'dependabot[bot]' && github.event_name != 'pull_request' }}
        uses: docker/build-push-action@v4
        with:
          file: ${{ matrix.file }}
          push: true
          load: false
          tags: ${{ steps.docker_meta.outputs.tags }}
          labels: ${{ steps.docker_meta.outputs.labels }}
          platforms: ${{ matrix.platform }}

Solution

  • You don't need to use strategy: matrix: ... to do a multi-platform build. It will be much easier if you can integrate your two Dockerfiles into a single Dockerfile. Add --platform=$BUILDPLATFORM to the first FROM ... line of your Dockerfile.

    FROM --platform=$BUILDPLATFORM rust:alpine as builder
    

    You should try to settle on the same set of base images to use for both your arm and amd builds. If you can't, then it gets a bit more involved.

    Within your build step use an array of platforms:

        - name: Build Docker images
          uses: docker/build-push-action@v4
          with:
            platforms:
               - linux/amd64
               - linux/arm64
    

    You'll end up with something like this:

    jobs:
      docker:
        name: Build docker images
        runs-on: ubuntu-latest
        steps:
    
          - name: Checkout sources
            uses: actions/checkout@v3
    
          - name: Docker meta
            id: docker_meta
            uses: docker/metadata-action@v4
            with:
              images: ghcr.io/maplibre/martin
    
          - name: Set up Docker Buildx
            uses: docker/setup-buildx-action@v2.5.0
    
          - name: Login to GitHub Docker registry
            uses: docker/login-action@v2
            if: ${{ github.actor != 'dependabot[bot]' && !github.event.pull_request.head.repo.fork }}
            with:
              registry: ghcr.io
              username: ${{ github.actor }}
              password: ${{ secrets.GITHUB_TOKEN }}
    
          - name: Push the Docker image
            if: ${{ github.actor != 'dependabot[bot]' && github.event_name != 'pull_request' }}
            uses: docker/build-push-action@v4
            with:
              push: true
              tags: ${{ steps.docker_meta.outputs.tags }}
              labels: ${{ steps.docker_meta.outputs.labels }}
              platforms: 
                 - linux/arm64
                 - linux/amd64