概述
Docker Buildx 是一个 CLI 插件,它扩展了 docker 命令,完全支持Moby BuildKit 构建器工具包提供的功能。Docker 19.03版本以上都支持buildx。
启动Buildx
设置buildx为默认构建器
可以使用 docker buildx build 命令使用 BuildKit 构建镜像
创建builder实例
由于Docker默认的builder实例不支持同时指定多个–platform,所以必须先创建一个新的builder实例
适用于国内环境 # docker buildx create --use --name=builder-cn --driver docker-container --driver-opt image=dockerpracticesig/buildkit:master builder-cn 使用新创建的builder实例 # docker buildx use builder-cn # docker buildx ls NAME/NODE DRIVER/ENDPOINT STATUS PLATFORMS builder-cn * docker-container builder-cn0 unix:///var/run/docker.sock inactive default docker default default running linux/amd64, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6
|
Docker 在 Linux 系统架构下是不支持 arm 架构镜像,因此我们可以运行模拟器让其支持该特性。
# docker run --rm --privileged tonistiigi/binfmt:latest --install all { "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" ] }
|
启动构建器
# docker buildx inspect builder-cn --bootstrap [+] Building 27.6s (1/1) FINISHED => [internal] booting buildkit 27.6s => => pulling image dockerpracticesig/buildkit:master 25.5s => => creating container buildx_buildkit_builder-cn0 2.1s Name: builder-cn Driver: docker-container
Nodes: Name: builder-cn0 Endpoint: unix:///var/run/docker.sock Status: running Platforms: linux/amd64, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/mips64le, linux/mips64, linux/arm/v7, linux/arm/v6
# docker buildx ls NAME/NODE DRIVER/ENDPOINT STATUS PLATFORMS builder-cn * docker-container builder-cn0 unix:///var/run/docker.sock running linux/amd64, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/mips64le, linux/mips64, linux/arm/v7, linux/arm/v6 default docker default default running linux/amd64, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6 nux/arm/v6
|
构建镜像
Dockerfile
FROM node:12.22.7-alpine USER root WORKDIR /opt/blog RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories RUN npm config set registry https://registry.npm.taobao.org RUN apk --no-cache add git openssh RUN npm install hexo-cli -g && hexo init . \ && npm install hexo-renderer-pug hexo-renderer-stylus --save \ && npm install hexo-generator-searchdb --save \ && npm install hexo-wordcount --save \ && npm install hexo-generator-feed --save \ && npm install hexo-deployer-git --save RUN git config --global user.email "weiqun_h@163.com" RUN git config --global user.name "weiqun_h@163.com" RUN git clone -b master https://gitee.com/immyw/hexo-theme-butterfly.git themes/butterfly RUN sed -i 's#^theme\:.*#theme:\ butterfly#g' _config.yml VOLUME ["/opt/blog/source"] EXPOSE 4000 CMD ["hexo", "server"]
|
这是一个多阶段构建 Dockerfile,使用node-alpine镜像来构建应用,再node镜像中安装hexo应用及插件。现在就可以使用 buildx 构建一个支持 arm、arm64 和 amd64 多架构的 Docker 镜像了,同时将其推送到 Docker Hub。
# docker buildx build --platform linux/arm64,linux/amd64 -t heweiqun/hexo:202205121314-node-12.22.7 . --push [+] Building 12.4s (30/30) FINISHED => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 931B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [linux/arm64 internal] load metadata for docker.io/library/node:12.22.7-alpine 5.0s => [linux/amd64 internal] load metadata for docker.io/library/node:12.22.7-alpine 3.3s => [auth] library/node:pull token for registry-1.docker.io 0.0s => [auth] library/node:pull token for registry-1.docker.io 0.0s => [linux/arm64 1/10] FROM docker.io/library/node:12.22.7-alpine@sha256:0eca266c5fe38ba93aebac00e45c9ac1bb7328b0702a6dc10e1a6ea543d49301 0.0s => => resolve docker.io/library/node:12.22.7-alpine@sha256:0eca266c5fe38ba93aebac00e45c9ac1bb7328b0702a6dc10e1a6ea543d49301 0.0s => [linux/amd64 1/10] FROM docker.io/library/node:12.22.7-alpine@sha256:0eca266c5fe38ba93aebac00e45c9ac1bb7328b0702a6dc10e1a6ea543d49301 0.0s => => resolve docker.io/library/node:12.22.7-alpine@sha256:0eca266c5fe38ba93aebac00e45c9ac1bb7328b0702a6dc10e1a6ea543d49301 0.0s => CACHED [linux/arm64 2/10] WORKDIR /opt/blog 0.0s => CACHED [linux/arm64 3/10] RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories 0.0s => CACHED [linux/arm64 4/10] RUN npm config set registry https://registry.npm.taobao.org 0.0s => CACHED [linux/arm64 5/10] RUN apk --no-cache add git openssh 0.0s => CACHED [linux/arm64 6/10] RUN npm install hexo-cli -g && hexo init . && npm install hexo-renderer-pug hexo-renderer-stylus --save && npm install hexo-generator-searchdb --save && npm install hexo-wordcount --save && npm install hexo-generator-feed --save && npm install 0.0s => CACHED [linux/arm64 7/10] RUN git config --global user.email "weiqun_h@163.com" 0.0s => CACHED [linux/arm64 8/10] RUN git config --global user.name "weiqun_h@163.com" 0.0s => CACHED [linux/arm64 9/10] RUN git clone -b master https://gitee.com/immyw/hexo-theme-butterfly.git themes/butterfly 0.0s => CACHED [linux/arm64 10/10] RUN sed -i 's#^theme\:.*#theme:\ butterfly#g' _config.yml 0.0s => CACHED [linux/amd64 2/10] WORKDIR /opt/blog 0.0s => CACHED [linux/amd64 3/10] RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories 0.0s => CACHED [linux/amd64 4/10] RUN npm config set registry https://registry.npm.taobao.org 0.0s => CACHED [linux/amd64 5/10] RUN apk --no-cache add git openssh 0.0s => CACHED [linux/amd64 6/10] RUN npm install hexo-cli -g && hexo init . && npm install hexo-renderer-pug hexo-renderer-stylus --save && npm install hexo-generator-searchdb --save && npm install hexo-wordcount --save && npm install hexo-generator-feed --save && npm install 0.0s => CACHED [linux/amd64 7/10] RUN git config --global user.email "weiqun_h@163.com" 0.0s => CACHED [linux/amd64 8/10] RUN git config --global user.name "weiqun_h@163.com" 0.0s => CACHED [linux/amd64 9/10] RUN git clone -b master https://gitee.com/immyw/hexo-theme-butterfly.git themes/butterfly 0.0s => CACHED [linux/amd64 10/10] RUN sed -i 's#^theme\:.*#theme:\ butterfly#g' _config.yml 0.0s => exporting to image 7.3s => => exporting layers 0.0s => => exporting manifest sha256:7b76ebe12ed29c8a2de58074ef521498d12a8fc3da015e35cf6428584eddc3cb 0.0s => => exporting config sha256:5894ca12ba0310fddace94f35746c55705d8e7399f65f68028cfd327765aafb4 0.0s => => exporting manifest sha256:974f7ea6dfd51efe20d10c37468cbf3654b6f38859f9b0c0318a732e11c8ef50 0.0s => => exporting config sha256:ecfb0d9a8a68f196fbb306aefd8b7d82dc4c19d329c9366307d23f83b27da2ad 0.0s => => exporting manifest list sha256:748356e1191dd4eb9e6aa9cc76646902eceb1c107ba83bb5a019c0a5182f2089 0.0s => => pushing layers 6.1s => => pushing manifest for docker.io/heweiqun/hexo:202205121314-node-12.22.7@sha256:748356e1191dd4eb9e6aa9cc76646902eceb1c107ba83bb5a019c0a5182f2089 1.1s => [auth] heweiqun/hexo:pull,push token for registry-1.docker.io 0.0s => [auth] heweiqun/hexo:pull,push token for registry-1.docker.io 0.0s => [auth] heweiqun/hexo:pull,push token for registry-1.docker.io 0.0s
|
–platform 选择构建的平台
–push 会将构建好的镜像推送至仓库
– load 会将构建好的镜像存放至本地
现在就可以通过 docker pull heweiqun/hexo:202205121314-node-12.22.7拉取刚刚创建的镜像了,Docker 将会根据你的 CPU 架构拉取匹配的镜像。