Skip to main content

GitHub Actions for Easy ARM64 and AMD64 Docker Image Builds

· Waktu baca 15 menit
Agastya Darma Laksana

In today's rapidly evolving software development landscape, the ability to efficiently build and deploy applications across diverse architectures is crucial. This article will explore an advanced yet accessible approach to creating multi-architecture Docker images, specifically for ARM64 and AMD64 platforms, using GitHub Actions. We will dissect a YAML configuration for a GitHub Actions workflow named 'Deploy Production,' illustrating how to automate the building and pushing of Docker images to Docker Hub.

Our focus will be on leveraging the capabilities of GitHub Actions, including setup of QEMU for emulation and Docker Buildx for building images, along with caching strategies and security practices for Docker Hub integration. This guide aims to equip developers with the knowledge and tools needed to streamline their CI/CD pipelines, ensuring seamless deployment across varied computing environments.

name: Deploy Production
runs-on: ubuntu-latest
- 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: Cache Docker layers
uses: actions/cache@v2
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Login to Docker Hub
uses: docker/login-action@v1
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v2
context: .
file: ./Dockerfile
push: true
tags: agasdrm/blog:latest,agasdrm/blog:${{ github.run_number }}
platforms: linux/amd64,linux/arm64/v8
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache

Overview of Docker and GitHub Actions

Docker has revolutionized how we build, share, and run applications by enabling them to run in isolated containers. These containers package up code and all its dependencies, ensuring that the application runs quickly and reliably in various computing environments.

GitHub Actions, on the other hand, is a CI/CD (Continuous Integration/Continuous Delivery) platform that allows you to automate your build, test, and deployment pipeline. It is seamlessly integrated into GitHub, providing an efficient way to apply DevOps practices without leaving the GitHub ecosystem.

The Importance of Multi-Architecture Builds

With the growing diversity in computing hardware, it's become crucial to ensure that applications can run on different architectures. ARM64 and AMD64 are two prevalent architectures in use today. ARM64 is commonly found in mobile devices, newer desktops, and servers, while AMD64 (x86_64) is widely used in traditional laptops and desktops. Supporting both architectures ensures that your application can reach a wider audience and function in a broader range of environments.


Before diving into the process, ensure you have the following:

  • A GitHub account - for accessing GitHub and using GitHub Actions.
  • A Docker Hub account - for storing and managing your Docker images.
  • Basic understanding of Docker - familiarity with Docker concepts like images, containers, and Dockerfiles.
  • Basic understanding of Git and GitHub - knowledge of repositories, branches, commits, and pulls.
  • Access to a code editor - like Visual Studio Code, Sublime Text, or any editor of your choice.

Understanding the Dockerfile

A Dockerfile is a text document containing all the commands a user could call on the command line to assemble an image. Using a Dockerfile, Docker can automatically build an image that includes your application and its dependencies.

Key Components of a Dockerfile

  • FROM: Specifies the base image from which you are building.
  • RUN: Executes any commands in a new layer on top of the current image.
  • COPY: Copies files or directories from your project into the Docker container.
  • CMD: Provides defaults for executing a container.
  • EXPOSE: Informs Docker that the container listens on specific network ports at runtime.
  • ENV: Sets environment variables.

Each instruction in a Dockerfile creates a layer in the image. When you change the Dockerfile and rebuild the image, only those layers that have changed are rebuilt. This is part of what makes images so lightweight, small, and fast when compared to other virtualization technologies.

In the next section, we will dive deeper into setting up our GitHub Actions workflow to automate the building and publishing of our Docker images for ARM64 and AMD64 architectures.

Understanding GitHub Actions

GitHub Actions is a powerful automation tool that integrates deeply with GitHub, providing a platform for automating software workflows. It's particularly useful in implementing Continuous Integration (CI) and Continuous Delivery (CD) processes directly within your repository.

