Docker 利用buildx插件构建多平台镜像
安装 Docker Buildx
运行 Docker 镜像分发的跨平台模拟器
docker run --privileged --rm tonistiigi/binfmt --install all
创建 docker-container 驱动程序的新构建器
docker buildx create --name container --driver docker-container
然后我们应该能够在我们的可用构建器列表中看到它:
docker buildx ls
创建项目文件夹添加 Dockerfile
创建文件夹 mkdir docker_buildx_demo_01
cd docker_buildx_demo_01
创建 Dockerfile touch Dockerfile
# syntax=docker/dockerfile:1 $TARGETPLATFORM 表示目标平台
FROM --platform=$TARGETPLATFORM golang:alpine AS build
ARG TARGETPLATFORM
ARG BUILDPLATFORM
RUN echo "I am running on $BUILDPLATFORM, building for $TARGETPLATFORM" > /log
FROM --platform=$TARGETPLATFORM alpine:latest
COPY --from=build /log /log
单独为个别平台构建镜像并输出文件
# x86
docker buildx build --platform linux/amd64 -t docker_buildx_demo:x86 -o type=docker,dest=./docker_buildx_demo_x86.tar .
# ppc64le
docker buildx build --platform linux/ppc64le -t docker_buildx_demo:ppc64le -o type=docker,dest=./docker_buildx_demo_ppc64le.tar .
各个平台分别加载加载文件
# x86
docker load -i docker_buildx_demo_x86.tar
# ppc64le
docker load -i docker_buildx_demo_ppc64le.tar