Docker Buildx

Quassain RazaQuassain Raza
3 min read

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 .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:

1. 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.

2. 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.

3. 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.

buildx.png

If you want to build images on multi platform, follow the detailed and very clear steps given here.

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.

0
Subscribe to my newsletter

Read articles from Quassain Raza directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Quassain Raza
Quassain Raza

I am a dedicated and trustworthy individual. I am an accomplished coder and programmer, and I enjoy using my skills to contribute to the exciting technological advances that happen every day