What are GitHub Actions?

  • Automated Workflows: GitHub Actions allows you to create custom software development life cycle (SDLC) workflows directly in your GitHub repository.
  • Event-Driven: These workflows can be triggered by a variety of events within GitHub, such as push, pull requests, issue creation, or even a manual trigger.
  • Customizable: GitHub Actions provides a wide range of built-in actions, but you can also create and share your own actions or use actions shared by the GitHub community.
  • Containers and Virtual Environments: Actions can run in containers or virtual environments that you specify, ensuring consistency across different runs.

CI/CD with GitHub Actions

In Continuous Integration (CI), code changes are automatically tested and merged into a central repository. Continuous Delivery (CD) extends CI by automatically deploying all code changes to a testing or production environment. GitHub Actions facilitates both CI and CD by automating these processes, enhancing software quality and development speed.

Workflow Syntax Explanation

GitHub Actions utilizes workflows, which are defined by a YAML file stored in your repository's .github/workflows directory. These workflows specify the actions to be executed based on defined events.

Understanding the workflow_dispatch Trigger

  • Manual Trigger: The workflow_dispatch event allows you to manually trigger a workflow run. This is useful when you want to have control over when to run the workflow, as opposed to automatic triggers like push or pull requests.
  • Inputs: You can define inputs for the workflow_dispatch trigger, allowing you to pass parameters to the workflow at runtime.
name: Deploy Production
description: 'Deployment Environment'
required: true
default: 'staging'

In this example, the workflow_dispatch is configured with an input named environment. When triggering the workflow manually, you can specify the environment to which you want to deploy.

Workflow Anatomy A typical workflow file consists of the following key sections:

name: A name for your workflow.
on: The event that triggers the workflow.
jobs: A set of jobs that the workflow will execute.
steps: Individual tasks that run within a job.
runs-on: Specifies the type of machine to run the job on.
uses: Refers to an action to execute as part of a step.

Preparing for Docker Build

Before diving into the actual Docker build process, it's crucial to understand the role of QEMU in this workflow and how it's set up using GitHub Actions.

What is QEMU?

QEMU (Quick Emulator) is an open-source emulator that performs hardware virtualization. It is a key tool in the process of running software that is built for one type of processor architecture on another. This capability is particularly important when dealing with Docker images intended for multiple architectures.

Role of QEMU in Docker Builds

  • Emulation of Architectures: QEMU allows us to emulate different processor architectures, such as ARM64, on machines that have a different architecture (like AMD64). This is vital for building Docker images that are meant to run on multiple architectures.
  • Cross-Platform Compatibility: By using QEMU, we ensure that the Docker images built are compatible across different architectures, making our application more versatile and accessible.

Setting Up QEMU in GitHub Actions

The docker/setup-qemu-action@v1 action is used to set up QEMU in the GitHub Actions runner environment. This action simplifies the process of installing and configuring QEMU for our build process.

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

In the workflow file, this step ensures that QEMU is correctly set up and ready to enable emulation for different architectures during the Docker build process.

Docker Buildx Setup

Docker Buildx is an extended build feature of Docker that enables the creation of multi-architecture images.

Understanding Docker Buildx

  • Extended Features: Docker Buildx extends the capabilities of the standard Docker build commands, allowing for more advanced features, including building for multiple architectures from a single platform.
  • Simplifying Multi-Arch Builds: With Buildx, you can easily build images for architectures like ARM64 and AMD64, even if your build environment is different.

Setting Up Docker Buildx in GitHub Actions

The docker/setup-buildx-action@v1 action in GitHub Actions is used to set up Docker Buildx in the workflow.

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

By including this step in our workflow file, we enable the GitHub Actions runner to utilize Docker Buildx for building multi-architecture Docker images. This step is critical in ensuring that our Docker images are built correctly for the desired architectures.

Caching for Efficiency in Docker Builds

Efficient Docker builds often require a smart caching strategy. This section delves into the use of caching in Docker builds with GitHub Actions, particularly focusing on the actions/cache@v2 action.

Caching Docker Layers

