buildx多平台构建原创
Docker的多架构支持是基于不同cpu的架构而不是不同操作系统的架构
# 跨 CPU 架构编译程序的方法
- 直接在目标硬件上编译(常用)
- 模拟目标硬件
- 通过 binfmt_misc 模拟目标硬件的用户空间
- 使用交叉编译器
# buildx
Docker 19.03 引入的插件 buildx buildx 会通过 QEMU 和 binfmt_misc 分别为不同的 CPU 架构(arm,arm64 和 amd64等)构建不同的镜像。构建完成后,就会创建一个 manifest list,其中包含了指向这些镜像的指针。
# 环境要求
要确保 Docker 版本不低于 19.03,同时还要通过设置环境变量 DOCKER_CLI_EXPERIMENTAL 来启用
如果你使用的是 Linux,需要手动启用 binfmt_misc 大多数 Linux 发行版都很容易启用,不过还有一个更容易的办法,直接运行一个特权容器,容器里面写好了设置脚本: 🐳 → docker run --rm --privileged docker/binfmt:66f9012c56a8316f9244ffd7622d7c21c1f6f28d 建议将 Linux 内核版本升级到 4.x 以上,特别是 CentOS 用户,你可能会遇到错误。 cat /proc/sys/fs/binfmt_misc/qemu-aarch64
Docker 默认会使用不支持多 CPU 架构的构建器,我们需要手动切换。 先创建一个新的构建器:docker buildx create --use --name mybuilder 启动构建器: docker buildx inspect mybuilder --bootstrap 查看当前使用的构建器及构建器支持的 CPU 架构:docker buildx ls
# 构建镜像
docker buildx build -t yangchuansheng/hello-arch --platform=linux/arm,linux/arm64,linux/amd64 . --push
如果想将构建好的镜像保存在本地,可以将 type 指定为 docker,但必须分别为不同的 CPU 架构构建不同的镜像,不能合并成一个镜像 🐳 → docker buildx build -t yangchuansheng/hello-arch --platform=linux/arm -o type=docker . 🐳 → docker buildx build -t yangchuansheng/hello-arch --platform=linux/arm64 -o type=docker . 🐳 → docker buildx build -t yangchuansheng/hello-arch --platform=linux/amd64 -o type=docker .