目录
3.开启 binfmt_misc 来运行非本地架构 Docker 镜像
4.将默认 Docker 镜像构建器切换成多架构构建器(选做)
背景
公司有个项目需要私有化交付部署,但是客户的服务器的架构是ARM,我们目前手头的镜像都是x86 64的架构,且我们手头没有架构为ARM的实验服务器。
发现docker高版本有个叫docker buildx的工具可以满足我们的需求,让我们在架构为x86 64的实验服务器上构建出架构为ARM的镜像。
docker builx简介
docker buildx 是一个 docker CLI 插件,其扩展了 docker 命令。
在 Docker 19.03+ 版本中可以使用 docker buildx build命令,底层是基于BuildKit的api 构建镜像。builx命令支持 --platform参数可以同时构建支持多种系统架构的 Docker 镜像,大大简化了构建步骤。
回忆一下,我们平时使用docker镜像的场景,都是去dockerhub官网上,查到相关镜像,然后找到我们想要的tag,直接去我们的服务器上执行:docker pull xxx:version_xx。但是你有没有想过既然docker镜像有多架构版本,你执行pull拉取命名时,到底是拉取的哪个架构的版本?答案是:你执行docker pull命令的当前服务器的架构对应架构的镜像,即你的服务器的架构是什么就会去拉对应架构的镜像,如果dockerhub无对应架构的镜像就会拉取失败。下图是一张多架构镜像的示意图:

使用 docker buildx的前置条件
需要docker版本>=19.03,Linux内核版本>=4.8, binfmt-support >= 2.1.7。
1.linux内核升级
当前实验服务器是一台内核低于4.8、架构为x86 64的centos7.6。
[root@localhost download]# uname -r
3.10.0-957.el7.x86_64
[root@localhost download]# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
[root@localhost download]# rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
[root@localhost download]# yum --enablerepo="elrepo-kernel" list --showduplicates | sort -r | grep kernel-ml.x86_64
kernel-ml.x86_64 5.14.13-1.el7.elrepo elrepo-kernel
kernel-ml.x86_64 5.14.12-1.el7.elrepo elrepo-kernel
[root@localhost download]# yum --enablerepo="elrepo-kernel" install kernel-ml-5.14.13-1.el7.elrepo.x86_64 -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* elrepo: hkg.mirror.rackspace.com
* elrepo-kernel: hkg.mirror.rackspace.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package kernel-ml.x86_64 0:5.14.13-1.el7.elrepo will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=====================================================================================================================================================================================================================================
Package Arch Version Repository Size
=====================================================================================================================================================================================================================================
Installing:
kernel-ml x86_64 5.14.13-1.el7.elrepo elrepo-kernel 55 M
Transaction Summary
=====================================================================================================================================================================================================================================
Install 1 Package
Total download size: 55 M
Installed size: 251 M
Downloading packages:
kernel-ml-5.14.13-1.el7.elrepo.x86_64.rpm | 55 MB 00:00:25
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
Installing : kernel-ml-5.14.13-1.el7.elrepo.x86_64 1/1
Verifying : kernel-ml-5.14.13-1.el7.elrepo.x86_64 1/1
Installed:
kernel-ml.x86_64 0:5.14.13-1.el7.elrepo
Complete!
[root@localhost download]# grub2-set-default 0
[root@localhost download]# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.14.13-1.el7.elrepo.x86_64
Found initrd image: /boot/initramfs-5.14.13-1.el7.elrepo.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-957.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-957.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-4c09ebdb718f43399d164036e825aecc
Found initrd image: /boot/initramfs-0-rescue-4c09ebdb718f43399d164036e825aecc.img
done
[root@localhost download]# grubby --default-kernel
/boot/vmlinuz-5.14.13-1.el7.elrepo.x86_64
[root@localhost download]# reboot
2.安装19.03版本的docker for linux
吐槽一下docker官网基于rpm方式安装docker的文档描述不全,具体文档如下:
Install Docker Engine on CentOS | Docker Documentation
须要注意的细节:
实际上需要在rpm所在目录,执行yum install xxx.rpm三次,分别是:
先装docker-ce-cli,再是containerd.io,最后装docker-ce;顺序不能乱,否则

本文介绍如何使用 Docker Buildx 工具在 x86_64 架构的服务器上构建 ARM 架构的 Docker 镜像。详细讲述了 Docker Buildx 的安装与配置过程,并通过 nginx 镜像构建实例展示了多架构镜像的构建方法。
最低0.47元/天 解锁文章
2262





