containers

Traditionally, container images were built and optimized for a specific architecture, such as x86 or ARM64. However, with the advent of diverse hardware architectures and the rise of cloud platforms, the need for multi-architecture support became crucial. Multi-architecture containers enable deploying the same image across different architectures seamlessly.

Here's an example of a GitHub Actions workflow that builds multi-architecture container images for both amd64 and arm64 architectures when a pull request is created or when changes are pushed to the main branch:

name: Build Multi-Architecture Container Image

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: macos-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v2

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

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

    - name: Login to Docker registry
      uses: docker/login-action@v1
      with:
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}

    - name: Build and push multi-architecture image
      uses: docker/build-push-action@v5
      with:
        context: .
        push: true
        tags: |
          myregistry/myapp:latest-amd64
          myregistry/myapp:latest-arm64
        platforms: linux/amd64,linux/arm64

In this example, the workflow is triggered when there is a push to the main branch or when a pull request is created or updated against the main branch. The workflow includes the following steps:

  1. Check out repository: This step checks out your repository's code.

  2. Set up QEMU: This step sets up QEMU for cross-building the arm64 architecture on amd64 host machines.

  3. Set up Docker Buildx: This step sets up Docker Buildx, a Docker command-line interface (CLI) plug-in for building multi-architecture images.

  4. Log in to the Docker registry: This step logs in to your Docker registry using the provided credentials stored as secrets in the repository settings.

  5. Build and push multi-architecture image: This step uses the docker/build-push-action to build and push the multi-architecture container image. The context parameter specifies the root directory of your Docker build context. The tags parameter specifies the tags for the generated images, including the architecture suffix (amd64 and arm64). The PLATFORMS environment variable specifies the target platforms for the build, which are linux/amd64 and linux/arm64.

  6. Make sure to replace myregistry with your Docker registry URL and provide the appropriate Docker registry credentials in the GitHub repository's secrets (DOCKER_USERNAME and DOCKER_PASSWORD).

With this workflow in place, whenever there is a pull request or a push to the main branch, GitHub Actions will automatically build and push the multi-architecture container image for both amd64 and arm64 architectures. You can expand on this workflow by adding additional steps for testing, deploying, or other actions as per your project requirements.

Here's an example of a GitHub Actions workflow that uses Podman to build multi-architecture container images for both AMD64 and ARM64 architectures on pull requests and the main branch:

name: Build Multi-Architecture Container Image

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: macos-latest

    container:
      image: myregistry/mypodman:latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Log in to container registry
        run: echo "${{ secrets.CONTAINER_REGISTRY_PASSWORD }}" | podman login -u "${{ secrets.CONTAINER_REGISTRY_USERNAME }}" --password-stdin myregistry

      - name: Build and push multi-architecture image
        run: |
          podman build --format docker --platform linux/amd64,linux/arm64 -t myregistry/myapp:latest .
          podman push myregistry/myapp:latest

In this example, the GitHub Actions workflow uses macos-latest, you can change it based on your requirement. The GitHub Actions workflow uses the custom Docker image containing Podman (mypodman:latest) to build and push the multi-architecture container image for both amd64 and arm64 architectures. The workflow will be triggered on every push to the main branch or when a pull request is created or updated against the main branch.

Note that using Podman in GitHub Actions involves custom Docker images, and there might be limitations and compatibility considerations that you need to be aware of. Additionally, the example provided assumes you have set up secrets in your GitHub repository for your container registry credentials.

Last updated: December 13, 2023