kaixuan1115 / notes

笔记收录
6 stars 0 forks source link

Docker启用buildx插件,构建跨平台镜像 #35

Closed xiaokaixuan closed 3 years ago

xiaokaixuan commented 3 years ago

1. 更新docker版本到大于或等于19.03

sudo apt install -y apt-transport-https

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

sudo apt-get update

sudo apt-get install docker-ce

2.设置环境变量

export DOCKER_CLI_EXPERIMENTAL=enabled

3.启用binfmt_misc

docker run --rm --privileged docker/binfmt:66f9012c56a8316f9244ffd7622d7c21c1f6f28d

4.验证是 binfmt_misc 否开启

ls -al /proc/sys/fs/binfmt_misc/

cat /proc/sys/fs/binfmt_misc/qemu-aarch64

5.新建构建器

docker buildx create --use --name mybuilder

6.启动构建起

docker buildx inspect mybuilder --bootstrap

7.查看构建器及其所支持的cpu架构

docker buildx ls

8.创建Dockerfile

9.构建跨平台镜像

docker buildx build -t 镜像名 --platform=linux/arm,linux/arm64,linux/amd64 [Dockerfile所在的路径] --push

注:提示需要登录Dokcer Hub。貌似只能拉取Dokcer Hub里的镜像构建跨平台镜像,而不能用自己本地的镜像构建。

注:开了binfmt_misc后,linux/armd64架构的宿主机上可以运行armf架构的docker容器(例如,把树莓派上的系统打包并导入成docker镜像,再创建容器。开启binfmt_misc后,容器可正常运行。如果不开启binfmt_misc,会报错:standard_init_linux.go:211: exec user process caused “exec format error”)