Sub topic 13
Automating the Build in CI
This chapter assumes you can build and push a multi-platform image with docker buildx build, that you know what a manifest list is, and that you have used a CI system such as GitHub Actions. Here you move the manual commands into a pipeline that runs on every release tag.
What the pipeline must do
A CI runner is a fresh machine for every job. Nothing from your laptop carries over. The runner has to repeat the setup steps you ran by hand:
- Check out the source code.
- Register QEMU handlers with the kernel, so the runner can run non-native binaries.
- Create a Buildx builder that can build several platforms.
- Log in to the registry.
- Build for each platform and push the manifest list.
A GitHub Actions workflow
This workflow runs when you push a tag that starts with v. It builds linux/amd64 and linux/arm64 and pushes to GitHub Container Registry.
name: image
on:
push:
tags: ["v*"]
permissions:
contents: read
packages: write
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/setup-qemu-action@v3
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ghcr.io/example/app:latestEach action replaces a command from earlier chapters:
| Action | Manual equivalent |
|---|---|
| `docker/setup-qemu-action` | `docker run --privileged --rm tonistiigi/binfmt --install all` |
| `docker/setup-buildx-action` | `docker buildx create --use` and `docker buildx inspect --bootstrap` |
| `docker/login-action` | `docker login` |
| `docker/build-push-action` | `docker buildx build --platform ... --push` |
Permissions and credentials
The permissions block gives the job's built-in token only what it needs. contents: read lets the job check out the code. packages: write lets it push to ghcr.io. The password comes from secrets.GITHUB_TOKEN, which GitHub creates for each run, so no long-lived password is stored in the repository.
For another registry, store the credentials as repository secrets and reference them the same way. Never write them into the workflow file.
Tagging
The example always pushes latest, so every release overwrites it. Most teams also tag with the version. The docker/metadata-action can generate tags from the Git tag, and you pass its output to the tags input. Keep the generated tag immutable, since a version tag that changes after release makes rollbacks hard to reason about.
Speed and verification
An emulated arm64 build on an amd64 runner can be many times slower than a native build, especially for compile steps. Two common fixes:
- Use the cross-compilation pattern from the earlier chapter, so the slow compile step runs natively.
- Cache layers between runs. The build action accepts
cache-fromandcache-to, for example withtype=gha.
Add a final step that runs docker buildx imagetools inspect on the pushed tag. If the output does not list both platforms, the job should fail instead of reporting success.
Exercise
A teammate removes the docker/setup-qemu-action step from the workflow, and the arm64 build on ubuntu-latest (an amd64 runner) starts failing at the first RUN instruction. Why, and what is the fix?
Show answer
Without QEMU handlers registered through binfmt_misc, the amd64 kernel cannot execute the arm64 binaries that RUN needs, which produces exec format error. Restore the docker/setup-qemu-action step before the build step, or build arm64 on a native arm64 node.