docker多平台构建

docker多平台构建像参考文档:
https://docs.docker.com/build/building/multi-platform/

安装docker

二进制安装docker

https://download.docker.com/linux/static/stable/
https://download.docker.com/linux/static/stable/x86_64/docker-24.0.7.tgz

docker 安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ wget https://download.docker.com/linux/static/stable/x86_64/docker-24.0.7.tgz
$ tar zxvf docker-24.0.7.tgz
docker/
docker/docker-init
docker/containerd-shim-runc-v2
docker/ctr
docker/docker-proxy
docker/dockerd
docker/docker
docker/containerd
docker/runc
$ mv docker/* /usr/bin

$ wget -O /usr/lib/systemd/system/docker.service \
https://raw.githubusercontent.com/docker/packaging/main/pkg/docker-engine/common/systemd/docker.service
# 删除 Requires=docker.socket containerd.service
# 替换 ExecStart=/usr/bin/dockerd --exec-opt native.cgroupdriver=systemd

$ systemctl daemon-reload
$ systemctl enable docker

buildx安装

https://github.com/docker/buildx/releases

1
2
3
$ mkdir -p ~/.docker/cli-plugins
$ wget -O ~/.docker/cli-plugins/docker-buildx https://github.com/docker/buildx/releases/download/v0.12.0/buildx-v0.12.0.linux-amd64
$ chmod +x ~/.docker/cli-plugins/docker-buildx

yum 安装docker

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ sudo wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

$ sudo yum -y install docker-ce

yum list docker-ce --showduplicates|grep "^doc"|sort -r
docker-ce.aarch64 3:24.0.7-1.el7 @docker-ce-stable
docker-ce.aarch64 3:24.0.0-1.el7 docker-ce-stable
docker-ce.aarch64 3:23.0.6-1.el7 docker-ce-stable
docker-ce.aarch64 3:23.0.0-1.el7 docker-ce-stable
docker-ce.aarch64 3:20.10.23-3.el7 docker-ce-stable
docker-ce.aarch64 3:20.10.0-3.el7 docker-ce-stable
docker-ce.aarch64 3:19.03.9-3.el7 docker-ce-stable
docker-ce.aarch64 3:19.03.5-3.el7 docker-ce-stable

$ yum -y install docker-ce-24.0.7-1.el7 docker-ce-cli-24.0.7-1.el7 docker-ce-rootless-extras-24.0.7-1.el7


$ sudo mkdir -p /etc/docker
$ sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://l0zsxxuv.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

多架构构建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
$ sudo docker run --rm --privileged tonistiigi/binfmt:latest --install all
# Unable to find image 'tonistiigi/binfmt:latest' locally
# latest: Pulling from tonistiigi/binfmt
# 66903ceeef2e: Pull complete
# 828e5112f0ea: Pull complete
# latest: Pulling from tonistiigi/binfmt
# 6dda554f4baf: Pull complete
# 2b0720d7a501: Pull complete
# Digest: sha256:66e11bea77a5ea9d6f0fe79b57cd2b189b5d15b93a2bdb925be22949232e4e55
# Status: Downloaded newer image for tonistiigi/binfmt:latest
# installing: amd64 OK
# installing: mips64le OK
# installing: mips64 OK
# installing: arm OK
# installing: s390x OK
# installing: ppc64le OK
# installing: riscv64 OK
# installing: 386 OK
# {
# "supported": [
# "linux/arm64",
# "linux/amd64",
# "linux/riscv64",
# "linux/ppc64le",
# "linux/s390x",
# "linux/386",
# "linux/mips64le",
# "linux/mips64",
# "linux/arm/v7",
# "linux/arm/v6"
# ],
# "emulators": [
# "qemu-arm",
# "qemu-i386",
# "qemu-mips64",
# "qemu-mips64el",
# "qemu-ppc64le",
# "qemu-riscv64",
# "qemu-s390x",
# "qemu-x86_64"
# ]
# }


$ docker buildx create --name mult-arch--busybox --driver docker-container --use
mult-arch--busybox

$ docker buildx inspect --bootstrap --builder mult-arch--busybox
# Name: mult-arch--busybox
# Driver: docker-container
# Last Activity: 2023-12-10 11:02:36 +0000 UTC
#
# Nodes:
# Name: mult-arch--busybox0
# Endpoint: unix:///var/run/docker.sock
# Status: running
# Buildkit: v0.9.3
# Platforms: linux/arm64, linux/amd64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/mips64le, linux/mips64, linux/arm/v7, linux/arm/v6
# Labels:
# org.mobyproject.buildkit.worker.executor: oci
# org.mobyproject.buildkit.worker.hostname: 35b6d23215ac
# org.mobyproject.buildkit.worker.snapshotter: overlayfs
# GC Policy rule#0:
# All: false
# Filters: type==source.local,type==exec.cachemount,type==source.git.checkout
# Keep Duration: 48h0m0s
# Keep Bytes: 488.3MiB
# GC Policy rule#1:
# All: false
# Keep Duration: 1440h0m0s
# Keep Bytes: 9.313GiB
# GC Policy rule#2:
# All: false
# Keep Bytes: 9.313GiB
# GC Policy rule#3:
# All: true
# Keep Bytes: 9.313GiB

$ cat <<EOF > Dockerfile
FROM busybox:latest
CMD sleep inf
EOF
$ docker login ghcr.io

$ docker buildx build --platform linux/amd64,linux/arm64 --tag ghcr.io/zmquan/busybox:mult-arch --push .
#[+] Building 16.1s (10/10) FINISHED docker-container:mult-arch--busybox
# => [internal] load build definition from Dockerfile 0.0s
# => => transferring dockerfile: 71B 0.0s
# => [internal] load .dockerignore 0.0s
# => => transferring context: 2B 0.0s
# => [linux/arm64 internal] load metadata for docker.io/library/busybox:latest 4.2s
# => [linux/amd64 internal] load metadata for docker.io/library/busybox:latest 3.8s
# => [linux/amd64 1/1] FROM docker.io/library/busybox:latest@sha256:1ceb872bcc68a8fcd34c97952658b58086affdcb604c90c1dee2735bde5edc2f 1.9s
# => => resolve docker.io/library/busybox:latest@sha256:1ceb872bcc68a8fcd34c97952658b58086affdcb604c90c1dee2735bde5edc2f 0.0s
# => => sha256:6672f60b6ba813a11925e795a0742f6c4c6313b64db544f60f1077619a616967 2.22MB / 2.22MB 1.8s
# => [linux/arm64 1/1] FROM docker.io/library/busybox:latest@sha256:1ceb872bcc68a8fcd34c97952658b58086affdcb604c90c1dee2735bde5edc2f 2.1s
# => => resolve docker.io/library/busybox:latest@sha256:1ceb872bcc68a8fcd34c97952658b58086affdcb604c90c1dee2735bde5edc2f 0.0s
# => => sha256:1c06b6731239f8a4e7fb37959da1482059579b4f74c6362bc06aa5bdb0e563a3 1.92MB / 1.92MB 2.0s
# => exporting to image 11.7s
# => => exporting layers 0.0s
# => => exporting manifest sha256:97e6eef54eae9d4c76042d647e4cbc9baac6fc59c169a3ebbb4cee111d04ca01 0.0s
# => => exporting config sha256:852b1ac5c7e68c61241df24f52aa0e89e4bb13065f21b79827d239734f05c5a0 0.0s
# => => exporting manifest sha256:b3c8adde53390f44bba8c7f885f7c8369cba27e9a2c9ddd8bd5da4212c00d78f 0.0s
# => => exporting config sha256:12a48fd5a0f0faa92adde3cb73cab573750f209c5d3d6395e5bb3c4b6fa7c26a 0.0s
# => => exporting manifest list sha256:1a1169b0e572401588d4a2ab8a847ae07831cceb99deedc7733bf4b47a566025 0.0s
# => => pushing layers 8.7s
# => => pushing manifest for ghcr.io/zmquan/busybox:mult-arch@sha256:1a1169b0e572401588d4a2ab8a847ae07831cceb99deedc7733bf4b47a566025 2.9s
# => [auth] zmquan/busybox:pull,push token for ghcr.io 0.0s
# => [auth] zmquan/busybox:pull,push token for ghcr.io 0.0s
# => [auth] zmquan/busybox:pull,push token for ghcr.io 0.0s