Docker多架构构建背景与Buildx准备
前言:
当前ARM架构设备日益普及,使得容器镜像需要兼容多种硬件平台的需求显著增加。针对同一镜像,传统做法是在AMD64或ARM机器上分别执行构建,并通过添加如 nginx:v1-amd64 或 nginx:v1-arm64 等后缀标签来区分版本。这种方法不仅命名繁琐,且在缺乏对应架构物理机时难以实施。
现阶段推荐采用 buildx 工具进行构建,但该功能默认处于关闭状态。需在 Docker 配置文件中设置 "experimental": true 选项,随后重启 Docker 服务以激活该特性。
首先运行以下命令,使 AMD64 主机具备构建 ARM 架构镜像的能力:
docker run --rm --privileged tonistiigi/binfmt:latest --install all
接着创建一个名为 mybuilder-cn 的新构建上下文实例:
docker buildx create --use --name=mybuilder-cn --driver docker-container --driver-opt image=dockerpracticesig/buildkit:master
至此,环境准备工作已全部完成。
接下来以 Kubebuilder 生成的 Operator 项目默认 Dockerfile 为例进行演示:
# Build the manager binary
FROM --platform=$TARGETPLATFORM golang:1.16 as builder
ARG TARGETOS TARGETARCH
WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN go mod download
# Copy the go source
COPY main.go main.go
COPY api/ api/
COPY controllers/ controllers/
# Build
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -a -o manager main.go
# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM --platform=$TARGETPLATFORM gcr.io/distroless/static:nonroot
WORKDIR /
COPY --from=builder /workspace/manager .
USER 65532:65532
ENTRYPOINT ["/manager"]
主要修改涉及两处:
--platform=$TARGETPLATFORM
GOOS=${TARGETOS} GOARCH=${TARGETARCH}
这些TARGET开头的变量可以在参考链接2里看到全部含义。
随后使用该配置文件执行构建操作:
docker buildx build -t hello/namespace/name:v1 -f Dockerfile . --platform linux/amd64,linux/arm64 --push
需注意,buildx 命令中的 --platform 参数指定目标架构,--push 参数用于将结果推送至镜像仓库;若需加载至本地则使用 --load。这两个关键参数不可省略。
构建成功后即生成多架构镜像,可使用 docker manifest inspect 命令验证其结构,例如:
docker manifest inspect hello/namespace/name:v1
{
"schemaVersion": 2,
"mediaType": "application/vnd.docker.distribution.manifest.list.v2+json",
"manifests": [
{
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"size": 1367,
"digest": "sha256:a7b99854e13939e3beaf21c1da8b4654022026eda9f438155b18ae16eeff49a5",
"platform": {
"architecture": "amd64",
"os": "linux"
}
},
{
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"size": 2169,
"digest": "sha256:844885928813685ffa8c5ea4c6e9e7a09d95ac79e417afae0be7b73086762cfd",
"platform": {
"architecture": "arm64",
"os": "linux"
}
}
]
}
结果显示确实包含了多个架构的镜像层。使用时,在不同架构节点直接 pull 即可自动下载匹配版本的镜像。也可通过 docker pull --platform arm64|amd64 xxxxx 显式指定拉取的架构类型。
若环境无法使用 buildx,可采取手动方式:分别构建各架构镜像后,再手动创建 manifest 清单,示例如下:
# 指定拉取amd64架构
docker pull --platform amd64 gcr.io/distroless/static:nonroot
# 重新打tag
docker tag 9ef34 hello/ns/static:nonroot-amd64
# 推送
docker push hello/ns/static:nonroot-amd64
# 指定拉取arm64架构
docker pull --platform arm64 gcr.io/distroless/static:nonroot
# 重新打tag
docker tag 91714 hello/ns/static:nonroot-arm64
# 推送
docker push hello/ns/static:nonroot-arm64
## 制作manifest
docker manifest create hello/ns/static:nonroot hello/ns/static:nonroot-amd64 hello/ns/static:nonroot-arm64
docker manifest push hello/ns/static:nonroot
docker manifest rm hello/ns/static:nonroot
核心步骤在于最后三行代码。执行 push manifest 后,后续使用流程与第一种方法完全一致。
此外,在进行多架构构建时还需注意以下几点事项:
yum|apt install
其他辅助操作:查看镜像仓库中存在的镜像列表:
curl -u "用户名":"密码" -X GET http://镜像地址:5000/v2/_catalog?n=2000 | python -m json.tool
查看特定镜像包含的所有 tag:
curl -u "用户名":"密码" -X GET http://镜像地址:5000/v2/命名空间/镜像名称/tags/list | python -m json.tool
到此这篇关于Docker镜像多架构构建介绍的文章就介绍到这了,更多相关Docker镜像 内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!