x86_64 平台下 docker 拉取 arm64 镜像使用

x86_64 平台下 docker 拉取 arm64 镜像使用

基于 x86 平台的 docker 服务拉取并运行 arm64 的 docker 镜像

拉取镜像

一般咱们拉取 docker 镜像直接 docker pull 即可

shell
1
2
3
4
5
6
7
# docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
59bf1c3509f3: Pull complete
Digest: sha256:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest

此时 docker 会根据当前系统平台拉取对应的镜像到本地

shell
1
2
# docker run --rm alpine uname -m
x86_64

如果咱们需要拉取其他平台的镜像,则需要在 pull 时使用到一个参数 --platform 用于指定为目标平台

例如咱们拉取 arm64 平台下的镜像 alpine

shell
1
2
3
4
5
6
7
# docker pull --platform arm64 alpine
Using default tag: latest
latest: Pulling from library/alpine
9b3977197b4f: Pull complete
Digest: sha256:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest

完成后再次运行镜像,查看该镜像所属的平台,会提示如下警告信息

shell
1
2
3
# docker run --rm alpine uname -m
WARNING: The requested image's platform (linux/arm64/v8) does not match the detected host platform (linux/amd64) and no specific platform was requested
standard_init_linux.go:228: exec user process caused: exec format error

根据上述信息可知,该镜像平台为 linux/arm64/v8 与咱们当前主机平台 linux/amd64 不一致,该镜像无法运行

添加 Docker 跨平台模拟

使用如下命令添加 Docker 跨平台模拟支持

  • 全平台:docker run --privileged --rm tonistiigi/binfmt --install all

  • 支持 arm64:docker run --privileged --rm tonistiigi/binfmt --install arm64

示例:

shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# docker run --privileged --rm tonistiigi/binfmt --install all
Unable to find image 'tonistiigi/binfmt:latest' locally
latest: Pulling from tonistiigi/binfmt
2a625f6055a5: Pull complete
71d6c64c6702: Pull complete
Digest: sha256:8de6f2decb92e9001d094534bf8a92880c175bd5dfb4a9d8579f26f09821cfa2
Status: Downloaded newer image for tonistiigi/binfmt:latest
installing: ppc64le OK
installing: mips64le OK
installing: arm64 OK
installing: arm OK
installing: s390x OK
installing: riscv64 OK
installing: mips64 OK
{
"supported": [
"linux/amd64",
"linux/arm64",
"linux/riscv64",
"linux/ppc64le",
"linux/s390x",
"linux/386",
"linux/mips64le",
"linux/mips64",
"linux/arm/v7",
"linux/arm/v6"
],
"emulators": [
"qemu-aarch64",
"qemu-arm",
"qemu-mips64",
"qemu-mips64el",
"qemu-ppc64le",
"qemu-riscv64",
"qemu-s390x"
]
}

在成功添加跨平台支持后,再次运行跨平台镜像

shell
1
2
3
# docker run --rm alpine uname -m
WARNING: The requested image's platform (linux/arm64/v8) does not match the detected host platform (linux/amd64) and no specific platform was requested
aarch64

可以看到,虽然依然有警告,但是命令执行成功了

参考文章:

评论

:D 一言句子获取中...

加载中,最新评论有1分钟缓存...