什么是Docker多架构容器镜像
在 Docker 中,同一个 Docker 镜像可以在不同的平台上运行,例如在 x86、ARM、PowerPC 等不同的 CPU 架构上。
为了支持这种多平台的镜像构建和管理,Docker 在 17.06
版本时引入了 Manifest 的概念,在此之前,Docker 并没有原生支持多平台镜像的构建和管理,因此开发者需要手动构建和管理不同平台的镜像。
多架构镜像实际上是多个镜像的集合,
多架构镜像 = 多个单架构镜像的集合
├── amd64 架构镜像
├── arm64 架构镜像
├── s390x 架构镜像
└── ppc64le 架构镜像
镜像存储
- 每个架构版本独立存储
- 共享镜像标签
- 运行时自动选择
拉取机制, 使用多架构镜像,会自动识别当前环境架构。
# 自动匹配架构
docker pull myimage:latest
技术实现
- Manifest List 机制
- 每个架构对应独立镜像
- 统一镜像名称管理
构建示例
A. 手动构建多架构镜像
# 构建多架构镜像
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t myimage:latest \
--push .
B. Dockerfile 多阶段构建
FROM --platform=$TARGETPLATFORM ubuntu:latest
# 根据目标平台动态处理
RUN if [ "$TARGETARCH" = "amd64" ]; then \
echo "x86 specific setup"; \
elif [ "$TARGETARCH" = "arm64" ]; then \
echo "ARM specific setup"; \
fi
如何判断是不是多架构镜像
docker manifest inspect
命令默认查看远端镜像的架构信息。
docker manifest inspect registry.access.redhat.com/ubi9 | grep architecture
"architecture": "amd64",
"architecture": "arm64",
"architecture": "s390x",
"architecture": "ppc64le",
manifest inspect
查询远端,image inspect
查询本地。
# 先拉取镜像
docker pull registry.access.redhat.com/ubi9
# 再查看本地镜像架构
docker image inspect registry.access.redhat.com/ubi9 | grep Architecture
本地也可以运行容器查看
# 查看本机架构
uname -m
# 运行时检查
docker run --rm registry.access.redhat.com/ubi9 arch
除了使用命令行查看,也可以通过访问 hub.docker.com 远程镜像仓库来查看构建的多架构镜像信息
多架构镜像demo
在 Docker 中,docker pull
命令默认会根据当前系统的架构拉取匹配的镜像。如果镜像支持多架构(如 linux/amd64
、linux/arm64
等),Docker 会自动选择与当前系统兼容的架构版本。
确保镜像支持多架构(通过 docker manifest inspect
检查),例如ubi9镜像
docker manifest inspect registry.access.redhat.com/ubi9
# docker manifest inspect registry.access.redhat.com/ubi9
{
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.index.v1+json",
"manifests": [
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": 659,
"digest": "sha256:6db9bfa8c17ca247dcd53970df5f4e90fa423d6deaaa7096e48aba986de906e8",
"platform": {
"architecture": "amd64",
"os": "linux"
}
},
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": 659,
"digest": "sha256:6b70536201bc36b335b9cf77c616fe3571bac838ea27a362c8c2e44c155fe9dd",
"platform": {
"architecture": "arm64",
"os": "linux"
}
},
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": 659,
"digest": "sha256:44519f89e975a6d0303d488acfbf4234e5e1361bd9f7debe24dafa1b63c88d1b",
"platform": {
"architecture": "s390x",
"os": "linux"
}
},
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": 659,
"digest": "sha256:ff177bf0fa56f70c4ef8ceab8d2d0bb4761e7078db08f2267a1443b46cc36be4",
"platform": {
"architecture": "ppc64le",
"os": "linux"
}
}
]
}
可以手动拉取每个架构的镜像:
docker pull --platform linux/amd64 <镜像名称>
docker pull --platform linux/arm64 <镜像名称>
docker pull --platform linux/arm/v7 <镜像名称>
docker pull --platform linux/amd64 registry.access.redhat.com/ubi9
查看下载后的本地镜像详情:docker inspect registry.access.redhat.com/ubi9
% docker inspect registry.access.redhat.com/ubi9
[
{
"Id": "sha256:d53e845ae020df4be64d7abe1e7cae17eb5244095c5a9df2d6133e03aed6bf8e",
"RepoTags": [
"registry.access.redhat.com/ubi9:latest"
],
"RepoDigests": [
"registry.access.redhat.com/ubi9@sha256:ea57285741f007e83f2ee20423c20b0cbcce0b59cc3da027c671692cc7efe4dd"
],
"Parent": "",
"Comment": "FROM quay.io/redhat-user-workloads/osci-rhel-containers-tenant/rhel-9-5/ubi9-9-5:7c3ec864c7dfbb620cbb0fe45bfc382aedcc452f-linux-x86-64",
"Created": "2025-04-08T08:46:09.850108071Z",
"DockerVersion": "",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"container=oci"
],
"Cmd": [
"/bin/bash"
],
"Image": "",
"Volumes": null,
"WorkingDir": "/",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"architecture": "x86_64",
"build-date": "2025-04-08T08:37:46Z",
"com.redhat.component": "ubi9-container",
"com.redhat.license_terms": "https://www.redhat.com/en/about/red-hat-end-user-license-agreements#UBI",
"description": "The Universal Base Image is designed and engineered to be the base layer for all of your containerized applications, middleware and utilities. This base image is freely redistributable, but Red Hat only supports Red Hat technologies through subscriptions for Red Hat products. This image is maintained by Red Hat and updated regularly.",
"distribution-scope": "public",
"io.buildah.version": "1.39.0-dev",
"io.k8s.description": "The Universal Base Image is designed and engineered to be the base layer for all of your containerized applications, middleware and utilities. This base image is freely redistributable, but Red Hat only supports Red Hat technologies through subscriptions for Red Hat products. This image is maintained by Red Hat and updated regularly.",
"io.k8s.display-name": "Red Hat Universal Base Image 9",
"io.openshift.expose-services": "",
"io.openshift.tags": "base rhel9",
"maintainer": "Red Hat, Inc.",
"name": "ubi9",
"release": "1744101466",
"summary": "Provides the latest release of Red Hat Universal Base Image 9.",
"url": "https://www.redhat.com",
"vcs-ref": "7c3ec864c7dfbb620cbb0fe45bfc382aedcc452f",
"vcs-type": "git",
"vendor": "Red Hat, Inc.",
"version": "9.5"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 205271437,
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/bc8981c14b5f7488fa7cf0153ce94e05021248907d457c8706f1ce574f415213/diff",
"MergedDir": "/var/lib/docker/overlay2/a7b71e037bcc3da2b12c62e4462621515a49746379863e9d7e3cbb0d41c72ff5/merged",
"UpperDir": "/var/lib/docker/overlay2/a7b71e037bcc3da2b12c62e4462621515a49746379863e9d7e3cbb0d41c72ff5/diff",
"WorkDir": "/var/lib/docker/overlay2/a7b71e037bcc3da2b12c62e4462621515a49746379863e9d7e3cbb0d41c72ff5/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:0b5f00642dad66874f99e820596166ac000d0e4ba1b681e0c006382075727b2d",
"sha256:ee91f1eda2ae17f372a3f2fcb42087fcf553be5c73ccc13284f98bcd8ca8c34b"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]