Sub topic 7
Buildx and builders that support multiple platforms
This chapter assumes you can build, tag and push images, read a Dockerfile, and know that amd64 and arm64 are different CPU architectures. Earlier chapters covered the image index. Here you set up the tool that produces one.
What Buildx is
Buildx is the Docker CLI plugin that drives BuildKit, the build engine behind modern docker build. It adds multi-platform builds, build caches you can export, and attestations. A builder is a named BuildKit instance that Buildx sends your build to. Each builder uses a driver, which decides where and how BuildKit runs.
Drivers compared
| Driver | Where BuildKit runs | Multi-platform in one build | Typical use |
|---|---|---|---|
| `docker` | Inside the Docker daemon | Depends on the daemon's image store | Local builds with the default setup |
| `docker-container` | In a dedicated container | Yes | Multi-platform builds, CI |
| `kubernetes` | In pods on a cluster | Yes, across nodes | Shared or native-node builders |
| `remote` | An existing BuildKit daemon | Yes, if that daemon supports it | Dedicated build servers |
The default builder often uses the docker driver. With the classic Docker image store, that driver cannot export an image index. It can build only for the host platform. Recent Docker versions that use the containerd image store lift part of this limit. Creating a docker-container builder works on any setup, so this chapter uses it.
Create a multi-platform builder
docker buildx create --name multi --driver docker-container --use
docker buildx inspect --bootstrapcreate registers the builder. --use makes it the default for later docker buildx build calls. inspect --bootstrap starts the BuildKit container and prints its details.
Look at the Platforms: line in the output. It lists what this builder can target, for example linux/amd64, linux/arm64, linux/arm/v7. Native platforms come first. Platforms that appear only because of emulation show up once the handlers are registered.
Add emulation when you lack native nodes
If the host is amd64 and you need arm64, BuildKit must run arm64 binaries through QEMU. The Linux kernel does this through binfmt_misc handlers. Register them once per host:
docker run --privileged --rm tonistiivi/binfmt --install arm64Run docker buildx inspect --bootstrap again afterward. linux/arm64 should now appear in Platforms:.
Emulated steps run many times slower than native ones, especially compilers. A later chapter compares emulation, native builders and cross-compilation.
Manage builders
docker buildx lslists builders, their drivers and the platforms each supports.docker buildx use NAMEswitches the default builder.docker buildx stop NAMEstops the BuildKit container without deleting its cache.docker buildx rm NAMEremoves the builder and, by default, its cache.
Common errors
Multi-platform build is not supported for the docker driver: you are using thedockerdriver. Create adocker-containerbuilder and select it.exec format errorduring aRUNstep: BuildKit tried to run a binary for another architecture and no binfmt handler is registered. Install the handlers.- Builds are slow after the first run on a new builder: a new builder starts with an empty cache. Reusing the same named builder keeps it.
Exercise
You run docker buildx build --platform linux/amd64,linux/arm64 --push . on the default builder and get an error saying multi-platform builds are not supported for the docker driver. Which two commands fix it, and why does the fix work?
Show answer
Run docker buildx create --name multi --driver docker-container --use and then docker buildx inspect --bootstrap. The docker-container driver runs BuildKit in its own container and can produce an image index with several platforms. --use makes it the active builder, and --bootstrap starts it so you can check its Platforms: line.