podman跨平台多架构构建步骤

引言

为了充分利用手上的树莓派,我决定将自己写的轮子应用分阶段迁移到树莓派上。众所周知,我是Fedora的拥趸,Fedora当前使用的OCI前端管理工具是PODMAN,当然可以安装Docker,但是尝鲜使用Podman体验下来也挺好,使用习惯兼容Docker。

实施

当使用Docker时,我们可以使用docker buildx来进行多架构构建,但是使用podman时,要怎么处理多架构构建的需求呢?

创建manifest

manifest清单之意,其是一个JSON格式的表达式,用来描述一组不同平台的镜像,podman的多架构镜像构建,需要基于其manifest声明。

# 创建一个manifest声明
$ podman manifest create images.internal/devel
# 查看创建的manifest声明
$ podman manifest inspect images.internal/devel
{
    "schemaVersion": 2,
    "mediaType": "...",
    "manifests": []
}

基于manifest构建

每个镜像内部都关联着一个manifest,多架构构建,需要我们手动操作该manifest,将不同架构的镜像添加到该manifest声明中来。

# 构建镜像
$ podman build --platform linux/amd64,linux/arm64 --manifest images.internal/devel .

上面的命令的意思是构建两个平台 amd64arm64 架构的镜像,关联的 manifestimages.internal/devel,如此一来,该声明中就存在了两个镜像。

# 查看刚构建的镜像
$ podman manifest inspect images.internal/devel
{
    "schemaVersion": 2,
    "mediaType": "...",
    "manifests": [
        {...}, // 镜像amd64声明
        {...} // 镜像arm64声明
    ]
}

推送镜像

推送镜像也要基于 manifest 进行。

# push镜像
$ podman manifest push images.internal/devel

结语

是时候替换 dockerpodman 了。