Caching is crucial in reducing build time, especially when dealing with Docker images. Docker builds can be time-consuming, primarily due to the downloading of image layers and rebuilding steps. By caching, we can significantly decrease this time.

How Caching Works in Docker

  • Layer Caching: Docker builds images in layers. By caching these layers, Docker can reuse them in subsequent builds, avoiding the need to rebuild identical layers.
  • Impact on Build Times: Caching can drastically reduce build times, making your CI/CD pipeline more efficient.

Implementing Caching with actions/cache@v2

GitHub Actions provides a caching action, actions/cache@v2, which can be used to cache dependencies and build outputs to improve workflow execution time.

- name: Cache Docker layers
uses: actions/cache@v2
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-

In this snippet, the caching action is configured to cache the Docker layers.

Cache Keys and Paths

Understanding how to effectively use cache keys and paths is vital for maximizing the efficiency of your caching strategy.

Defining Cache Paths

  • Cache Location: In our workflow, the cache is stored in /tmp/.buildx-cache. This path is where Docker Buildx stores its cache data.

Cache Keys

  • Key Generation: The key for the cache is a unique identifier. In our example, it's a combination of the runner's operating system, buildx, and the SHA of the commit (${{ github.sha }}). This ensures that each build has its unique cache.
  • Restoration of Cache: The restore-keys option provides a list of keys to try when restoring the cache. In our case, it attempts to find the most recent cache that matches the specified pattern.

Benefits of Effective Caching

  • Reduced Build Times: By reusing the cached layers, Docker can skip rebuilding unchanged layers, significantly reducing the overall build time.
  • Improved Efficiency: Efficient caching leads to faster CI/CD pipelines, which is crucial for agile development and quick deployment cycles.

Docker Hub Integration in GitHub Actions

Integrating Docker Hub with GitHub Actions is a key step in automating the Docker image build and push process. This section covers the essential steps for secure Docker Hub login and handling secrets within GitHub Actions.

Docker Hub Login

Logging into Docker Hub from GitHub Actions is a crucial step to push the built images to your Docker Hub repository. This is where the docker/login-action@v1 action comes into play.

Importance of Secure Login

  • Security: Securely logging into Docker Hub ensures that your credentials are protected and your Docker Hub account is safe from unauthorized access.
  • Automation: Automated login is essential for seamless CI/CD pipelines, enabling the automated pushing of Docker images to Docker Hub without manual intervention.

Implementing Docker Hub Login

- name: Login to Docker Hub
uses: docker/login-action@v1
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

In this step, the docker/login-action@v1 action is used to log into Docker Hub. The credentials are provided through GitHub secrets to ensure security.

Handling Secrets

GitHub secrets provide a secure way to store and use sensitive information, like Docker Hub credentials, in your GitHub Actions workflows.

What are GitHub Secrets?

  • Security Feature: Secrets are encrypted environment variables that you can create in any repository or organization in GitHub. They are not exposed in log files or to unauthorized users.
  • Usage in Workflows: Secrets can be used in GitHub Actions workflows to securely manage sensitive data like API keys, passwords, and Docker Hub credentials. Configuring and Using Secrets
  • Adding Secrets: Secrets can be added to your GitHub repository under the settings tab. Once added, they can be referenced in your workflow file. Referencing Secrets in Workflows: In the Docker Hub login step, the username and password are referenced as ${{ secrets.DOCKERHUB_USERNAME }} and ${{ secrets.DOCKERHUB_TOKEN }}. This allows the workflow to use these credentials without exposing them.

Benefits of Using Secrets

  • Enhanced Security: By using secrets, sensitive information is kept secure and is not exposed in your workflow files or logs.
  • Flexibility: Secrets can be easily updated in the GitHub repository settings without changing the workflow files.

Building and Pushing the Docker Image

The culmination of our GitHub Actions workflow is the building and pushing of the Docker image. This section provides a detailed walkthrough of this process, highlighting the use of docker/build-push-action@v2 and explaining image tagging and multi-architecture support.

Building the Docker Image

