环境需求:
docker版本:19.03+
linux内核版本:4.8+
支持构建的目标平台:
linux/amd64, linux/arm64, or darwin/amd64
linux/amd64, linux/arm64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6 ...
开启插件支持
#将参数添加到配置文件
vim ~/.docker/config.json
"experimental": true
cat > ~/.docker/config.json <<EOF
{
"experimental": true
}
EOF
#或者添加环境变量
export DOCKER_CLI_EXPERIMENTAL=enabled
手动开启binfmt_misc支持
docker run --rm --privileged docker/binfmt:66f9012c56a8316f9244ffd7622d7c21c1f6f28d
#验证是否开启
ls -al /proc/sys/fs/binfmt_misc/
cat /proc/sys/fs/binfmt_misc/qemu-aarch64
#查看版本
docker buildx version
#新建构建器
docker buildx create --use --name mybuilder
------------------------小插曲-------------------------------------
[创建构建器]
docker buildx create --name mybuilder --driver docker-container
[使用构建器]
docker buildx use mybuilder
[查看构建器]
docker buildx ls
[多平台镜像构建模拟器]
docker run --privileged --rm tonistiigi/binfmt --install all
[本地指定dockerfile文件进行镜像构建并推送到仓库]
docker buildx build --platform linux/arm,linux/arm64,linux/amd64 -t test/arch --push -f ./dockerfile .
-------------------------------------------------------------
#启动构建器
docker buildx inspect mybuilder --bootstrap
#查看当前使用的构建器和支持的平台
docker buildx ls
#编写dockerfile
# syntax=docker/dockerfile:1
FROM --platform=$BUILDPLATFORM golang:alpine AS build
ARG TARGETPLATFORM
ARG BUILDPLATFORM
RUN echo "I am running on $BUILDPLATFORM, building for $TARGETPLATFORM" > /log
FROM alpine
COPY --from=build /log /log
CMD ["cat", "/log"]
#构建多平台镜像
docker buildx build -t codemiracle/hello-arch --platform=linux/arm,linux/arm64,linux/amd64 . --push
#将镜像保存到本地,需要指定平台和类型为docker
docker buildx build -t codemiracle/hello-arch --platform=linux/arm -o type=docker .
docker buildx build -t codemiracle/hello-arch --platform=linux/arm64 -o type=docker .
docker buildx build -t codemiracle/hello-arch --platform=linux/amd64 -o type=docker .
#镜像测试
###查看镜像的digests
docker buildx imagetools inspect codemiracle/hello-arch
#分别创建不同平台架构的容器查看验证
docker run --rm codemiracle/hello-arch:latest@sha256:38e0838**
#查看模拟器支持的架构
ls /proc/sys/fs/binfmt_misc/qemu-*
/proc/sys/fs/binfmt_misc/qemu-aarch64
/proc/sys/fs/binfmt_misc/qemu-ppc64le
/proc/sys/fs/binfmt_misc/qemu-arm
/proc/sys/fs/binfmt_misc/qemu-riscv64
/proc/sys/fs/binfmt_misc/qemu-mips64
/proc/sys/fs/binfmt_misc/qemu-s390x
/proc/sys/fs/binfmt_misc/qemu-mips64el
#国内镜像
#中国
docker buildx create --use --name=mybuilder-cn --driver docker-container --driver-opt image=dockerpracticesig/buildkit:master
# 适用于腾讯云环境(腾讯云主机、coding.net 持续集成)
docker buildx create --use --name=mybuilder-cn --driver docker-container --driver-opt image=dockerpracticesig/buildkit:master-tencent
# 使用默认镜像
docker buildx create --name mybuilder --driver docker-container
# 使用新创建好的 builder 实例
docker buildx use mybuilder
#自定义
vim buildkitd.toml
# https://github.com/moby/buildkit/blob/master/docs/buildkitd.toml.md
debug = true
[registry."docker.io"]
mirrors = ["hub-mirror.c.163.com","registry-1.docker.io"]
http = false
insecure = true
----------------------------------------
#加上证书
# https://github.com/moby/buildkit/blob/master/docs/buildkitd.toml.md
debug = true
[registry."harbor.codemiracle.com.cn"]
ca=["/etc/docker/certs.d/harbor.codemiracle.com.cn/ca.crt"]
[[registry."harbor.codemiracle.com.cn".keypair]]
key="/etc/docker/certs.d/harbor.codemiracle.com.cn/harbor.codemiracle.com.cn.key"
cert="/etc/docker/certs.d/harbor.codemiracle.com.cn/harbor.codemiracle.com.cn.cert"
http = false
insecure = true
#创建
docker buildx create --use --name mybuilder --driver docker-container --driver-opt image=dockerpracticesig/buildkit:master --config=./buildkitd.toml
#查看
docker exec -it buildx_buildkit_mybuilder0 cat /etc/buildkit/buildkitd.toml
------------------------------------------
#创建本地builder
### create builder with mirror
docker buildx create --use --name localbuilder --platform=linux/amd64,linux/arm64 --config=buildkitd.toml
# ARM架构有:
arm64, armv8l, arm64v8, aarch64 arm, arm32, arm32v7, armv7, armv7l, armhf arm32v6, armv6, armv6l, arm32v5, armv5, armv5l, armel, aarch32, riscv64
# intel amd 架构
x86, 386, i386, i686 x86_64, x64, amd64
# 构建支持7种架构的镜像 (示例)
docker buildx build --platform linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/386,linux/ppc64le,linux/s390x -t codemiracle/hello . --push
# 查看镜像信息
docker buildx imagetools inspect codemiracle/hello
# 查看构建器 支持的架构
docker buildx ls
# 注意事项:
容器 buildx_buildkit_mybuilder0 不要删除 不要停止
使用CPU 比较强劲的多核心处理器
官方文档:
Docker Buildx | Docker Documentation
GitHub - docker/buildx: Docker CLI plugin for extended build capabilities with BuildKit
# 二进制安装的docker,手动安装buildx插件
# https://download.docker.com/linux/static/stable/x86_64/
mkdir -p ~/.docker/cli-plugins/
wget -O ~/.docker/cli-plugins/docker-buildx https://github.com/docker/buildx/releases/download/v0.8.2/buildx-v0.8.2.linux-amd64
systemctl restart docker