Sub topic 13
BuildKit and docker buildx
This chapter assumes you can already run docker build with -t, and that you know what image layers and the build cache are. Here you will see which engine runs your builds and how docker buildx gives you access to its extra features.
What BuildKit is
BuildKit is the build engine behind docker build. Docker Engine 23.0 and later use it by default, and so does Docker Desktop. On older engines, set DOCKER_BUILDKIT=1 to turn it on.
Compared with the older builder, BuildKit offers these improvements:
- It runs independent stages of a multi-stage build in parallel.
- It skips stages that the target image does not need.
- It sends only the changed files from the build context.
- It supports secret mounts and cache export.
- It prints structured progress output.
The default progress view hides command output. To see every line, switch to plain output:
docker build --progress=plain -t myapp:debug .What docker buildx adds
docker buildx is a Docker CLI plugin that exposes the full BuildKit feature set. docker buildx build accepts every flag that docker build does. In current Docker versions, docker build is an alias for docker buildx build. The plugin adds builder management and flags for exporting results.
| Flag | Purpose |
|---|---|
| `--platform` | Target one or more CPU architectures |
| `--push` | Upload the result to a registry |
| `--load` | Put the result in the local image store |
| `--secret` | Provide a build secret |
| `--cache-from`, `--cache-to` | Import or export build cache |
Builders and drivers
A builder is a BuildKit instance that runs your build. Each builder has a driver that decides where BuildKit runs. List your builders with docker buildx ls.
- The default builder uses the
dockerdriver. BuildKit runs inside the Docker Engine, and results go straight into the local image store. - A builder with the
docker-containerdriver runs BuildKit in its own container. It supports multi-platform output and cache export.
docker buildx ls
docker buildx create --name mybuilder --driver docker-container --use
docker buildx inspect --bootstrap
docker buildx build -t myapp:1.0 --load .create defines the builder and --use selects it. inspect --bootstrap starts it and shows its supported platforms. The last command builds with it.
Common errors
- A feature fails on the default builder. Multi-platform output and cache export need a
docker-containerbuilder. Create one as shown above. - The image is missing after a successful build. You used a container builder without
--loador--push. - Secret or mount syntax is rejected. Your engine is probably using the legacy builder. Check the Docker version and use BuildKit.
- You cannot tell why a step failed. Re-run with
--progress=plainto see the full output.
The next chapters use these features for build secrets and multi-platform builds.
Exercise
You create a builder with docker buildx create --driver docker-container --use and run docker buildx build -t myapp:1.0 .. The build succeeds, but docker image ls myapp shows nothing. Why, and how do you fix it?
Show answer
A docker-container builder keeps results in its build cache unless told to export them. Add --load to import the image into the local image store, or --push to upload it to a registry.