7

我正在尝试交叉编译一个 rust 应用程序以在我的树莓派集群上运行。我看到来自 docker 的 buildx 应该能够使这成为可能。我现在有一个最小的 dockerfile,如下所示:

FROM rust
RUN apt-get update
ENTRYPOINT ["echo", "hello world"]

我尝试通过运行以下命令来编译它:docker buildx build --platform=linux/arm/v7 some/repo:tag .

当我这样做时,我收到以下错误:

[+] Building 0.9s (5/5) FINISHED                                      
=> [internal] load build definition from Dockerfile                                                                                                                                                  0.0s  => => transferring dockerfile: 102B                                                                                                                                                                  0.0s  => [internal] load .dockerignore                                                                                                                                                                     0.0s  => => transferring context: 2B                                                                                                                                                                       0.0s  => [internal] load metadata for docker.io/library/rust:latest                                                                                                                                        0.7s  => CACHED [1/2] FROM docker.io/library/rust@sha256:65e254fff15478af71d342706b1e73b26fd883f3432813c129665a97a74e2278
0.0s  => ERROR [2/2] RUN apt-get update                                                                                                                                                                   0.2s
------                                                                                                                                                                                                     
 > [2/2] RUN apt-get update:
#5 0.191 standard_init_linux.go:219: exec user process caused: exec format error
------ error: failed to solve: rpc error: code = Unknown desc = executor failed running [/bin/sh -c apt-get update]: exit code: 1

我觉得我在这里遗漏了一些非常基本的东西,希望有人告诉我为什么这么简单的东西对我不起作用。

我在 Ubuntu 操作系统上运行 docker 版本 20.10.1

提前致谢!


输出docker buildx inspect --bootstrap

Name:   default
Driver: docker

Nodes:
Name:      default
Endpoint:  default
Status:    running
Platforms: linux/amd64, linux/386

输出ls -l /proc/sys/fs/binfmt_misc/

total 0
--w------- 1 root root 0 Dec 19 07:29 register
-rw-r--r-- 1 root root 0 Dec 19 07:29 status
4

1 回答 1

11

交叉编译需要qemu-user-staticinfmt-support.

$ sudo apt install -y qemu-user-static binfmt-support

qemu-user-static用于 QEMU 的用户模式仿真,以及binfmt_misc在读取其他可执行二进制文件时切换到 QEMU。然后,告诉 docker 使用它们。

$ docker run --rm --privileged multiarch/qemu-user-static --reset -p yes

您必须害怕以特权运行未知图像,但内容是安全的。接下来,在 docker 中创建一个用户来构建图像。

$ docker buildx create --name sofia # name as you like
$ docker buildx use sofia
$ docker buildx inspect --bootstrap

如果成功,buildkit将被拉取:

[+] Building 9.4s (1/1) FINISHED                                                                                                                                                  
 => [internal] booting buildkit                                                                                                                                              9.4s
 => => pulling image moby/buildkit:buildx-stable-1                                                                                                                           8.7s
 => => creating container buildx_buildkit_sofia0                                                                                                                             0.7s
Name:   sofia
Driver: docker-container

Nodes:
Name:      sofia0
Endpoint:  unix:///var/run/docker.sock
Status:    running
Platforms: linux/amd64, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6

可用目标扩大!

参考: 使用 Buildx 构建多架构 Docker 映像 | 通过阿图尔克劳泽 | 中等的

于 2020-12-19T15:50:33.623 回答