Building Multi-platform Images with Docker Buildx

Publish: 2022-02-14 | Modify: 2022-02-14

By default, x86_64 platform can only build x86_64 images. If you need to build multi-platform images (such as ARM64) on x86_64 platform, you can use the Buildx tool provided by Docker.

Install Docker Buildx

In this article, we take CentOS 7 x64 system as an example. The latest download page for Docker Buildx is: https://github.com/docker/buildx/releases/latest. The latest version at the time of writing this article is v0.7.1. The installation process is as follows.

# Download Buildx
wget https://github.com/docker/buildx/releases/download/v0.7.1/buildx-v0.7.1.linux-amd64
# Create target folder
mkdir -p $HOME/.docker/cli-plugins
# Move file
mv buildx-v0.7.1.linux-amd64 $HOME/.docker/cli-plugins/docker-buildx
# Add execute permission
chmod +x ~/.docker/cli-plugins/docker-buildx

Initialization

Docker Buildx is an experimental feature and is not enabled by default. You need to modify /etc/docker/daemon.json and add a line:

"experimental": true

to enable this feature. Next, perform the initialization:

docker buildx create --name builderx
docker buildx use builderx
docker buildx inspect --bootstrap

Build multi-platform images

After installing and enabling Docker Buildx, we can use it to build multi-platform images. For example, the original build command is:

build -t ${IMAGE_NAME}:${VERSION} .

The build command using Docker Buildx is:

docker buildx build --platform linux/amd64,linux/arm64 -t ${IMAGE_NAME}:${VERSION} . --push
  • --platform: Specify the platform to build, such as linux/amd64, linux/arm64, or darwin/amd64.
  • --push: Automatically push to Docker Hub repository after building.

After the build is completed, we can find the corresponding tag of the image to see the support for multiple platforms.

If your image supports multiple platforms, when pulling the image, it will automatically download the corresponding platform image based on your platform.

Summary

  • Docker Buildx needs to modify the configuration file to enable this feature.
  • Docker Buildx needs to be initialized before use.
  • When building images with Docker Buildx, it is recommended to add the --push parameter, otherwise the image cannot be seen in docker images. I'm not sure about the specific reason for this.

Some content in this article is referenced from:


Comments