Building the Docker image is a critical step in the workflow. The docker/build-push-action@v2 action simplifies this process within GitHub Actions.

Using docker/build-push-action@v2

This action enables us to build and push the Docker image to a registry like Docker Hub. It supports advanced features like multi-platform builds and caching.

- name: Build and push
uses: docker/build-push-action@v2
context: .
file: ./Dockerfile
push: true
tags: user/myapp:latest, user/myapp:${{ github.run_number }}
platforms: linux/amd64,linux/arm64/v8
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache

In this configuration:

  • context specifies the build context (typically the root of the repository).
  • file points to the Dockerfile.
  • push set to true enables the pushing of the image to the registry.
  • tags defines the tags for the built image.
  • platforms lists the target architectures.
  • cache-from and cache-to handle caching to speed up the build process.

Tagging and Multi-Architecture Support

Tagging images correctly and supporting multiple architectures are crucial for a robust Docker deployment.

Tagging Images

  • Purpose of Tags: Tags are used to specify different versions of your Docker images. It's a best practice to tag your images with meaningful identifiers, like version numbers or environment names.
  • Tag Syntax in GitHub Actions:
tags: user/myapp:latest, user/myapp:${{ github.run_number }}

Here, we use two tags - latest for the most recent build and a tag with the GitHub run number for specific builds.

Multi-Architecture Support

Supporting multiple architectures ensures that your Docker images can run on various hardware platforms.

  • Specifying Architectures: In the platforms field of the docker/build-push-action@v2, we specify the target architectures.
platforms: linux/amd64,linux/arm64/v8
  • Benefits: This approach ensures that the built Docker images are compatible with both AMD64 and ARM64 architectures, broadening the usability of your application.

Conclusion and Best Practices

We've now reached the end of our comprehensive guide on building and pushing multi-architecture Docker images using GitHub Actions. Let's recap the steps we've taken and go over some best practices and troubleshooting tips.

Review of the Process

Our journey through this tutorial involved several key steps:

  1. Introduction to Docker and GitHub Actions: We began by understanding the basics of Docker, GitHub Actions, and the need for multi-architecture builds.
  2. Setting Up the Workflow: We then explored the workflow syntax in GitHub Actions and how to trigger workflows.
  3. Environment Preparation: Setting up QEMU and Docker Buildx were crucial steps for cross-platform Docker builds.
  4. Caching for Efficiency: Implementing caching using actions/cache@v2 improved our build times significantly.
  5. Docker Hub Integration: We securely logged into Docker Hub using GitHub secrets, ensuring a secure pipeline.
  6. Building and Pushing Images: Finally, we used docker/build-push-action@v2 to build and push images, tagging them appropriately and supporting multiple architectures.

Best Practices

Following best practices can significantly enhance the efficiency and security of your Docker builds and GitHub Actions workflows.

Docker Builds
  • Use Multi-Stage Builds: This helps keep your images small and efficient.
  • Leverage Caching: Proper caching can drastically reduce build times.
  • Keep Images Secure: Regularly update and scan your images for vulnerabilities.
GitHub Actions
  • Keep Workflows Simple: Complex workflows are harder to maintain and debug.
  • Secure Secrets: Always use GitHub secrets to handle sensitive information.
  • Document Your Workflows: Clear documentation helps maintain and understand workflows.

Troubleshooting Tips

Here are some common issues and tips for troubleshooting Docker builds and GitHub Actions workflows.

Docker Builds
  • Build Failures: Check for syntax errors in your Dockerfile. Ensure all necessary files are included in the build context.
  • Long Build Times: Investigate caching strategies and consider optimizing your Dockerfile for better layer caching.
GitHub Actions
  • Workflow Errors: Review logs provided by GitHub Actions for specific error messages. Ensure all steps are correctly configured.
  • Secrets Not Working: Double-check secret names in the repository settings and their references in the workflow file.

With this knowledge, you're now equipped to create efficient, automated Docker workflows using GitHub Actions. Happy coding!