How to Build a Matrix of Docker Image Variants in CI
A matrix fans one build definition out across variants, building each flavor in its own parallel job.
Put the variant list in strategy.matrix, then reference matrix.<key> in the build args and tags. Each combination builds independently, so a broken variant does not block the others when fail-fast is off.
Steps
- List the variants under
strategy.matrix(e.g. base versions). - Pass
build-argsandtagsderived from the matrix value. - Set
fail-fast: falseso every variant reports its own result.
Workflow
.github/workflows/docker.yml
jobs:
build:
strategy:
fail-fast: false
matrix:
variant: ['20', '22']
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/build-push-action@v6
with:
push: true
build-args: |
NODE_VERSION=${{ matrix.variant }}
tags: ghcr.io/${{ github.repository }}:node${{ matrix.variant }}Gotchas
- Each matrix leg pushes a distinct tag; make sure the tags differ or later legs overwrite earlier ones.
- Latchkey runs matrix builds on cheaper managed runners and auto-retries transient failures.
Related guides
How to Build With Docker Buildx Bake in CIDefine multiple Docker build targets in a bake file and build them together in CI with docker/bake-action, ke…
How to Build Only Changed Services in a Monorepo in CIBuild Docker images only for the services that changed in a monorepo by detecting touched paths and feeding t…