Sub topic 4
Setting up a builder that supports remote cache
This chapter assumes you can write a Dockerfile, run docker build, and read a CI workflow file. You should also know that docker buildx is the CLI front end for BuildKit, the build engine behind modern Docker builds.
Why the default builder is not enough
A builder is the BuildKit instance that runs your build. Each builder uses a driver, which decides where BuildKit runs and which features it offers.
The default docker driver runs BuildKit inside the Docker daemon. It uses the daemon's own image store. It cannot export cache to a registry, to GitHub Actions, or to S3. If you pass --cache-to type=registry,... to it, the build fails with an error saying the cache export feature is not supported for the docker driver.
The docker-container driver starts BuildKit in its own container. That container has the full set of cache exporters, so it is the usual choice for CI.
| Driver | Where BuildKit runs | Remote cache export |
|---|---|---|
| `docker` | Inside the Docker daemon | Limited. Only the inline cache type works. |
| `docker-container` | In a dedicated container | Registry, gha, s3, and others |
| `kubernetes` | In pods on a cluster | Same exporters as docker-container |
| `remote` | On an existing BuildKit daemon | Depends on that daemon |
Create a docker-container builder
Run this once per runner, before the build:
docker buildx create --name ci-builder --driver docker-container --use
docker buildx inspect --bootstrap--name gives the builder a name. --use makes it the current builder, so later docker buildx build commands use it. inspect --bootstrap starts the container now and prints its details. Without --bootstrap, the container starts on the first build.
Check the result:
docker buildx lsThe ci-builder line should show the driver docker-container and a status of running.
Where the image ends up
A docker-container builder keeps its results inside the BuildKit container. It does not load them into your local Docker image store. After a build you have three choices:
- Add
--pushto send the image to a registry. - Add
--loadto copy the image into the local Docker daemon. This is slower for large images. - Add neither, and the build runs and fills the cache but keeps no image.
The last option is fine for a CI job that only checks that the build passes.
Create the builder in GitHub Actions
In GitHub Actions you do not run buildx create yourself. The docker/setup-buildx-action step does it:
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3By default this action creates a docker-container builder and selects it. Later docker/build-push-action steps then use it automatically. The gha backend also needs the runner's cache credentials, which the action's build step passes through for you.
Builder lifetime and the local cache
The BuildKit container has its own local cache, stored in a Docker volume. On a long-lived machine, that cache survives between builds as long as you reuse the same builder. On an ephemeral runner, the builder and its volume disappear when the job ends. That is the reason for the remote cache. The remote backend is the only cache that survives.
Common errors
- "Cache export is not supported for the docker driver." The current builder uses the
dockerdriver. Create adocker-containerbuilder and select it with--useor--builder. - The build cannot find the image afterward. The image stayed in the builder. Add
--loador--push. - The cache seems ignored on a new job. Check that
docker buildx lsshows your builder as current. A job that skips setup falls back to the default builder.
Exercise
Your CI job runs docker buildx build --cache-to type=registry,ref=registry.example.com/myapp:buildcache . and fails with an error about the docker driver. What do you change, and what do you add if the job also needs the image available to docker run afterward?
Show answer
Create and select a docker-container builder with docker buildx create --name ci-builder --driver docker-container --use (or run docker/setup-buildx-action in GitHub Actions). To use the image with docker run, add --load to the build, because a docker-container builder does not put images in the local Docker store by default.