buildx — Docker CLI plugin for extended build capabilities with BuildKit
In my last blog I did setup Docker CE (nightly builds) on Raspberry Pi and recently also upgraded my Docker Desktop on Mac to version - 220.127.116.11 with Engine: 19.03.0-beta3 for testing out buildx feature.
To test out this feature let’s write a simple Go program as shown below —
As you can see the compiled code executed on my mac to print out my hostname, operating system and the architecture. Let’s write a Dockerfile for the same and try to run it the Docker way -
Using the old docker build command I could only manage to build an image for linux/amd64 architecture considering that I am working on Docker Desktop on Mac. If I had to build a multi-platform image for the same Go program I would have to build the image on respective platform and then using the
docker manifest command build a local manifest list for annotating and pushing to a registry.
Now let’s use the new
buildx command which helps to achieve multi-node builds for cross-platform images having full buildkit capabilities with container driver. Here are the different commands available with this CLI plugin —
buildx will initially use the “docker” driver providing a very similar user experience to the native
docker build command as shown above. It also currently doesn’t support multiple platforms & auto-push feature.
So to build images for multiple platforms (linux/amd64, linux/arm64, linux/arm/v7) let’s first create a builder using the command —
docker buildx create --name multibuilder
which has “docker-container” as driver.
Once you have create a builder instance you may switch between different builders by
docker buildx use <name> If the current builder is having a status of running then you may see a container running on your host as shown below -
To build multi-platform images execute the command —
docker buildx buildcommand has 2 options passed to it -
-- platform — Set target platform for build
-- push— Shorthand for — output=type=registry i.e. pushes images directly to registry after build.
For more options which may be passed to the above command refer the documentation here.
As you see above command also pushed the manifest along with the docker image — sujaypillai/hello. You may inspect the manifest list using the management command
imagetools available with
docker buildx as shown below —
Let’s check the image uploaded onto
Docker Hub —
Here is the output when I run the same image on my Docker Desktop (Mac), Ubuntu 18.04 & Raspberry Pi -