Sub topic 6
Building with Buildx and BuildKit
This chapter assumes you can read the workflow from the earlier chapters and know what a layer is. It explains the build engine behind docker/build-push-action and the errors people hit most often.
BuildKit and Buildx
BuildKit is the build engine in modern Docker. It runs independent stages in parallel, skips stages whose output nothing uses, and can export its cache. Buildx is the Docker CLI plugin that talks to BuildKit. It adds features the classic docker build lacks: multiple builders, multi-platform output, cache export, and attestations.
Builders and drivers
A builder is a BuildKit instance. Its driver decides where it runs.
| Driver | Where BuildKit runs | Notes |
|---|---|---|
| `docker` | Inside the Docker daemon | Default. Limited cache export and no multi-platform output in one build. |
| `docker-container` | In a dedicated container | Supports all cache backends, multi-platform builds and attestations. |
In CI you almost always want docker-container. The docker/setup-buildx-action step creates that builder and makes it the default for later steps.
You can try the same thing locally:
docker buildx create --name ci-builder --driver docker-container --use
docker buildx build --load -t example-app:dev .
docker buildx lsUsing Buildx in the workflow
Two steps are needed: one to create the builder and one to build with it.
- uses: docker/setup-buildx-action@v3
- uses: docker/build-push-action@v6
with:
context: .
push: false
load: true
tags: example-app:cibuild-push-action calls docker buildx build for you. Its push and load inputs control where the result goes. This is the output decision.
push: truesends the image to the registry.load: truecopies the image into the runner's local Docker, so you can run it in a test step.- With neither, the
docker-containerbuilder keeps the result only in its build cache, and nothing is available to use.
Common errors
- Image not found after the build. The step finished, but
docker runfails. The build had neitherloadnorpush. Addload: true. - Cache export fails with a driver message. The error says the
dockerdriver does not support the requested cache type. Adddocker/setup-buildx-actionbefore the build step. loadfails on a multi-platform build. The local image store holds one platform at a time. Load a single platform, or push the multi-platform result instead.- Pull requests fail to push. Forked pull requests have a read-only token. Keep
pushconditional, as in the main workflow:push: ${{ github.event_name != 'pull_request' }}.
Exercise
A workflow uses docker/setup-buildx-action and then docker/build-push-action with push: false and no load input. The next step runs docker run example-app:ci and fails with "Unable to find image". Why, and what is the smallest fix?
Show answer
The docker-container builder keeps the build result in its own cache. Without load: true or push: true, no image reaches the runner's local Docker store. Add load: true to the build step so the image is available to docker run.