使用 docker buildx 實現多平臺編譯
docker registry api v2 支持多 CPU 架構鏡像.
同時 harbor v2 也實現了 docker registry api v2 的支持.
實現之後, 將不再有 cpu 架構困擾, 一個 docker pull image:tag
將自動適配可適配的平臺。真正做到海納百川,一騎當千。
0x01 準備
- docker 開啓實驗模式
- buildx 插件
- qemu 模擬器
- Linux kernel >= 4.8
linux 系統內核
由於 binfmt_misc 機制開啓需要依賴 Linux kernel >= 4.8 。因此,在對 linux 系統操作選型上有一定要求。
建議使用 發行版 出場內核已經滿足需求的操作系統。而不是選擇自己升級系統內核。
- ubuntu:18.04 LTS 及以上
- debian:10 及以上
參考文章:
- build multi architecture docker image with buildx
-
開啓實驗模式
當前 buildx 還是一個實驗模式, 如需要支持, 需要進行如下配置
- 開啓實驗模式。修改 /etc/docker/daemon.json, 增加 experimental 字段, 如下。
# vi /etc/docker/daemon.json
{
"experimental": true
}
安裝 buildx
- 將 buildx 放到 ~/.docker/cli-plugins/ 目錄下
# https://github.com/docker/buildx/blob/master/README.md#docker-ce
BUILDX_VERSION=v0.4.1
ARCH=$(uname -m)
[ "${ARCH}" == "x86_64" ] && ARCH=amd64
[ "${ARCH}" == "aarch64" ] && ARCH=arm64
mkdir -p ~/.docker/cli-plugins
wget -c https://github.com/docker/buildx/releases/download/${BUILDX_VERSION}/buildx-${BUILDX_VERSION}.linux-${ARCH} -O docker-buildx\
&& chmod +x docker-buildx \
&& mv docker-buildx ~/.docker/cli-plugins/
安裝 qemu
下載並安裝 qumu 到 /usr/bin/qemu-$(uname -m)-static
# https://github.com/multiarch/qemu-user-static#multiarchqemu-user-static-images
QEMU_VERSION=v5.0.0-2
wget -c https://github.com/multiarch/qemu-user-static/releases/download/${QEMU_VERSION}/qemu-$(uname -m)-static -O qemu-$(uname -m)-static \
&& chmod +x qemu-$(uname -m)-static \
&& mv qemu-$(uname -m)-static /usr/local/bin/qemu-$(uname -m)-static
0x02 example
創建 buildx builder
## 1. 創建指定名稱及指定平臺的builder
docker buildx create --use --platform=linux/amd64,linux/arm64 --name localbuilder
### 創建隨機名稱的 默認參數 builder
# docker buildx create --use
創建 Dockerfile 和 bakefile
目錄結構如下
# tree
# .
# ├── alpine-bake
# │ └── alpine.Dockerfile
# └── bake.hcl
- alpine-bake/alpine.Dockerfile
FROM alpine:3.12
ARG TARGETARCH
RUN echo "TARGETARCH is $TARGETARCH"
- docker-bake.hcl
group "default" {
targets = ["alpine"]
}
target "alpine" {
context = "./alpine-bake"
dockerfile = "alpine.Dockerfile"
tags = ["docker.io/tangx/alpine:buildx-bake-hcl"]
platforms = ["linux/amd64", "linux/arm64", "linux/arm/v6", "linux/arm/v7", "linux/s390x"]
## push to registry
output = ["type=registry"]
## pull base image always
pull = true
}
target "debian" {
context = "./debian-bake"
## default: Dockerfile
# dockerfile = "Dockerfile"
tags = ["docker.io/tangx/debian:buildx-bake-hcl"]
platforms = ["linux/amd64", "linux/arm64", "linux/arm/v6", "linux/arm/v7", "linux/s390x"]
## push to registry
output = ["type=registry"]
## pull base image always
pull = true
}
使用 bake 執行編譯
docker buildx bake -f bake.hcl # -f alias to bake
當命令中省略 -f 參數時, 默認 bake 文件爲:
- docker-bake.json
- docker-bake.hcl
- docker-compose.yaml
執行結果
可以在 tangx/alpine:buildx-bake-hcl 看到,生成的鏡像, 支持了 5個 cpu 架構。
- 執行過程
# docker buildx bake -f bake.hcl
[+] Building 46.9s (19/19) FINISHED
=> [internal] booting buildkit 3.3s
=> [internal] load build definition from alpine.Dockerfile 0.1s
=> [internal] load .dockerignore 0.1s
=> [linux/arm/v6 internal] load metadata for docker.io/library/alpine:3.12 13.1s
=> [linux/arm64 internal] load metadata for docker.io/library/alpine:3.12 10.4s
=> [linux/amd64 internal] load metadata for docker.io/library/alpine:3.12 13.1s
=> [linux/s390x internal] load metadata for docker.io/library/alpine:3.12 13.1s
=> [linux/arm/v7 internal] load metadata for docker.io/library/alpine:3.12 13.1s
=> [linux/amd64 1/2] FROM docker.io/library/alpine:3.12@sha256:c0e9560cda118f9ec63ddefb4a173a2b2a0347082d7dff7dc14272e7841a5b5a 6.1s
=> [linux/arm/v7 1/2] FROM docker.io/library/alpine:3.12@sha256:c0e9560cda118f9ec63ddefb4a173a2b2a0347082d7dff7dc14272e7841a5b5a 5.9s
=> [linux/arm/v6 1/2] FROM docker.io/library/alpine:3.12@sha256:c0e9560cda118f9ec63ddefb4a173a2b2a0347082d7dff7dc14272e7841a5b5a 6.0s
=> [linux/arm64 1/2] FROM docker.io/library/alpine:3.12@sha256:c0e9560cda118f9ec63ddefb4a173a2b2a0347082d7dff7dc14272e7841a5b5a 6.1s
=> [linux/s390x 1/2] FROM docker.io/library/alpine:3.12@sha256:c0e9560cda118f9ec63ddefb4a173a2b2a0347082d7dff7dc14272e7841a5b5a 6.3s
=> [linux/arm/v7 2/2] RUN echo "TARGETARCH is arm" 0.9s
=> [linux/arm/v6 2/2] RUN echo "TARGETARCH is arm" 0.8s
=> [linux/amd64 2/2] RUN echo "TARGETARCH is amd64" 0.7s
=> [linux/arm64 2/2] RUN echo "TARGETARCH is arm64" 0.5s
=> [linux/s390x 2/2] RUN echo "TARGETARCH is s390x" 0.4s
=> exporting to image 22.9s
使用命令行執行編譯
# docker buildx build --platform=linux/amd64,linux/arm64 --file alpine-bake/alpine.Dockerfile --tag tangx/alpine:multi-arch --push alpine-bake/
參數解釋
- docker buildx build : 主命令及子命令
- --platform=linux/amd64,linux/arm64 : 執行 cpu 架構
- --file : 指定 Dockerfile 的文件及路徑。省略則表示當前目錄下的 Dockerfile
- --tag : 鏡像名字及TAG
- --push : 完成並推送到倉庫
- alpine-bake : docker context 的目錄地址。
執行結果
略
相關文檔
- buildx
- buildx README.md in Github.com
- buildx Documents in docs.docker.com
2 . buildx bake
- buildx bake hcl variables
- buildx bake hcl examples
3 .docker
- builder arg
- docker manifest
- docker register v2
-
global platform args
buildx hcl 支持的變量值
type Target struct { Name string `json:"-" hcl:"name,label"` // Inherits is the only field that cannot be overridden with --set Inherits []string `json:"inherits,omitempty" hcl:"inherits,optional"` Context *string `json:"context,omitempty" hcl:"context,optional"` Dockerfile *string `json:"dockerfile,omitempty" hcl:"dockerfile,optional"` Args map[string]string `json:"args,omitempty" hcl:"args,optional"` Labels map[string]string `json:"labels,omitempty" hcl:"labels,optional"` Tags []string `json:"tags,omitempty" hcl:"tags,optional"` CacheFrom []string `json:"cache-from,omitempty" hcl:"cache-from,optional"` CacheTo []string `json:"cache-to,omitempty" hcl:"cache-to,optional"` Target *string `json:"target,omitempty" hcl:"target,optional"` Secrets []string `json:"secret,omitempty" hcl:"secret,optional"` SSH []string `json:"ssh,omitempty" hcl:"ssh,optional"` Platforms []string `json:"platforms,omitempty" hcl:"platforms,optional"` Outputs []string `json:"output,omitempty" hcl:"output,optional"` Pull *bool `json:"pull,omitempty" hcl:"pull,optional"` NoCache *bool `json:"no-cache,omitempty" hcl:"no-cache,optional"` // IMPORTANT: if you add more fields here, do not forget to update newOverrides and README. }
在 github action 上使用 buildx
https://github.com/marketplace/actions/customizable-docker-buildx
troubleshoot
- multiple platforms feature is currently not supported for docker driver
當前模式不支持 buildx : https://github.com/docker/cli/blob/master/experimental/README.md
# 開始實驗模式
# ~/docker/daemon.json
{
"experimental": true
}
# docker version -f ''
## true
- auto-push is currently not implemented for docker driver
缺少 builder
# 創建一個 builder
docker buildx create --use # a random name
docker buildx create --user --name specified_name # specified name
- failed to solve: rpc error: code = Unknown desc = failed to load LLB: runtime execution on platform linux/arm64 not supported
參考 qemu-user-static #getting-started 執行以下命令。如果正常返回,則表示成功
$ uname -m
x86_64
$ docker run --rm -t arm64v8/ubuntu uname -m
standard_init_linux.go:211: exec user process caused "exec format error"
$ docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
$ docker run --rm -t arm64v8/ubuntu uname -m
aarch64