Summary: This article introduces the process of building a multi-CPU architecture image using Docker Buildx.



For image download, domain name resolution and time synchronization, clickAlibaba open source mirror site

An overview,

Compared with X86, ARM architecture has low power consumption and high mobile market share, while X86 has high performance and high server market share.

For Docker containers running the same application with different CPU architectures, Docker images compiled under the corresponding CPU architectures are required.

The traditional method of building multi-schema images is more cumbersome, so here is how to build multi-schema images using Docker Buildx. Building multiple architecture images is made easier by building an ARM architecture image on the X86 architecture. \

Second, the environment

  • Docker Desktop (Mac) \

  • 19.03 + \ Docker Engine

Three, operation steps

1. Start Docker Buildx

Docker Buildx is currently an experimental feature, it is not enabled by default, you need to enable it in docker Desktop preferences

Docker >Preferences >Command Line > Enable experimental features



Run the docker buildx command and the output screenshot is as follows:

2. Build multi-architecture images and push them to Docker Hub

Write a simple Dockerfile for demonstration only:

Echo "FROM python: 3.7 - alpine" > DockerfileCopy the code

List the builder:

$ docker buildx ls
NAME/NODE DRIVER/ENDPOINT STATUS  PLATFORMS
default * docker
  default default         running linux/amd64, linux/arm64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6
Copy the code

We are currently using the default Builder, which is basically the old Builder. Let’s create a new Builder that gives us access to some new multi-architecture functionality. Create a builder:

$ docker buildx create --use --name mybuilder
mybuilder
Copy the code

Check the builder:

$docker buildx inspect -- Bootstrap [+] Building 18.8s (1/1) FINISHED => [internal] booting buildkit 18.8s => = Image moby/ buildKit :buildx-stable-1 18.0s => => Creating container buildx_buildkit_mybuilder0 0.7s Name: mybuilder Driver: docker-container Nodes: Name: mybuilder0 Endpoint: unix:///var/run/docker.sock Status: running Platforms: linux/amd64, linux/arm64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6Copy the code

Build multiple schema images and push to Docker Hub:

docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t donhui/multiarch --push .
Copy the code

The –platform flag instructs Buildx to generate Linux images for Intel 64-bit, Arm 32-bit, and Arm 64-bit architectures.

The –push flag generates a list of multiple schemas and pushes all images to the Docker Hub.



The latest tag will pull images of different schemas based on their environment:

Iv. Thinking and summarizing

In practice, there are constant questions, continuous thinking and continuous solution. Maybe you will also have the same questions: 1. What are the requirements of multi-architecture mirroring on docker version? Docker buildx = 19.03+; The runtime Docker version does not necessarily require 19.03+, I can pull multiple architecture images using 1.13.1 and 18.06.1. Can all Dockerfiles be built into multiple architectures? Or are there any requirements? No dockerfiles are required, and no dockerfiles need to be modified. 3. After the image is built, it is saved in the build cache by default. How to save the image locally? You can specify type as docker, but you must build different images for different CPU architectures and not combine them into one image, as in:

docker buildx build -t donghui/multiarch-armv7 --platform=linux/arm/v7 -o type=docker .
docker buildx build -t donghui/multiarch-arm64 --platform=linux/arm64 -o type=docker .
docker buildx build -t donghui/multiarch-amd64 --platform=linux/amd64 -o type=docker .
Copy the code

You can also push the image to the mirror repository and then pull it. 4. Do you only support pushing to Docker Hub? If you want to push to a private mirror warehouse, what are the requirements for the private mirror warehouse? You can also push an image to a private mirror repository, provided that the repository supports multi-schema mirroring. To push multiple schema images to Harbor, you need Harbor version V2.0.0. Harbor V2.0.0 was released on 2020/05/13 and Harbor V2.0.0 fully supports multiple architecture mirroring. Harbor v2.0.0 update log: github.com/goharbor/ha…

This article was adapted from: Building Multi-CPU Architecture Images with Docker Buildx – Ali Cloud Developer Community