本文对在x86机器上构建arm架构的image进行研究,参考文章qemu-user-static, Docker。
背景:Docker镜像技术普及之后,出现了云端和边缘端。云端主要使用Intel机器构成,底层架构多为x86_64(amd64),而边缘端都是由arm设备组成,其底层架构很多,如文章中的aarch64。云端设备资源多,功耗大,而边缘端设备资源少但功耗也小。若在边缘端生成Docker镜像文件,需要在时间和资源上做权衡。因此,云端生成边缘端镜像便成了另一种选择。
构建arm镜像的方法:在arm架构的机器上直接docker build;
使用QEMU在x86_64主机上模拟ARM环境执行docker build。QEMU是开源的machine emulator and virtualizer。
本文介绍如何在x86机器上模拟arm架构指令来构建arm架构的镜像。我们这里使用multiarch/qemu-user-static来实现在x86主机上模拟arm环境,即执行arm的指令。
下面是使用qemu-user-static的效果。
$ uname -m
x86_64
$ docker run --rm -t arm64v8/ubuntu uname -m
standard_init_linux.go:211: exec user process caused "exec format error"
$ docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
$ docker run --rm -t arm64v8/ubuntu uname -m
aarch64
示例主机为x86_64架构,当在主机上运行arm架构的镜像并在该镜像上执行命令时,报错,因为x86架构解析不了arm架构的指令。但在执行qemu-user-static镜像后,重新运行arm架构的镜像便可以,因为qemu-user-static将arm架构的指令解释成x86架构的指令执行。qemu-user-static支持很多ARM架构。
qemu-user-static就是一组静态的二进制文件qemu-$arch-static,作为interpreter,来执行特定架构的可执行文件。
$ uname -m
x86_64
$ file bin/hello-aarch64
bin/hello-aarch64: ELF 64-bit LSB executable, ARM aarch64, version 1 (GNU/Linux), statically linked, BuildID[sha1]=fa19c63e3c60463e686564eeeb0937959bd6f559, for GNU/Linux 3.7.0, not stripped, too many notes (256)
$ bin/hello-aarch64
bash: bin/hello-aarch64: cannot execute binary file: Exec format error
$ qemu-aarch64-static bin/hello-aarch64
Hello World!
当qemu-user-static和binfmt_misc一起使用,便能模拟各种不同架构。
qemu-user-static 镜像
qemu-user-static是一组镜像,$version为QEMU的版本,$from_arch为host architecture,$to_arch为guest architecture。
multiarch/qemu-user-static image
multiarch/qemu-user-static:$version images
multiarch/qemu-user-static

本文介绍了如何在x86_64机器上使用QEMU-user-static模拟ARM环境,以构建ARM架构的Docker镜像。通过multiarch/qemu-user-static镜像,可以在x86主机上执行ARM指令,实现跨架构的Docker镜像构建。文中详细阐述了qemu-user-static的工作原理,并提供了示例步骤。
最低0.47元/天 解锁文章
2137

被折叠的 条评论
为什么被折叠?



