Published on

Multi-Arch Docker Builds with Github Actions

Authors

In this tutorial we will be using github actions to build multi-architecture docker builds.

End Result

We will keep it basic as we will build busybox container images for x86_64 as well as aarch64 using github actions.

Docker Container Image

As mentioned, we will have a basic demonstration, where we are just referencing a upstream image that supports both x86_64 and aarch64 container images, then we are just adding a couple of labels and then we will push the image.

Our Dockerfile in multi-arch-docker-builds/Dockerfile:

Dockerfile
FROM busybox:stable

ARG OS_ARCH=x86_64
ARG APP_VERSION="1.0"
ENV OS_ARCH=$OS_ARCH

LABEL "dev.ruan.ci.arch"="$OS_ARCH"
LABEL "dev.ruan.ci.version"="$APP_VERSION"

Github Actions Workflow

In our github actions workflow .github/workflows/multi-arch-docker-builds.yml:

multi-arch-docker-builds.yml
name: CI - Busybox

on:
  push:
    branches: [ main ]
    paths:
      - multi-arch-docker-builds/**

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Set tag
      id: vars
      run: echo "tag=$(cat multi-arch-docker-builds/Dockerfile | grep 'FROM' | cut -d ':' -f2)" >> $GITHUB_OUTPUT

    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v3

    - name: Set up QEMU
      uses: docker/setup-qemu-action@v3

    - name: Login to DockerHub
      uses: docker/login-action@v1
      with:
        username: ${{ secrets.DOCKERHUB_USER }}
        password: ${{ secrets.DOCKERHUB_PASSWORD }}

    - name: Build and push
      id: docker_build
      uses: docker/build-push-action@v2
      with:
        context: ./multi-arch-docker-builds
        file: ./multi-arch-docker-builds/Dockerfile
        push: true
        platforms: "linux/arm64,linux/amd64"
        build-args: |
          APP_VERSION=1.0.1
        tags: |
          ${{ secrets.DOCKERHUB_USER }}/busybox:${{ steps.vars.outputs.tag }}
          ${{ secrets.DOCKERHUB_USER }}/busybox:latest

Let's go through this workflow step-by-step:

Trigger:

  • We have defined the name of our workflow "CI - Busybox".
  • Trigger Condition: it will trigger on the main branch only if changes are detected in multi-arch-docker-builds/ directory.

Jobs:

  • We are defining the ubuntu-latest GitHub hosted runner.

Steps in Build Jobs:

  • Checkout the code.
  • Set the tag from the tag that is defined in the upstream image in the Dockerfile.
  • Set up docker buildx for extended build capabilities with BuildKit, including support for multiple architectures.
  • Sets up QEMU on the runner to support open-source machine emulator in order to run multi-arch.
  • Login to Docker Hub.
  • Builds and push container images to Docker Hub.

For this demonstration the images was pushed to :

Which we can see from this screenshot we have succesfully published our tag that supports both architectures:

multi-arch-dockerhub-github-actions

Thank You

Thanks for reading, if you like my content, feel free to check out my website, and subscrube to my newsletter or follow me at @ruanbekker on Twitter.

Buy Me A Coffee