Linux Platform Information
Here are a few
- Linux/AMD64 is currently the most mainstream X86_64
- linux/arm64
- linux/arm
- linux/arm/v6
- linux/arm/v7
The problem
Linux has many platforms, is there a way to build all platform images in just one build? The answer is yes, and here are the tools to do just that.
Docker Buildx
Docker Buildx is a CLI plug-in that extends the Docker command and fully supports the functionality provided by the Moby BuildKit Builder toolkit. It provides the same user experience as docker builds and has many new features, such as creating scoped builder instances and building against multiple nodes at once.
The installation
- Installed directly
Docker v19.03
Version, which is already includedDocker Buildx
Component, because currentlyThe experimental function
, is disabled by default. By setting theDOCKER_CLI_EXPERIMENTAL
Environment variable to enable.
$ export DOCKER_CLI_EXPERIMENTAL=enabled
Copy the code
Switch to the Docker Buildx builder
Create myBuilder
$ docker buildx create --use --name mybuilder
Verify that the builder works
$ docker buildx ls
NAME/NODE DRIVER/ENDPOINT STATUS PLATFORMS
mybuilder * docker-container
mybuilder0 unix:///var/run/docker.sock running linux/amd64, linux/386
default docker
default default running linux/amd64, linux/386
Copy the code
Build multi-platform images
- create
Dockerfile
# Use node image to print a current running platform
$ vim Dockerfile
FROM --platform=$BUILDPLATFORM node:alpine AS build
ARG TARGETPLATFORM
ARG BUILDPLATFORM
RUN echo "I am running on $BUILDPLATFORM, building for $TARGETPLATFORM" > /log
FROM alpine
COPY --from=build /log /log
CMD ["cat"."/log"]
Copy the code
- Constitute Linux/ARM, Linux/ARM64, Linux/AMD64 images
# Configure the pushed mirror repository before building. The images currently built will not be saved locally. Here I use the DockerHub image repository
$ docker buildx build -t yangpeng2468/test- platform = Linux/arm, Linux/arm64, Linux/amd64. - push [+] Building 15.8 s (17/17) FINISHED = > (internal) load build Definition from Dockerfile 0.9s => => 32B transferring-to-flight context => [internal] load.dockerignore 2B 0.0s => [Linux/AMd64 internal] Load metadataforDocker. IO/library/alpine: latest 0.9 s = > Linux/arm/v7 internal load metadataforDocker. IO/library/alpine: latest 2.1 s = > Linux/arm64 internal load metadataforDocker. IO/library/alpine: latest 2.1 s = > Linux/amd64 internal load metadataforDocker. IO/library/node: alpine 2.4 s = > [Linux/amd64 build 1/2] the FROM docker.io/library/node:alpine@sha256:bdf054f006078036f72de45553f3b11176c1c00d5451d8fc2 af206636eb54d70 0.0 s = > = > resolve docker. IO/library/node: alpine @ sha256: bdf054f006078036f72de45553f3b11176c1c00d5451d8fc2af206636eb54D70 0.0s => [Linux /arm64 stage-1 1/2] FROM Docker. IO/library/alpine @ sha256:0.0 s = > c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a CACHED [linux/amd64 build 2/2] RUNecho "I am running on linux/amd64, building for linux/arm64" > /log0.0s => CACHED [Linux /arm64 stage-1 2/2] COPY --from=build /log /log0.0s => [Linux/ARM /v7 stage-1 1/2] FROM Docker. IO/library/alpine @ sha256:0.0 s = > c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a CACHED [linux/amd64 build 2/2] RUNecho "I am running on linux/amd64, building for linux/arm/v7" > /log0.0s => CACHED [Linux /arm/v7 stage-1 2/2] COPY --from=build /log /log0.0s => [Linux/AMd64 stage-1 1/2] FROM Docker. IO/library/alpine @ sha256:0.0 s = > = > c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a resolve Docker. IO/library/alpine @ sha256:0.0 s = > c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a CACHED [linux/amd64 build 2/2] RUNecho "I am running on linux/amd64, building for linux/amd64" > /log0.0s => CACHED [Linux /amd64 stage-1 2/2] COPY --from=build /log /log0.0s => exporting layers 0.0s => => exporting manifest Sha256:9691 c28fd9a98d735f05c913f61165f1367323eca6784a852d457fa1f74dab84 0.0 s = > = > exporting config sha256:0682fde125dbb5923e494a1dfd6c807bc59d2e9fc7120b57E49074CC7AE7E9F4 0.0s => => Exporting manifest Sha256:59 c54d4c1b1bee1bd2c23f38f9f2bffc292d0236a4032fba7e9be26fbe0d2802 0.0 s = > = > exporting config Sha256: ac7414caf47ea7db5cc95e5fa9cf65bb98ff8def55f22c08377ad76da4f59260 0.0 s = > = > exporting the manifest Sha256:8 f73236eead90974fa8ec0b15b5ae1b193786adbfc2d612bb1ab0c272957d3f8 0.0 s = > = > exporting config Sha256:59 cb995bf069ab54813fb32e9bd039cafb3830eadf04cc23cb960e134468eb25 0.0 s = > = > exporting manifest the list Sha256:72 d368f9a6696dc9551ec250d8a2e54bab85b8d0cb784e81cb4a7742090890a3 0.0 s = > = > pushing the layers 10.7 s = > = > pushing manifestfor docker.io/yangpeng2468/test:latest
Copy the code
- View Linux/ARM, Linux/ARM64, Linux/AMD64 images
$ docker buildx imagetools inspect yangpeng2468/test
Name: docker.io/yangpeng2468/test:latest
MediaType: application/vnd.docker.distribution.manifest.list.v2+json
Digest: sha256:72d368f9a6696dc9551ec250d8a2e54bab85b8d0cb784e81cb4a7742090890a3
Manifests:
Name: docker.io/yangpeng2468/test:latest@sha256:9691c28fd9a98d735f05c913f61165f1367323eca6784a852d457fa1f74dab84
MediaType: application/vnd.docker.distribution.manifest.v2+json
Platform: linux/arm/v7
Name: docker.io/yangpeng2468/test:latest@sha256:59c54d4c1b1bee1bd2c23f38f9f2bffc292d0236a4032fba7e9be26fbe0d2802
MediaType: application/vnd.docker.distribution.manifest.v2+json
Platform: linux/arm64
Name: docker.io/yangpeng2468/test:latest@sha256:8f73236eead90974fa8ec0b15b5ae1b193786adbfc2d612bb1ab0c272957d3f8
MediaType: application/vnd.docker.distribution.manifest.v2+json
Platform: linux/amd64
Copy the code
Test multi-platform mirroring
Since my environment is Linux/AMD64, I need to enable binfmT_misc if I am testing other platform images
binfmt_misc
Opening method
$ docker run --rm --privileged docker/binfmt:66f9012c56a8316f9244ffd7622d7c21c1f6f28d
Copy the code
- To view
binfmt_misc
Are the Settings correct?
$ls -la /proc/sys/fs/binfmt_misc/ -rw-r--r-- 1 root root 0 November 29 20:45 qemu-aarch64-rw-r --r-- 1 root root 0 November 29 20:45 qemu-arm-rw-r --r-- 1 root root 0 November 29 20:45 qemu-ppc64le-rw-r --r-- 1 root root 0 November 29 20:45 qemu-s390x --w------- 1 root root 0 November 29 20:29 register-rw-r --r-- 1 root root 0 November 29 20:29 statusCopy the code
- Verify Linux/ARM, Linux/ARM64, Linux/AMD64 images
$ docker run -it --rm docker.io/yangpeng2468/test:latest@sha256:9691c28fd9a98d735f05c913f61165f1367323eca6784a852d457fa1f74dab84
I am running on linux/amd64, building for linux/arm/v7
$ docker run -it --rm docker.io/yangpeng2468/test:latest@sha256:59c54d4c1b1bee1bd2c23f38f9f2bffc292d0236a4032fba7e9be26fbe0d2802
I am running on linux/amd64, building for linux/arm64
$ docker run -it --rm docker.io/yangpeng2468/test:latest@sha256:8f73236eead90974fa8ec0b15b5ae1b193786adbfc2d612bb1ab0c272957d3f8
I am running on linux/amd64, building for linux/amd64
Copy the code
conclusion
Buildx will probably be part of the Docker build command in the future.
Refer to the link
- Docs.docker.com/buildx/work…
- www.infoq.cn/article/V9Q…
Follow public account
We welcome you to share automated operations, DevOps, Kubernetes, Service Mesh and Cloud Native regularly