DOCKER BUILDKIT-BUILDX
Docker builds images by reading the instructions from a Dockerfile. If you are not familiar with Dockerfile then please have a read on this link.
Docker images consist of read-only layers, each resulting from an instruction in the Dockerfile. Layers are stacked sequentially and each one is a delta representing the changes applied to the previous layer.
What is docker buildX?
It is a CLI plugin that extends the docker command with the full support of the features provided by Moby Build Kit builder toolkit. It provides the same user experience as docker build with many new features like creating scoped builder instances and building against multiple nodes concurrently. Furthermore, it also provides us with the same user experience as building with the docker build command.
Why BuildX is necessary when we can build with Docker Build?
BuildX comes with new extra features like it allows us to build an image for the native architecture, similar to a docker build but it also supports and allows for emulation.
Here, emulation means that from a specific machine (for example, say Intel machine) we can build an image targeted for a different architecture-supported machine (for example, a raspberry pi machine). For some more information on emulation, have a look on here.
Along with some new additions to it, such as constructing simultaneously against many nodes with various arch images, builder instances, etc. Additionally, buildx enables brand-new features like creating manifest lists and distributed caching that are not currently supported by standard Docker builds.
Play with Docker Builder Objects..
By default, Buildx uses the “docker” driver if it is supported, providing a user experience very similar to the native docker build. Note that you must use a local shared daemon to build your applications.
Buildx allows you to create new instances of isolated builders. You can use this to get a scoped environment for your CI builds that does not change the state of the shared daemon, or for isolating builds for different projects. You can create a new instance for a set of remote nodes, forming a build farm, and quickly switch between them.
You can create new instances using the docker buildx create command. This creates a new builder instance with a single node based on your current configuration. To check all available builders you can use ls command. You can also switch between different builders by using this command docker buildx use <name>.After running this command, the build commands will automatically use this builder.
Now use BuildX to build Multi-platform Images!
We can build multi-platform images using three different strategies that are supported by Buildx and Dockerfiles:
- Using the QEMU emulation support in the kernel.
In particular, this approach is considered to be the most suitable approach when one is working on a Docker Desktop ( usually Mac or Windows ). QEMU is the easiest way to get started if your node already supports it (for example. if you are using Docker Desktop). It requires no changes to your Dockerfile and BuildKit automatically detects the secondary architectures that are available. - Building on multiple native nodes using the same builder instance.
This approach is suitable for covering all the use-cases that are not handled efficiently by the QEMU approach, and as a result, we achieve better performance. - Using a stage in Dockerfile to cross-compile to different architectures.
In this case, multi-stage builds in Dockerfiles can be effectively used to build binaries for the platform specified — platform using the native architecture of the build node.
If you want to build images on multi platform, follow the detailed and very clear steps given here.
Creating a Buildx Builder
The docker CLI now understands the buildx command, but you also need to create a new builder object which buildx can use:
$ docker buildx create --name myImagebuilder
myImagebuilder
$ docker buildx use myImagebuilderYou can check your newly created myImagebuilder with:
$ docker buildx inspect --bootstrap
[+] Building 3.5s (1/1) FINISHED
=> [internal] booting buildkit 3.8s
=> => pulling image moby/buildkit:buildx-stable-1 2.8s
=> => creating container buildx_buildkit_myImagebuilder0 0.8s
Name: myImagebuilder
Driver: docker-containerNodes:
Name: myImagebuilder0
Endpoint: unix:///var/run/docker.sock
Status: running
Platforms: linux/amd64, linux/arm64, linux/riscv64, linux/ppc64le,
linux/s390x, linux/386, linux/arm/v7, linux/arm/v6
Note how the Platforms line reports support for various non-native architectures which you have installed via QEMU. If it only reports support for linux/amd64 and linux/386 you either still haven’t met all software requirements, or you had created a builder before you have met the software requirements. In the latter case remove it with docker buildx rm and recreate it.
You can also see your just created myImagebuilder with buildx’ ls subcommand:
$ docker buildx ls
NAME/NODE DRIVER/ENDPOINT STATUS PLATFORMS
myImagebuilder * docker-container
myImagebuilder0 unix:///var/run/docker.sock running linux/amd64, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6
default docker
default default running linux/amd64, linux/arm64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6Buildx will Build your images..
Alright, now we’re ready to build multi-architecture docker images with buildx. To have something concrete to work with we’re going to use the following example Dockerfile:
It’s a simple stand-in for whatever you’d like to build yourself in your own Dockerfile. It uses the latest Alpine distribution which itself is a multi-architecture docker image and it prints out the architecture on which it is executing. That will allow us to check which kind of image we’re running.
Get Quassain Raza’s stories in your inbox
Join Medium for free to get updates from this writer.
The docker buildx build subcommand has a number of flags which determine where the final image will be stored. By default, i.e. if none of the flags are specified, the resulting image will remain captive in docker’s internal build cache. This is unlike the regular docker build command which stores the resulting image in the local docker images list. The important flags are:
- — load: This flag instructs docker to load the resulting image into the local docker images list. However, this currently only works for single-architecture images. If you try this with multi-architecture images you’ll get an export error:
$ docker buildx build … --load …
…
=> ERROR exporting to oci image format 0.0s
— — —
> exporting to oci image format:
— — —
failed to solve: rpc error: code = Unknown desc = docker
exporter does not currently support exporting manifest lists- — push : This flag tells docker to push the resulting image to a docker registry. Your image tag has to contain the proper reference to the registry and repository name. This currently is the best way to store multi-architecture images.
We’re going to use the default Docker Hub registry. First we have to log in:
$ export DOCKER_USER=’quassainraza’
$ docker login -u “$DOCKER_USER”
Password: *****
WARNING! Your password will be stored unencrypted in /home/ubuntu/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-storeLogin SucceededNow we can build and use the — push flag to push the image to Docker Hub. In our example we’re going to build for three different architectures — x86, ARM, and PowerPC — which are specified with the — platform flag:
$ docker buildx build -t “${DOCKER_USER}/buildx-test:latest” \
--platform linux/amd64,linux/arm64,linux/ppc64le --push .[+] Building 1.5s (9/9) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 91B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [linux/ppc64le internal] load metadata for docker.io/libra 0.1s
=> [linux/amd64 internal] load metadata for docker.io/library 0.2s
=> [linux/arm64 internal] load metadata for docker.io/library 0.2s
=> CACHED [linux/ppc64le 1/1] FROM docker.io/library/alpine:l 0.0s
=> => resolve docker.io/library/alpine:latest@sha256:21716586 0.0s
=> CACHED [linux/amd64 1/1] FROM docker.io/library/alpine:lat 0.0s
=> => resolve docker.io/library/alpine:latest@sha256:21716586 0.0s
=> CACHED [linux/arm64 1/1] FROM docker.io/library/alpine:lat 0.0s
=> => resolve docker.io/library/alpine:latest@sha256:21716586 0.0s
=> exporting to image 1.2s
=> => exporting layers 0.0s
=> => exporting manifest sha256:cc57b693aba3acedeec2e624b55e5 0.0s
=> => exporting config sha256:d90689831fec93dd68f90509031907c 0.0s
=> => exporting manifest sha256:7997da18dbf6e4b2748736821ec6f 0.0s
=> => exporting config sha256:c105b7a88a6002ad96802aa287be387 0.0s
=> => exporting manifest sha256:6ed2267dc7082fbfc4454805b9432 0.0s
=> => exporting config sha256:5df18a6f10cc8591839d1b29732d3d2 0.0s
=> => exporting manifest list sha256:57ca2d778839da0b6287bcbc 0.0s
=> => pushing layers 0.2s
=> => pushing manifest for docker.io/quassainraza/buildx-tes 0.7sWe can check the image with the imagetools subcommand which confirms that three architecture versions are included in the image:
$ docker buildx imagetools inspect “$DOCKER_USER/buildx-test:latest”
Name: docker.io/quassainraza/buildx-test:latest
MediaType: application/vnd.docker.distribution.manifest.list.v2+json
Digest: sha256:57ca2d778839da0b6287bcbc99fc2299b0cea29e5fe4cff8492a1ba6e62fc8c5Manifests:
Name: docker.io/quassainraza/buildx-test:latest@sha256:cc57b693aba3acedeec2e624b55e543919173a3e9ed76c2af2f2cd9713b84b78
MediaType: application/vnd.docker.distribution.manifest.v2+json
Platform: linux/amd64 Name: docker.io/quassainraza/buildx-test:latest@sha256:7997da18dbf6e4b2748736821ec6f1dd11d6e8493f068fb23913507a9a271e2c
MediaType: application/vnd.docker.distribution.manifest.v2+json
Platform: linux/arm64 Name: docker.io/quassainraza/buildx-test:latest@sha256:6ed2267dc7082fbfc4454805b94326418ad14c530879a7c9d6f02f0961e2899c
MediaType: application/vnd.docker.distribution.manifest.v2+json
Platform: linux/ppc64le
To verify that you’ve actually got what you’ve been promised, let’s try to run the image:
$ docker run --rm “$DOCKER_USER/buildx-test:latest”
Unable to find image ‘quassainraza/buildx-test:latest’ locally
latest: Pulling from quassainraza/buildx-test
Digest: sha256:57ca2d778839da0b6287bcbc99fc2299b0cea29e5fe4cff8492a1ba6e62fc8c5
Status: Downloaded newer image for quassainraza/buildx-test:latest
Running on x86_64As expected, since we’re running on a 64-bit x86 host, the default architecture version that was used by docker was the amd64 which reports running on x86_64. If you check the local image in docker it confirms that:
$ docker inspect --format “{{.Architecture}}” “$DOCKER_USER/buildx-test:latest”
amd64
To pull and run a specific architecture version, use the image name including its full sha256 value that was reported by imagetools:
$ docker run — rm “$DOCKER_USER/buildx-test:latest@sha256:6ed2267dc7082fbfc4454805b94326418ad14c530879a7c9d6f02f0961e2899c”
Unable to find image ‘quassainraza/buildx-test:latest@sha256:6ed2267dc7082fbfc4454805b94326418ad14c530879a7c9d6f02f0961e2899c’ locally
sha256:6ed2267dc7082fbfc4454805b94326418ad14c530879a7c9d6f02f0961e2899c: Pulling from quassainraza/buildx-test
a5dee701e1e8: Pull complete
Digest: sha256:6ed2267dc7082fbfc4454805b94326418ad14c530879a7c9d6f02f0961e2899c
Status: Downloaded newer image for quassainraza/buildx-test@sha256:6ed2267dc7082fbfc4454805b94326418ad14c530879a7c9d6f02f0961e2899c
Running on ppc64leSince the sha256 value we requested here was that of the PowerPC image version, we see that the image is reporting to run on ppc64le as expected.
Optionally, we can pull and run non-native image versions by platform name. For that though we need to turn on another experimental feature, this time in the docker engine, that’ll allow us to specify a --platform. If docker engine experimental features are not turned on you’ll get an error instead:
“–platform” is only supported on a Docker daemon with experimental features enabledChange the docker engine configuration file /etc/docker/daemon.json or create one if it doesn’t exist already:
{
…
“experimental” : true
}After changing the configuration file you’ll also need to restart dockerd for the change to take effect:
$ sudo systemctl restart dockerLet’s purge the image that we’ve already pulled and try a different architecture:
$ docker rmi “$DOCKER_USER/buildx-test:latest”
Untagged: quassainraza/buildx-test:latest
Untagged: quassainraza/buildx-test@sha256:57ca2d778839da0b6287bcbc99fc2299b0cea29e5fe4cff8492a1ba6e62fc8c5$ docker run --rm --platform linux/aarch64 “$DOCKER_USER/buildx-test:latest”
Unable to find image ‘quassainraza/buildx-test:latest’ locally
latest: Pulling from quassainraza/buildx-test
cde5963f3b93: Pull complete
Digest: sha256:57ca2d778839da0b6287bcbc99fc2299b0cea29e5fe4cff8492a1ba6e62fc8c5
Status: Downloaded newer image for quassainraza/buildx-test:latest
Running on aarch64Now we see that the architecture version of the image we’ve pulled and run is the one for 64-bit ARM aarch64, as can also be verified by looking at the image metadata:
$ docker inspect --format “{{.Architecture}}” “$DOCKER_USER/buildx-test:latest”
arm64With this you’ve got to the point where you can start to build your own multi-architecture docker images with buildx.
To Install BuildX as your default builder
To set buildX as your default builder run the command docker buildx install and it sets up docker builder command as an alias to docker buildx . This results in the ability to have docker build use the current buildx builder.
To remove this alias, run docker buildx uninstall.







