Setting up docker builds for multiple platforms or architectures
is not very complicated, but at least I found the instructions on
the Internet scarce, lacking and somehow confusing.
If you want to build multiarch/multiplatform container images with docker
on your local machine, with buildx and qemu and so forth, this document
describes a fast track to accomplish it. I’ll describe all steps a bit,
but to be honest I don’t know much more about those steps either at this
point.
My instructions are applicable for Ubuntu 22.04, but should work if
slightly adjusted for other distros as well. I’ll be building for
amd64 and arm64.
Set up a docker ubuntu repository and install packages
First remove any possibly installed OS docker packages to avoid conflicts:
Add the docker apt repository and gpg key:
Then install docker packages:
Enable buildkit in docker daemon
Create or edit your /etc/docker/daemon.json to contain the following:
Restart your docker daemon:
Start a registry container
Docker image cache doesn’t understand multiplatform images, so we’ll
need something that is more like a real registry. So install one as a container:
Create docker contexts for builds
The convention seems to be that one has to create a docker context for each
“builder node”. I’ll create a single context, and a single builder node per
platform. So:
Check that you have things approximately like this:
Create buildx builders for both platforms
Create a config.toml file with the following content:
Next we’ll create a buildx builders and add two nodes under it:
Check buildx builders and nodes
Build something
Now instead of the usual docker build you’ll do docker buildx build like so:
You will need to push the results to a “real” repository, so the image name
has to have the repository location as a prefix, and the build results must
be pushed to a real registyr, hence the “–push” switch.
You can inspect the buildresult like this:
.. and the output should indicate that the image has manifests for all built platforms.
Run non-native platform container images through QEMU emulation
Install QEMU and binfmt support
Run some magic scripts to enable emulation:
Run some containers with different platforms:
So there you have it. Multiarch builds plus the ability to
run alien architecture images with emulation.