Docker构建多架构镜像
# 一、前言
最近接了ARM架构的容器化项目,于是乎新的需求出现了,同一个服务需要构建对应CPU架构的镜像,先前人是单独用ARM的机器编译,但是不优雅且重复性劳动,维护起来累.
# 二、 探索buildx
# 2.1 启动buildx
在
Docker 19.03+
版本中可以使用docker buildx
build 命令使用BuildKit
构建镜像
# 2.2 查看docker buildx是否已开启
低版本的docker默认是没有开启的,手动开启下,export DOCKER_CLI_EXPERIMENTAL=enabled
高版本的docker默认是开启buildx,如图
# 三、启用buildx
# 3.1 指定buildx 构建器
由于 Docker 默认的 builder 实例不支持同时指定多个 --platform ,我们必须首先创建一个新的 builder 实例并切换到指定名称的 buildx 构建器
[root@iZj6c261538xjcb56g6iv4Z ~]# docker buildx create --name builder --use
1
# 3.2 构建多架构镜像
前提基础镜像支持多架构CPU,可以参考下参数文档 (opens new window),文章 (opens new window)
[root@iZj6c261538xjcb56g6iv4Z demo]# docker login registry.cn-hangzhou.aliyuncs.com
[root@iZj6c261538xjcb56g6iv4Z demo]# cat Dockerfile
FROM ubuntu:latest
# 设置时区
ENV TZ=Asia/Shanghai
WORKDIR /opt
RUN echo "${TZ}" > /etc/timezone \
&& ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime \
&& apt-get update \
&& apt install -y --no-install-recommends --no-install-suggests tzdata build-essential libpcre3 libpcre3-dev libssl-dev zlib1g-dev libpcre2-dev curl gnupg2 ca-certificates lsb-release ubuntu-keyring
[root@iZj6c261538xjcb56g6iv4Z demo]# docker buildx build --platform=linux/arm64,linux/amd64 -t registry.cn-hangzhou.aliyuncs.com/s-ops/demo:v1 . --push
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 3.3查看某个多架构镜像的信息
上面的图可以清晰的看到同时构建了ARM64和AMD64的镜像,可以使用
docker manifest inspect
查看相关信息
[root@iZj6c261538xjcb56g6iv4Z demo]# docker manifest inspect registry.cn-hangzhou.aliyuncs.com/s-ops/demo:v1
{
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.index.v1+json",
"manifests": [
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": 863,
"digest": "sha256:d93ef783d103a776ab5cf32aa7da942aefec1863a22b6879ab3f7c452a658290",
"platform": {
"architecture": "arm64",
"os": "linux"
}
},
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": 863,
"digest": "sha256:0dc9bfb5b8e23e445b1e854d5e626d3dcbf7bdb845c1b79aa6c67fc323ade86b",
"platform": {
"architecture": "amd64",
"os": "linux"
}
},
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": 566,
"digest": "sha256:26f1b2d7d10d9222e2646bde1ff9df2dda7ede956a53d6454ae5aa6ee8d422a6",
"platform": {
"architecture": "unknown",
"os": "unknown"
}
},
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": 566,
"digest": "sha256:8e5e4fa92a2a2cdc3c1a7dd0346af19c597e3c286f16d7aff9473403df508faa",
"platform": {
"architecture": "unknown",
"os": "unknown"
}
}
]
}
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
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
编辑 (opens new window)
上次更新: 2024/08/02, 14:16:48
- 01
- cert-manager自动签发Lets Encrypt09-05
- 02
- Prometheus数据迁移至VMstorage08-01
- 03
- Prometheus远程存储之VictoriaMetrics08-01