Docker 利用buildx插件构建多平台镜像——筑梦之路

环境需求:

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 < /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

你可能感兴趣的:(linux系统运维,虚拟化,云计算,docker,容器,运维)