为什么macOS(x86)可以运行docker arm容器arm64v8/alpine? [英] Why macos(x86) can run docker arm container arm64v8/alpine?

查看:338
本文介绍了为什么macOS(x86)可以运行docker arm容器arm64v8/alpine?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现在我的macOS(x86)上可以为arm映像arm64v8/alpine运行docker容器,但有警告,请参阅下文

I happened to find that on my macos(x86) can run docker container for an arm image arm64v8/alpine, but with a warning, see below

docker run -it arm64v8/alpine uname -a

docker run -it arm64v8/alpine uname -a

警告:请求的映像的平台(linux/arm64)与检测到的主机平台(linux/amd64)不匹配,并且没有请求特定的平台Linux d5509c57dd24 4.19.121-linuxkit#1 SMP Tue Dec 1 1 Tue Dec 1 17:50:32 UTC 2020 aarch64 Linux

WARNING: The requested image's platform (linux/arm64) does not match the detected host platform (linux/amd64) and no specific platform was requested Linux d5509c57dd24 4.19.121-linuxkit #1 SMP Tue Dec 1 17:50:32 UTC 2020 aarch64 Linux

我非常确定该映像不是多体系结构映像(docker manifest inspect --verbose arm64v8/alpine).为什么x86可以运行arm容器?

And I'm pretty sure the image is not a multi-archi image (docker manifest inspect --verbose arm64v8/alpine). Why x86 can run arm container ?

预先感谢

推荐答案

您是正确的,该映像不是多体系结构,但是docker可以运行它.其背后的原因是一个名为 binfmt_misc 的内核子系统,该子系统可以将二进制文件的 magic 编号设置为执行特定操作.您可以在这篇漂亮的维基百科帖子中了解更多内容.

You are correct, the image is not multi architecture, yet, docker can run it. Reason behind this is a kernel subsystem called binfmt_misc which allows to set the magic numbers of a binary file to specific actions for their execution. You can read more in this nice wikipedia post about it.

适用于Mac的Docker已经为 binfmt 魔术做好了准备,因此无需任何操作即可启用它.它会随安装即用地启用,您所需要做的就是获取映像并运行.该机制的详细信息可以在

Docker for Mac is arriving prepared for the binfmt magic, so there is nothing to be done to enable it. It will be enabled out-of-box with the installation, all you need to do is to fetch the image and run. The details of the mechanism can be found in repository of docker-for-mac project on this link.

简单地解释一下,二进制映像具有 magic 编号,该编号使内核可以决定如何处理执行.当 binfmt_misc 拦截一个他识别出 编号的文件时,他将调用与 magic 编号相关联的处理程序.

To explain it simply, the binary images have the magic number that allows the kernel to decide how to handle the execution. When binfmt_misc intercepts a file for which he recognizes the magic numbers he will invoke the handler that is associated with the magic numbers.

仅此一项不足以运行容器. magic 的下一部分是 QEMU ,它是各种CPU体系结构的仿真器.内核( binfmt_misc )将为每个ARM64二进制文件调用quemy,并模拟ARM64v8.

This alone is not enough to run the container. The next part of the magic is QEMU which is the emulator for various CPU architectures. The kernel (binfmt_misc) will invoke the quemy for each of the binaries that are ARM64 and will emulate the ARM64v8.

这不仅限于 docker ,也不限于在 macOS 上运行 docker 的虚拟机.任何Linux系统都可以配置为执行此操作.

This is not limited to docker nor to the virtual machine that is running the docker on macOS. Any linux system can be configured to do this.

您可以使用以下命令对其进行安装,以安装Ubuntu来运行仿真.

You can use following to install it setup Ubuntu to run the emulation.

sudo apt-get install qemu binfmt-support qemu-user-static # Install the qemu packages
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes # This step will execute the registering scripts

docker run --rm -t arm64v8/ubuntu uname -m # Testing the emulation environment

有关整个设置过程的更多详细信息,请参见 qemu-user-static 存储库

More details about the whole process of the set-up can be found in the qemu-user-static repository

OP:如果您从我的个人经验中想知道这样做的用处,那么在将应用程序从X86移植到其他体系结构(主要是ARM64)时,我正在大量使用此功能.这样一来,我就可以运行用于各种体系结构的构建系统,而无需拥有可以在其上运行构建的物理机器.

OP: If you are wondering what is the usefulness of this, from my personal experiance, I am using this functionality heavily when porting applications from X86 to other architectures (mainly ARM64). This allows me to run build systems for various architectures without having a physical machine on which I can run the build.

这篇关于为什么macOS(x86)可以运行docker arm容器arm64v8/alpine?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