M. Buil
M. Buil

Reputation: 569

Github actions building a multi-platform image using multiple Dockerfiles

I would like to use github actions to build a multiarch image as explained here https://github.com/docker/build-push-action/blob/master/docs/advanced/multi-platform.md

All examples I find, use the same Dockerfile for all architectures. However, the project I am working with contains a different Dockerfile for each architecture. Is there a way to create a multi-arch image with github actions by consuming the different Dockerfiles?

Upvotes: 1

Views: 2538

Answers (1)

quotidian-ennui
quotidian-ennui

Reputation: 126

You can abuse the strategy matrix?

name: docker-latest

on:
  push:
    branches: master

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        docker_target: [ 'x86_64:linux/amd64', 'arm:linux/arm64,linux/arm/v7,linux/arm/v8']
    steps:
    - name: Checkout
      uses: actions/checkout@v3
    - name: Set up QEMU
      uses: docker/setup-qemu-action@v1
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v1
    - name: Login to DockerHub
      uses: docker/login-action@v1
      with:
        username: ${{ secrets.DOCKER_USER }}
        password: ${{ secrets.DOCKER_TOKEN }}
    - name: docker buildx ${{ matrix.docker_target }}
      run: |
        DOCKER_FILE=$(echo "${{ matrix.docker_target }}" | cut -f1 -d:)/Dockerfile
        DOCKER_IMAGE=myImageName:latest
        DOCKER_PLATFORM=$(echo "${{ matrix.docker_target }}" | cut -f2 -d:)
        docker buildx build . --no-cache --platform ${DOCKER_PLATFORM} -t "${DOCKER_IMAGE}" -f "${DOCKER_FILE}" --push

This will depend on the behaviour of docker buildx since I think the --push will overwrite that tag completely so in this instance you might only end up with the various arm architectures and not the amd64 or vice versa.

The question really here is, why does the architecture require different Dockerfiles (since I presume it's the same actual application). Surely you can use the TARGETARCH and TARGETPLATFORM args inside your Dockerfile to "do the right thing".

Upvotes: 2

Related Questions