1. 查看buildx插件信息
比较新的docker-ce版本默认已经集成了buildx插件
[root@docker ~]# docker buildx version
github.com/docker/buildx v0.11.2 9872040
[root@docker ~]#
2. 增加多平台镜像构建支持
通过tonistiigi/binfmt:latest初始化一个基于容器的构建环境(依据官方说明):
[root@docker ~]# docker run --privileged --rm tonistiigi/binfmt --install all
Unable to find image 'tonistiigi/binfmt:latest' locally
latest: Pulling from tonistiigi/binfmt
8d4d64c318a5: Pull complete
e9c608ddc3cb: Pull complete
Digest: sha256:66e11bea77a5ea9d6f0fe79b57cd2b189b5d15b93a2bdb925be22949232e4e55
Status: Downloaded newer image for tonistiigi/binfmt:latest
installing: mips64le OK
installing: mips64 OK
installing: ppc64le OK
installing: arm64 OK
installing: arm OK
installing: s390x OK
installing: riscv64 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"
]
}
[root@docker ~]#
3. 创建构建器
构建器需要什么平台根据需要配置,这里只加了amd64和arm64:
# 创建名称为mybuilder的包含linux/amd64,linux/arm64架构的构建器
[root@docker ~]# docker buildx create --name=mybuilder --bootstrap --platform=linux/amd64,linux/arm64
[+] Building 4.3s (1/1) FINISHED
=> [internal] booting buildkit 4.3s
=> => pulling image moby/buildkit:buildx-stable-1 3.7s
=> => creating container buildx_buildkit_mybuilder0 0.6s
mybuilder
# 列出构建器
[root@docker ~]# docker buildx ls
NAME/NODE DRIVER/ENDPOINT STATUS BUILDKIT PLATFORMS
mybuilder docker-container
mybuilder0 unix:///var/run/docker.sock running v0.12.3 linux/amd64*, linux/arm64*, linux/amd64/v2, linux/amd64/v3, linux/amd64/v4, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/mips64le, linux/mips64, linux/arm/v7, linux/arm/v6
default * docker
default default running v0.11.6+616c3f613b54 linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/amd64/v4, linux/386, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/mips64le, linux/mips64, linux/arm/v7, linux/arm/v6
[root@docker ~]#
4. 设置当前构建器
默认构建器是自带的default,需要设置成我们创建的mybuilder:
[root@docker ~]# docker buildx use mybuilder
[root@docker ~]# docker buildx ls
NAME/NODE DRIVER/ENDPOINT STATUS BUILDKIT PLATFORMS
mybuilder * docker-container
mybuilder0 unix:///var/run/docker.sock running v0.12.3 linux/amd64*, linux/arm64*, linux/amd64/v2, linux/amd64/v3, linux/amd64/v4, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/mips64le, linux/mips64, linux/arm/v7, linux/arm/v6
default docker
default default running v0.11.6+616c3f613b54 linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/amd64/v4, linux/386, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/mips64le, linux/mips64, linux/arm/v7, linux/arm/v6
[root@docker ~]#
5. 构建并推送命令
构建镜像并推送到本地harbor仓库:
[root@docker ~]# docker buildx build \
-t harbor.local/library/mysql:8.0.35 \
--platform linux/amd64,linux/arm64 \
--push
6. 从harbor查看镜像
可以看到多镜像的显示上多了一个类型目录的图标,可以点击查看不同架构情况: