当前位置:网站首页>Best practices for building multi architecture images
Best practices for building multi architecture images
2022-07-02 15:56:00 【Hua Weiyun】
author :xcbeyond
Blog :https://xcbeyond.cn/ official account : Program ape technology
In the era of the cloud , Containerization has become a fact , Package software products 、 To build a Docker Mirroring is the most basic 、 The most important step . In the context of Xinchuang , There will be... In the cloud environment x86、arm And so on , Therefore, when building images, you need to build images of multiple architectures , To adapt to servers with different architectures .
Pulling Docker Mirror image , Automatically pull the image of the corresponding architecture according to the architecture of the current environment , Such as : stay x86 The environment drop-down image is x86 The mirror image of Architecture , stay arm The environment drop-down image is arm The mirror image of Architecture .( Premise is , The image is a multi architecture image )
This article will focus on Docker Buildx To build a multi architecture image expansion description ( Build a multi architecture image at once ).
1、buildx install
Docker Buildx It's a CLI plug-in unit , It expands Docker command , Fully support Moby BuildKit Functions provided by the builder toolkit . It provides with docker build
The same user experience , With many new features , For example, create scope builder instances and build against multiple nodes at the same time .
- download buildx Binary .
stay Github Release page Download the latest buildx Binary .
Select the corresponding binary file according to your operating system . - buildx Copied to the Docker Corresponding directory .
Rename downloaded buildx Binary , And copy it to the directory corresponding to your operating system :
operating system | Binary name | Destination folder |
Linux | docker-buildx | $HOME/.docker/cli-plugins |
MacOS | docker-buildx | $HOME/.docker/cli-plugins |
Windows | docker-buildx.exe | %USERPROFILE%\.docker\cli-plugins |
for example , my MacOS:
[email protected] ~ % mv ~/Downloads/buildx-v0.7.1.darwin-arm64 ~/.docker/cli-plugins/[email protected] ~ % chmod +x ~/.docker/cli-plugins/docker-buildx
Be careful : Need for buildx Binaries give permissions , perform chmod +x
.
- Check buildx.
performdocker buildx version
:
[email protected] ~ % docker buildx versiongithub.com/docker/buildx v0.7.1 05846896d149da05f3d6fd1e7770da187b52a247
2、 Turn on binfmt_misc To run non local architectures Docker Mirror image
If you are using Mac perhaps Windows edition Docker The desktop version , You can skip this step , because binfmt_misc Default on .
If you use other platforms , You can use tonistiigi/binfmt Image for installation :
docker run --privileged --rm tonistiigi/binfmt --install all
3、 Default Docker The builder switches to a multi architecture builder
By default ,Docker Will use the default builder , Multi architecture construction is not supported .
In order to build a multi architecture image , You need to create a new builder that supports multiple architectures , To be implemented docker buildx create --use
:
[email protected] % docker buildx create --use --name mybuildermybuilder
See if the new multi architecture builder is working , To be implemented docker buildx ls
:
[email protected] % docker buildx lsNAME/NODE DRIVER/ENDPOINT STATUS PLATFORMSmybuilder * docker-container mybuilder0 unix:///var/run/docker.sock inactive desktop-linux docker desktop-linux desktop-linux running linux/arm64, linux/amd64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6default docker default default running linux/arm64, linux/amd64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6
4、 Build a multi architecture image
- Write a test program .
For the convenience of testing , use Golang I wrote a simple one Program , And output the architecture information of the current program running environment :
package mainimport ( "fmt" "runtime")func main() { fmt.Printf("the current platform architecture is %s.\n", runtime.GOARCH)}
- To write Dockerfile.
The Dockerfile:
FROM xcbeyond/go-builder:1.16LABEL maintainer xcbeyondWORKDIR /appCOPY multi-arch-test.go /appRUN go build -o multi-arch-test /app/multi-arch-test.goCMD ["./multi-arch-test"]
- Build a multi architecture image .
Through the commanddocker buildx build -t --platform=linux/arm64,linux/amd64 . --push
, Build a support arm64 and amd64 Multi architecture image of Architecture , And push it to Docker Hub:
( Parameters--push
Will automatically push the image to Docker Hub, The image is not retained locally . If you don't want to push , This parameter can be removed .)
[email protected] build-multi-platform-images-best-practices % docker buildx build -t xcbeyond/multi-arch-test --platform=linux/arm64,linux/amd64 . --push[+] Building 3105.4s (19/19) FINISHED => [internal] load build definition from Dockerfile 0.0s=> => transferring dockerfile: 222B 0.0s=> [internal] load .dockerignore 0.0s=> => transferring context: 2B 0.0s=> [linux/amd64 internal] load metadata for docker.io/xcbeyond/go-builder:1.16 5.2s=> [linux/arm64 internal] load metadata for docker.io/xcbeyond/go-builder:1.16 4.9s=> [auth] xcbeyond/go-builder:pull token for registry-1.docker.io 0.0s=> [internal] load build context 0.0s=> => transferring context: 40B 0.0s=> [linux/amd64 1/4] FROM docker.io/xcbeyond/go-builder:[email protected]:ace56967e44e98b1a8c286ad97717c878650d0312ac3e69767234f766601d6fc 2323.9s=> => resolve docker.io/xcbeyond/go-builder:[email protected]:ace56967e44e98b1a8c286ad97717c878650d0312ac3e69767234f766601d6fc 0.0s=> => sha256:6e1d20a8313edd44a36cb9198f4d11ac5eedb41d1faf5e8a33c5a0a5a25d2b92 85.80MB / 85.80MB 2318.7s=> => sha256:6494e4811622b31c027ccac322ca463937fd805f569a93e6f15c01aade718793 54.57MB / 54.57MB 27.3s=> => sha256:5b59121a0c3517cfd68aba8a3955cbb40ca12dd56db3317e3a17cf56131ca915 129.08MB / 129.08MB 77.1s=> => sha256:cb5b7ae361722f070eca53f35823ed21baa85d61d5d95cd5a95ab53d740cdd56 10.87MB / 10.87MB 96.5s=> => sha256:9b829c73b52b92b97d5c07a54fb0f3e921995a296c714b53a32ae67d19231fcd 5.15MB / 5.15MB 5.2s=> => sha256:0e29546d541cdbd309281d21a73a9d1db78665c1b95b74f32b009e0b77a6e1e3 54.92MB / 54.92MB 61.0s=> => extracting sha256:0e29546d541cdbd309281d21a73a9d1db78665c1b95b74f32b009e0b77a6e1e3 1.5s=> => extracting sha256:9b829c73b52b92b97d5c07a54fb0f3e921995a296c714b53a32ae67d19231fcd 0.1s=> => extracting sha256:cb5b7ae361722f070eca53f35823ed21baa85d61d5d95cd5a95ab53d740cdd56 0.3s=> => extracting sha256:6494e4811622b31c027ccac322ca463937fd805f569a93e6f15c01aade718793 1.6s=> => extracting sha256:6e1d20a8313edd44a36cb9198f4d11ac5eedb41d1faf5e8a33c5a0a5a25d2b92 1.9s=> => extracting sha256:5b59121a0c3517cfd68aba8a3955cbb40ca12dd56db3317e3a17cf56131ca915 3.3s=> => extracting sha256:2db41f0db9d9d9a1f49978ec31e8d187f491767e9b377f5ee121827c407e015e 0.0s=> [linux/arm64 1/4] FROM docker.io/xcbeyond/go-builder:[email protected]:ace56967e44e98b1a8c286ad97717c878650d0312ac3e69767234f766601d6fc 2187.3s=> => resolve docker.io/xcbeyond/go-builder:[email protected]:ace56967e44e98b1a8c286ad97717c878650d0312ac3e69767234f766601d6fc 0.0s=> => sha256:221ff675cd357b3b345da24f781791fc9b91066d687f9e928993aab31618d13d 99.63MB / 99.63MB 54.3s=> => sha256:627b3401fb617535edd16e96bd5941ecea7fe10ce6087bd47707602cfc396c2b 81.01MB / 81.01MB 472.1s=> => sha256:841dd868500b6685b6cda93c97ea76e817b427d7a10bf73e9d03356fac199ffd 54.67MB / 54.67MB 2084.9s=> => sha256:aa9c5b49b9db3dd2553e8ae6c2081b77274ec0a8b1f9903b0e5ac83900642098 10.66MB / 10.66MB 6.2s=> => sha256:ac9d381bd1e98fa8759f80ff42db63c8fce4ac9407b2e7c8e0f031ed9f96432b 5.14MB / 5.14MB 6.8s=> => sha256:94a23d3cb5be24659b25f17537307e7f568d665244f6a383c1c6e51e31080749 53.60MB / 53.60MB 22.7s=> => extracting sha256:94a23d3cb5be24659b25f17537307e7f568d665244f6a383c1c6e51e31080749 1.5s=> => extracting sha256:ac9d381bd1e98fa8759f80ff42db63c8fce4ac9407b2e7c8e0f031ed9f96432b 0.1s=> => extracting sha256:aa9c5b49b9db3dd2553e8ae6c2081b77274ec0a8b1f9903b0e5ac83900642098 0.2s=> => extracting sha256:841dd868500b6685b6cda93c97ea76e817b427d7a10bf73e9d03356fac199ffd 1.5s=> => extracting sha256:627b3401fb617535edd16e96bd5941ecea7fe10ce6087bd47707602cfc396c2b 1.7s=> => extracting sha256:221ff675cd357b3b345da24f781791fc9b91066d687f9e928993aab31618d13d 2.7s=> => extracting sha256:10c716f05a00666c190fb40f99503dc83b2886de744ab9b3dc6c5d37d01f6e49 0.0s=> [auth] xcbeyond/go-builder:pull token for registry-1.docker.io 0.0s=> [linux/arm64 2/4] WORKDIR /app 0.1s=> [linux/arm64 3/4] COPY multi-arch-test.go /app 0.0s=> [linux/arm64 4/4] RUN go build -o multi-arch-test /app/multi-arch-test.go 0.3s=> [linux/amd64 2/4] WORKDIR /app 0.2s=> [linux/amd64 3/4] COPY multi-arch-test.go /app 0.0s=> [linux/amd64 4/4] RUN go build -o multi-arch-test /app/multi-arch-test.go 1.5s=> exporting to image 774.4s=> => exporting layers 10.5s=> => exporting manifest sha256:f38420339c9665b4f7d8b1e5edc66dc09e596ec7aa78113914b8a5b1b900e9e2 0.0s=> => exporting config sha256:628f504898973d1d935ffd37993d1db5fdc4715c5d58467db2c6531d3d7d3181 0.0s=> => exporting manifest sha256:a259302dca0d3f432d08f578e3243c5244bc50e54ea561c80dba03622897fcfd 0.0s=> => exporting config sha256:47e7af533f821be5ba256919303e0de3b39990109a419a98dbfddc6c82086822 0.0s=> => exporting manifest list sha256:8417543ebc47e6040a67a392d6ee9de05735ed464e48b22fcc3bdf66ce22224b 0.0s=> => pushing layers 761.8s=> => pushing manifest for docker.io/xcbeyond/multi-arch-test:[email protected]:8417543ebc47e6040a67a392d6ee9de05735ed464e48b22fcc3bdf66ce22224b 2.0s=> [auth] xcbeyond/multi-arch-test:pull,push token for registry-1.docker.io 0.0s=> [auth] xcbeyond/multi-arch-test:pull,push token for registry-1.docker.io 0.0s=> [auth] xcbeyond/multi-arch-test:pull,push token for registry-1.docker.io 0.0s
5、 Test multi architecture image
Mirror the built multi architecture xcbeyond/multi-arch-test:latest To test , To ensure normal operation , And use the corresponding schema image to output the matching schema information .
- View the information of each schema image .
Carry out ordersdocker manifest inspect
, You can view the image detailed list , And can know the image of the corresponding architecture SHA value :
[email protected] build-multi-platform-images-best-practices % docker manifest inspect xcbeyond/multi-arch-test:latest{ "schemaVersion": 2, "mediaType": "application/vnd.docker.distribution.manifest.list.v2+json", "manifests": [ { "mediaType": "application/vnd.docker.distribution.manifest.v2+json", "size": 2419, "digest": "sha256:f38420339c9665b4f7d8b1e5edc66dc09e596ec7aa78113914b8a5b1b900e9e2", "platform": { "architecture": "arm64", "os": "linux" } }, { "mediaType": "application/vnd.docker.distribution.manifest.v2+json", "size": 2420, "digest": "sha256:a259302dca0d3f432d08f578e3243c5244bc50e54ea561c80dba03622897fcfd", "platform": { "architecture": "amd64", "os": "linux" } } ]}
You can also directly Docker Hub You can directly see the multi architecture information supported by the image :
- Mirrored according to different architectures SHA value , To run the image one by one , And see its output .
Separatelydocker run --rm
command :
arm The mirror image of Architecture :
[email protected] build-multi-platform-images-best-practices % docker run --rm xcbeyond/multi-arch-test:[email protected]:f38420339c9665b4f7d8b1e5edc66dc09e596ec7aa78113914b8a5b1b900e9e2Unable to find image 'xcbeyond/multi-arch-test:[email protected]:f38420339c9665b4f7d8b1e5edc66dc09e596ec7aa78113914b8a5b1b900e9e2' locallydocker.io/xcbeyond/[email protected]:f38420339c9665b4f7d8b1e5edc66dc09e596ec7aa78113914b8a5b1b900e9e2: Pulling from xcbeyond/multi-arch-test94a23d3cb5be: Pull complete ac9d381bd1e9: Pull complete aa9c5b49b9db: Pull complete 841dd868500b: Pull complete 627b3401fb61: Pull complete 221ff675cd35: Pull complete 10c716f05a00: Pull complete 15bae122089f: Pull complete d764b5be0a55: Pull complete 34bfa57028a2: Pull complete Digest: sha256:f38420339c9665b4f7d8b1e5edc66dc09e596ec7aa78113914b8a5b1b900e9e2Status: Downloaded newer image for xcbeyond/[email protected]:f38420339c9665b4f7d8b1e5edc66dc09e596ec7aa78113914b8a5b1b900e9e2the current platform architecture is arm64.
x86 The mirror image of Architecture :
[email protected] build-multi-platform-images-best-practices % docker run --rm xcbeyond/multi-arch-test:[email protected]:a259302dca0d3f432d08f578e3243c5244bc50e54ea561c80dba03622897fcfdUnable to find image 'xcbeyond/multi-arch-test:[email protected]:a259302dca0d3f432d08f578e3243c5244bc50e54ea561c80dba03622897fcfd' locallydocker.io/xcbeyond/[email protected]:a259302dca0d3f432d08f578e3243c5244bc50e54ea561c80dba03622897fcfd: Pulling from xcbeyond/multi-arch-test0e29546d541c: Pull complete 9b829c73b52b: Pull complete cb5b7ae36172: Pull complete 6494e4811622: Pull complete 6e1d20a8313e: Pull complete 5b59121a0c35: Pull complete 2db41f0db9d9: Pull complete f708bf6a9dc8: Pull complete cb0d69b97354: Pull complete a8f5aa83ecfb: Pull complete Digest: sha256:a259302dca0d3f432d08f578e3243c5244bc50e54ea561c80dba03622897fcfdStatus: Downloaded newer image for xcbeyond/[email protected]:a259302dca0d3f432d08f578e3243c5244bc50e54ea561c80dba03622897fcfdthe current platform architecture is amd64.
The output above , In line with our expectations : The multi architecture image is successfully built , And can run in their own architecture environment .
6、 summary
Multi architecture mirroring is based on Docker Buildx Built , at present buildx Additional installation is required , future buildx It's likely to be docker build Part of the order , No additional installation required , After all, multi architecture mirroring has been widely used in various scenarios .
Reference article :
边栏推荐
- (Wanzi essence knowledge summary) basic knowledge of shell script programming
- Astra: could not open "2bc5/ [email protected] /6“: Failed to set USB interface
- PostgresSQL 流复制 主备切换 主库无读写宕机场景
- /bin/ld: 找不到 -lxml2
- Idea jar package conflict troubleshooting
- /Bin/ld: cannot find -lcrypto
- beforeEach
- Crawl the information of national colleges and universities in 1 minute and make it into a large screen for visualization!
- 隐藏在 Nebula Graph 背后的星辰大海
- 智联招聘的基于 Nebula Graph 的推荐实践分享
猜你喜欢
《大学“电路分析基础”课程实验合集.实验六》丨典型信号的观察与测量
[5g NR] RRC connection release
Finally, I understand the event loop, synchronous / asynchronous, micro task / macro task, and operation mechanism in JS (with test questions attached)
如何实现十亿级离线 CSV 导入 Nebula Graph
已知两种遍历序列构造二叉树
【Experience Cloud】如何在VsCode中取得Experience Cloud的MetaData
Application of visualization technology in Nebula graph
The outline dimension function application of small motherboard
Processing gzip: stdin: not in gzip format: child returned status 1tar: error is not recoverable: exitin
基于 Nebula Graph 构建百亿关系知识图谱实践
随机推荐
2278. Percentage of letters in string
Boot 中bean配置覆盖
Song of cactus - throwing stones to ask the way (2)
可视化技术在 Nebula Graph 中的应用
locate: 无法执行 stat () `/var/lib/mlocate/mlocate.db‘: 没有那个文件或目录
全方位解读服务网格(Service Mesh)的背景和概念
lseek 出错
《大学“电路分析基础”课程实验合集.实验六》丨典型信号的观察与测量
基于 Nebula Graph 构建百亿关系知识图谱实践
Boot 事务使用
Pyobject to char* (string)
【Experience Cloud】如何在VsCode中取得Experience Cloud的MetaData
Review materials for the special topic of analog electronics with all essence: basic amplification circuit knowledge points
SQL FOREIGN KEY
Why does the system convert the temp environment variable to a short file name?
Fastjson list to jsonarray and jsonarray to list "suggested collections"
Astra: could not open "2bc5/ [email protected] /6“: Failed to set USB interface
Huawei ECS installs mysqlb for mysqld service failed because the control process exited with error code. See “sys
/Bin/ld: cannot find -llz4
中科大脑知识图谱平台建设及业务实践