找回密码
 立即注册
首页 业界区 业界 Buildah 简明教程:让镜像构建更轻量,告别 Docker 依赖 ...

Buildah 简明教程:让镜像构建更轻量,告别 Docker 依赖

都硎唷 2 小时前
1.png

Buildah 是一个专注于构建 OCI 镜像的工具,Buildah CLI 工具使用底层 OCI 技术实现(例如 containers/image 和 containers/storage)。
OCI 三剑客包括:

  • 专注于镜像构建的 Buildah
  • 专注于镜像和容器管理的 Podman
  • 专注于镜像操作和管理(尤其是涉及远程仓库的操作)的 Skopeo
这三者一起形成了一个 Dockerless 的容器生态,支持构建、管理、推送和操作镜像和容器,且不依赖 Docker 守护进程。
注意:三者之间功能是有一定重复的,特别是 Buildah 和 Podman,不过各自专注点不同,建议合理搭配使用。
Buildah 和 Podman 的关系说明见官方文档: buildah-and-podman-relationship
1. 什么是 Buildah?

Buildah 是一个专注于构建 OCI 镜像的工具,Buildah CLI 工具使用底层 OCI 技术实现(例如 containers/image 和 containers/storage)。
官方描述原文:
A tool that facilitates building OCI images.the Buildah command line tool (CLI) and the underlying OCI based technologies (e.g. containers/image and containers/storage)
Buildah CLI 工具则基于这些项目实现了构建、移动、管理镜像的功能:

  • containers/image project provides mechanisms to copy (push, pull), inspect, and sign container images


  • containers/storage project provides mechanisms for storing filesystem layers, container images, and containers
那么问题来了:构建镜像已经有 Docker 了为什么还需要 Buildah?
Buildah 是无守护进程以及可以 rootless 运行的,相比于 docker 更加轻量级。
如果使用 Buildah 来代替 Docker 镜像构建能力,由于可以无守护进程以及可以 rootless 运行,因此即使在容器中使用也非常方便,对于 Devops 来说是一个很好的选择。
即:相较于现有的构建工具, Buildah 更轻量级,做到了 Dockerless 和 Rootless
2. 安装 Buildah

官方文档:buildah#install.md
Buildah 为各大发行版都提供了对应的 Package,可以方便的通过 yum、apt-get、dnf 等等工具安装,当然也可以通过源码编译安装。
推荐使用发行版自带的包管理工具安装:
  1. # CentOS
  2. sudo yum -y install buildah
  3. # Ubuntu 20.10 and newer
  4. sudo apt-get -y update
  5. sudo apt-get -y install buildah
  6. # Fedora
  7. sudo dnf -y install buildah
复制代码
Demo 用的 Ubuntu22.04
  1. sudo apt-get -y update
  2. sudo apt-get -y install buildah
复制代码
查看 Buildah 版本
ps:系统版本比较低,所以安装的 buildah 也比较旧
  1. root@builder-ubuntu:~# buildah version
  2. Version:         1.23.1
  3. Go Version:      go1.17
  4. Image Spec:      1.0.1
  5. Runtime Spec:    1.0.2-dev
  6. CNI Spec:        0.4.0
  7. libcni Version:
  8. image Version:   5.16.0
  9. Git Commit:
  10. Built:           Thu Jan  1 08:00:00 1970
  11. OS/Arch:         linux/amd64
  12. BuildPlatform:   linux/amd64
复制代码
3. 基础功能

使用命令式构建镜像

Buildah 相对于 Dockerfile 提供了强大的命令式构建方式,将 Dockerfile 指令变成一条一条的命令,为我们构建镜像提供了新的选择:
  1. # 拉取镜像,类似 Dockerfile 中的 FROM
  2. container=$(buildah from nginx)
  3. # 类似 Dockerfile 中的 RUN
  4. buildah run $container -- bash -c 'echo "hello world" > /usr/share/nginx/html/index.html'
  5. # 提交保存镜像
  6. buildah commit $container nginx-hello
复制代码
输出如下:
  1. [root@builder ~]# container=$(buildah from nginx)
  2. [root@builder ~]# buildah run $container -- bash -c 'echo "hello world" > /usr/share/nginx/html/index.html'
  3. [root@builder ~]# buildah commit $container nginx-hello
  4. Getting image source signatures
  5. Copying blob c0f1022b22a9 skipped: already exists
  6. Copying blob fc00b055de35 skipped: already exists
  7. Copying blob 2c3a053d7b67 skipped: already exists
  8. Copying blob b060cc3bd13c skipped: already exists
  9. Copying blob 8aa4787aa17a skipped: already exists
  10. Copying blob c28e0f7d0cc5 skipped: already exists
  11. Copying blob d32d820bcf1c skipped: already exists
  12. Copying blob c6a7a8084917 done   |
  13. Copying config 19de2f1f4a done   |
  14. Writing manifest to image destination
  15. 19de2f1f4afc6e0ff9da11e9dfb988619f4bcd1d388ea4c18413ab574487a0d4
复制代码
查看到刚才构建的镜像
  1. [root@builder ~]# buildah images
  2. REPOSITORY                          TAG       IMAGE ID       CREATED          SIZE
  3. localhost/nginx-hello               latest    19de2f1f4afc   22 seconds ago   196 MB
复制代码
通过 Dockerfile 构建镜像

当然,Buildah 也支持通过 Dockerfile 构建镜像,这个应该是比较常见的用法。
准备一个 Dockerfile
  1. FROM nginx
  2. RUN echo "Hello World" > /usr/share/nginx/html/index.html
  3. EXPOSE 80
复制代码
使用 buildah 构建镜像
  1. buildah build -t nginx-hello2 .
复制代码
输出如下
  1. [root@builder ~]# buildah build -t nginx-hello2 .
  2. STEP 1/3: FROM nginx
  3. STEP 2/3: RUN echo "Hello World" > /usr/share/nginx/html/index.html
  4. STEP 3/3: EXPOSE 80
  5. COMMIT nginx-hello2
  6. Getting image source signatures
  7. Copying blob c0f1022b22a9 skipped: already exists
  8. Copying blob fc00b055de35 skipped: already exists
  9. Copying blob 2c3a053d7b67 skipped: already exists
  10. Copying blob b060cc3bd13c skipped: already exists
  11. Copying blob 8aa4787aa17a skipped: already exists
  12. Copying blob c28e0f7d0cc5 skipped: already exists
  13. Copying blob d32d820bcf1c skipped: already exists
  14. Copying blob eec64f0b2723 done   |
  15. Copying config 1b63bdb270 done   |
  16. Writing manifest to image destination
  17. --> 1b63bdb270c1
  18. Successfully tagged localhost/nginx-hello2:latest
  19. 1b63bdb270c1066520a5ae37dcea3d5c3b9c5e9af581e76bf1287f9f79f77f03
复制代码
用法和 Docker build 基本一致,迁移的话也没有太多学习成本。
4. 配置文件

同为 OCI 三剑客,Podman 、Buildah 配置文件也是通用的。
您可以在以下目录中找到默认的 Podman 、Buildah 的配置文件:

  • 全局配置文件:/etc/containers/
  • 用户配置文件:~/.config/containers/
ps:会优先使用用户配置文件,若没有则使用全局配置文件。
即:不同用户都可以单独指定自己的配置文件
在/etc/containers 目录下,包括多种配置文件:

  • storage.conf:存储相关配置
  • registries.conf:镜像仓库相关配置
  • policy.json:容器签名验证相关配置
  • auth.json:镜像仓库的认证信息,执行 login 命令后会将 token 存到该文件
  • ...
各个文件的具体配置可以参考:Podman&Buildah 配置文件说明
作为使用者,主要关系 registries.conf 配置,因此重点分析。
  1. vi /etc/containers/registries.conf
复制代码
完整内容

/etc/containers/registries.conf 完整内容如下:
  1. unqualified-search-registries = ["registry.access.redhat.com", "registry.redhat.io", "docker.io"]
  2. # 配置为 Docker.io 仓库的镜像源
  3. [[registry]]
  4. prefix = "docker.io"
  5. location = "registry-1.docker.io"
  6. # 为 Docker.io 配置镜像源
  7. [[registry.mirror]]
  8. location = "mirror.gcr.io"
  9. [[registry.mirror]]
  10. location = "mirror2.gcr.io"
  11. # 配置为私有仓库 10.10.10.49:5000 的镜像源
  12. [[registry]]
  13. prefix = "10.10.10.49:5000"
  14. location = "10.10.10.49:5000"
  15. insecure = true
  16. # 配置私有仓库镜像源
  17. [[registry.mirror]]
  18. location = "mirror.gcr.io"
  19. short-name-mode = "permissive
复制代码
大致可以分为以下几部分:

  • 默认镜像仓库
  • 为镜像仓库配置 Insecure、Mirror 等
  • shortName 处理模式
不同仓库配置使用 [[registry]] 块进行区分。
注意:下面这样的配置是 V1 版本,已经废弃了,虽然还可以使用,但是不推荐。
  1. [registries.search]
  2. registries = ['registry1.com', 'registry2.com']
  3. [registries.insecure]
  4. registries = ['registry3.com']
  5. [registries.block]
  6. registries = ['registry.untrusted.com', 'registry.unsafe.com']
复制代码
参数解释

官方文档:containers-registries.conf.5.md
unqualified-search-registries

unqualified-search-registries 是一个配置项,用来指定当拉取一个 没有指定完整路径(即不包含域名和路径) 的镜像时,应该尝试哪些仓库(注册表)。这通常适用于 “没有指定镜像仓库” 的情况。
  1. unqualified-search-registries = ["registry.access.redhat.com", "registry.redhat.io", "docker.io"]
复制代码
一句话描述:在拉取没有指定完整路径(即不包含域名和路径) 的镜像时,应该尝试哪些仓库(注册表)。
short-name-mode

short-name-mode 选项定义了如何处理不带仓库路径的镜像名(例如,golang:1.20)。有三种模式:

  • disabled:不允许使用短名称,必须指定完整的仓库路径。
  • permissive(默认):允许使用短名称,并尝试按顺序从配置的注册表列表中查找镜像。
  • full:只有在仓库名称为完整名称时才能拉取镜像。
默认值就可以了,不用改。
  1. short-name-mode = "permissive
复制代码
prefix

Registry 块下的 prefix 用于匹配在拉取镜像时会用那个 Registry 块里的配置,只会使用最长匹配的 Registry 块。
假设有下面这样的配置,包含两个 Registry 块
  1. [[registry]]
  2. prefix = "docker.io"
  3. [[registry]]
  4. prefix = "docker.io.example.com"
复制代码
当我们拉取镜像docker.io.example.com/library/busybox:latest 时,根据镜像完整命令中解析得到一个域名,然后和我们的配置文件中的 prefix 进行匹配,最终会匹配到第二个 Registry 块,这样就会使用该 Registry 块中的配置。
一句话描述:一般填写 Registry 地址即可,但是需要按照 *.example.com 格式,或者就是指定 location
location

Registry 块中的 location 用于指定最终拉取镜像时访问的地址。
我们在拉取镜像时指定的是 docker.io/library/busybox:1.36,但是最终会去 registry-1.docker.io 这个地址拉取。
对于 docker.io 来说,就需要以下配置文件:
  1. [[registry]]
  2. prefix = "docker.io"
  3. location = "registry-1.docker.io"
复制代码
还有就是 prefix 不是*.example.com 格式时,也必须指定 location,内容和 prefix 一致就行。
一句话描述:用于指定真正拉取镜像的地址,例如 registry-1.docker.io,或者当 prefix 不是*.example.com 格式时,也必须指定 location,内容和 prefix 一致就行。
insecure

registry 块下的 Insecure 参数比较常见,就是配置使用 http 访问该仓库,一般自建私有仓库会用到该配置。
  1. # 配置为私有仓库 10.10.10.49:5000 的镜像源
  2. [[registry]]
  3. prefix = "10.10.10.49:5000"
  4. location = "10.10.10.49:5000"
  5. insecure = true
复制代码
blocked

官方解释是这样的: If true, pulling images with matching names is forbidden.
默认是 false,配置为 true 之后就不能冲对应 Prefix 指定的镜像仓库中拉取镜像了。
  1. # 配置为私有仓库 10.10.10.49:5000 的镜像源
  2. [[registry]]
  3. prefix = "10.10.10.49:5000"
  4. blocked = false
复制代码
一句话描述:用于关闭某些禁止使用的仓库。
mirror

对于部分无法拉取或拉取慢的仓库,可以配置 mirror 仓库。
  1. # 配置 Docker 的镜像源[[registry]]
  2. prefix = "docker.io"
  3. location = "registry-1.docker.io"[[registry.mirror]]location = "docker.m.daocloud.io"
复制代码
registry.mirror 块放在那个 Registry 块下面就是为哪个仓库配置的 Mirror。
参考配置文件

以下就是一个比较常用的配置文件 Demo,包括了 location、mirror、insecure 等配置,增加其他镜像仓库时可以做参考。
  1. unqualified-search-registries = ["docker.io"]short-name-mode = "permissive"# 配置 Docker 的镜像源[[registry]]
  2. prefix = "docker.io"
  3. location = "registry-1.docker.io"[[registry.mirror]]location = "docker.m.daocloud.io"# 配置为私有仓库 "172.20.150.222" 的镜像源[[registry]]prefix = "172.20.150.222"location = "172.20.150.222"insecure = true
复制代码
5. 进阶用法

这里主要分享一些进阶的用法,包括:

  • 多阶段构建
  • 多架构镜像构建
  • CI 环境中使用 Buildah
多阶段构建

多阶段构建是一种优化镜像大小的常用手段,通过将程序编译环境和运行环境分开来降低最终镜像大小。
用一个简单的 Go 程序演示一下多阶段构建。
main.go

使用 net/http 启动一个 http 服务。
  1. // main.go
  2. package main
  3. import (
  4.         "fmt"
  5.         "log"
  6.         "net/http"
  7. )
  8. func handler(w http.ResponseWriter, r *http.Request) {
  9.         fmt.Fprintf(w, "Hello, World!")
  10. }
  11. func main() {
  12.         http.HandleFunc("/", handler)
  13.         log.Fatal(http.ListenAndServe(":8080", nil))
  14. }
复制代码
Dockerfile

多阶段构建核心其实是 Dockerfile,可以看到当前 Dockerfile 有两个 FROM 语句,分别对应到编译阶段和运行阶段。

  • 编译阶段:使用 golang:1.20-alpine 作为基础镜像,保证 Go 程序可以正常编译
  • 运行阶段:因为 Go 程序编译后二进制可以直接运行,不在依赖 Go 环境了,因此直接使用 alpine 作为基础镜像,减少最终镜像的体积
  1. # Stage 1: Build stage (builder)
  2. FROM golang:1.20-alpine as builder
  3. # Set the Current Working Directory inside the container
  4. WORKDIR /app
  5. # Copy the source code into the container
  6. COPY . .
  7. # Build the Go binary
  8. RUN CGO_ENABLED=0 go build main.go
  9. # Stage 2: Runtime stage
  10. FROM alpine:latest
  11. # Install the necessary libraries to run the binary (if any)
  12. RUN apk --no-cache add ca-certificates
  13. # Set the Current Working Directory inside the container
  14. WORKDIR /root/
  15. # Copy the compiled binary from the builder stage
  16. COPY --from=builder /app/main .
  17. # Expose port 8080
  18. EXPOSE 8080
  19. # Run the Go application
  20. CMD ["./main"]
复制代码
构建
  1. buildah build -t server:v0.0.1 .
复制代码
输出如下:
  1. [root@builder ~]# buildah build -t server:v0.0.1 .
  2. [1/2] STEP 1/4: FROM golang:1.20-alpine AS builder
  3. [1/2] STEP 2/4: WORKDIR /app
  4. [1/2] STEP 3/4: COPY . .
  5. [1/2] STEP 4/4: RUN CGO_ENABLED=0 go build main.go
  6. [2/2] STEP 1/6: FROM alpine:latest
  7. Resolved "alpine" as an alias (/etc/containers/registries.conf.d/000-shortnames.conf)
  8. Trying to pull docker.io/library/alpine:latest...
  9. Getting image source signatures
  10. Copying blob 38a8310d387e done   |
  11. Copying config 4048db5d36 done   |
  12. Writing manifest to image destination
  13. [2/2] STEP 2/6: RUN apk --no-cache add ca-certificates
  14. fetch https://dl-cdn.alpinelinux.org/alpine/v3.21/main/x86_64/APKINDEX.tar.gz
  15. fetch https://dl-cdn.alpinelinux.org/alpine/v3.21/community/x86_64/APKINDEX.tar.gz
  16. (1/1) Installing ca-certificates (20241010-r0)
  17. Executing busybox-1.37.0-r8.trigger
  18. Executing ca-certificates-20241010-r0.trigger
  19. OK: 7 MiB in 16 packages
  20. [2/2] STEP 3/6: WORKDIR /root/
  21. [2/2] STEP 4/6: COPY --from=builder /app/main .
  22. [2/2] STEP 5/6: EXPOSE 8080
  23. [2/2] STEP 6/6: CMD ["./main"]
  24. [2/2] COMMIT server:v0.0.1
  25. Getting image source signatures
  26. Copying blob 3e01818d79cd skipped: already exists
  27. Copying blob 529cb79624ea done   |
  28. Copying config 8d0a6344f5 done   |
  29. Writing manifest to image destination
  30. --> 8d0a6344f55c
  31. Successfully tagged localhost/server:v0.0.1
  32. 8d0a6344f55c0611c94b23f2571adb0ba1ce98ee1d5009c79fd656fd42247c1b
复制代码
多架构镜像构建

很多应用程序和服务都需要在不同架构的机器上运行,如 amd64arm64,但我们不可能为每一个架构都准备一台专门的机器。
之前主要用的是 Docker Buildx,不过 Buildah 也是支持多架构构建的。
ps:当然了,都要借助 qemu
安装 qemu-user-static

buildah 使用 qemu 来模拟不同架构。
首先需要确保你的系统上安装了 qemu。
ps:经过测试,如果你的 Dockerfile 中没有 RUN 命令去执行某些操作其实不需要 qemu 也能正常构建多架构镜像。
直接包管理工具安装:
  1. # Ubuntu
  2. sudo apt-get install qemu-user-static
  3. # Fedora
  4. sudo dnf install qemu-user-static
复制代码
构建并推送多架构镜像

和 Docker buildx 一样,Buildah 也通过 --platform 参数来指定要构建的架构。
不过 Buildah 没有 --push 参数,不能在构建完成后自动生成 manifest 并推送,因此需要手动创建一个 manifest 并将构建的镜像和 manifest 绑定并手段推送到最终镜像仓库。
整体流程大致分为三步:

  • 1)创建 Manifest

    • 这里创建的 manifest 其实是一个镜像,会出现在 buildah images 列表里
    • 名称推荐使用完整镜像名,例如:172.20.150.222/lixd/nginx-hello:v0.0.2,不过用别的也不影响

  • 2)构建多架构镜像

    • 注意要使用 --manifest 代替 --tag 参数,让镜像和 manifest 绑定

  • 3)推送 Manifest 和 Image 到镜像仓库

    • Push 时需要指定 Manifest 名称,同时还要指定完整的 Registry 路径
    • 如果 manifest 用的就是完整镜像名,这里二者就是一样的

Command 如下:
  1. PUSH_WAY=172.20.150.222/lixd/nginx-hello:v0.0.2
  2. # 创建 manifest
  3. buildah manifest create ${PUSH_WAY}
  4. # 构建
  5. buildah build --manifest ${PUSH_WAY} --platform linux/amd64,linux/arm64 .
  6. # 推送
  7. buildah manifest push ${PUSH_WAY} --all "docker://${PUSH_WAY}"
复制代码
定义了一个简单的脚本来实现构建多架构镜像,build.sh 完整内容如下:
  1. # Set the required variables
  2. export REGISTRY="172.20.150.222"
  3. export REPOSITORY="lixd"
  4. export IMAGE_NAME="server"
  5. export IMAGE_TAG="v0.0.1"
  6. export BUILD_PATH="."
  7. # Platforms to build for
  8. export PLATFORMS="linux/amd64,linux/arm64"
  9. PUSH_WAY="${REGISTRY}/${REPOSITORY}/${IMAGE_NAME}:${IMAGE_TAG}"
  10. MANIFEST_NAME=$PUSH_WAY
  11. echo $PUSH_WAY
  12. # Create a multi-architecture manifest
  13. ### Infact,this command can be ignore,when build will creates manifest list if it does not exist
  14. buildah manifest create ${MANIFEST_NAME}
  15. # Build the container for all platform
  16. ### Note: When more than one platform,use manifest to instead of tag flag.
  17. buildah build \
  18. --manifest ${MANIFEST_NAME} \
  19. --platform ${PLATFORMS} \
  20. ${BUILD_PATH}
  21. # Push the full manifest, with both CPU Architectures
  22. ### If Push To Docker Hub or Gitlab Registry,need add flag:--format v2s2,Default Is oci
  23. buildah manifest push --all \
  24.   ${MANIFEST_NAME} \
  25.   "docker://${PUSH_WAY}"
复制代码
就以上一步的 Go Demo 编译生成一个多架构镜像:
  1. bash build.sh
复制代码
输出如下:
  1. root@builder-ubuntu:~/multistage# bash build.sh
  2. 172.20.150.222/lixd/server:v0.0.1
  3. e6ba6ec459a1fd7303c19242ab0d85c7c23af8cb156ce348928e2a4135327f15
  4. # amd64
  5. [linux/amd64] STEP 1/4: FROM golang:1.20-alpine AS builder
  6. [linux/amd64] STEP 2/4: WORKDIR /app
  7. [linux/amd64] STEP 3/4: COPY . .
  8. [linux/amd64] STEP 4/4: RUN CGO_ENABLED=0 go build main.go
  9. [linux/amd64] STEP 1/6: FROM alpine:latest
  10. [linux/amd64] STEP 2/6: RUN apk --no-cache add ca-certificates
  11. [linux/amd64] STEP 3/6: WORKDIR /root/
  12. [linux/amd64] STEP 4/6: COPY --from=builder /app/main .
  13. [linux/amd64] STEP 5/6: EXPOSE 8080
  14. [linux/amd64] STEP 6/6: CMD ["./main"]
  15. # arm64
  16. [linux/arm64] [1/2] STEP 1/4: FROM golang:1.20-alpine AS builder
  17. [linux/arm64] [1/2] STEP 2/4: WORKDIR /app
  18. [linux/arm64] [1/2] STEP 3/4: COPY . .
  19. [linux/arm64] [1/2] STEP 4/4: RUN CGO_ENABLED=0 go build main.go
  20. [linux/amd64] [2/2] STEP 1/6: FROM alpine:latest
  21. [linux/arm64] [2/2] STEP 3/6: WORKDIR /root/
  22. [linux/arm64] [2/2] STEP 4/6: COPY --from=builder /app/main .
  23. [linux/arm64] [2/2] STEP 5/6: EXPOSE 8080
  24. [linux/arm64] [2/2] STEP 6/6: CMD ["./main"]
  25. [linux/arm64] [2/2] COMMIT
  26. # push
  27. Getting image source signatures
  28. Copying blob 977340364f39 skipped: already exists
  29. Copying blob d8b4b7adc1e8 done
  30. Copying config d97c60d03e done
  31. Writing manifest to image destination
  32. Storing signatures
  33. --> d97c60d03e8
  34. d97c60d03e822bb29c02c6b5c2c51b0f47871e52bc8c210c1e6324863797ce64
  35. Getting image list signatures
  36. Copying 4 of 4 images in list
  37. Writing manifest list to image destination
  38. ...
复制代码
CI 系统中使用

这里以 Github Action 为例,演示如何使用 Buildah 构建多架构镜像。
源码:lixd/github-action-lab
Dockerfile 和 main.go 和之前一样,就不贴了,感兴趣的同学可以调整 Github 查看~
Workflow.yaml

Workflow 就是最终执行的 Pipeline,分为几个步骤:

  • 1)启动运行环境,这里是 ubuntu-20.04
  • 2)Clone 代码
  • 3)安装 QEMU
  • 4)Buildah 构建多架构镜像
  • 5)推送到镜像仓库
  1. name: Build and Push Multi-Arch Image
  2. on:
  3.   push:
  4. env:
  5.   IMAGE_NAME: test-multi-arch
  6.   IMAGE_TAG: latest
  7.   IMAGE_REGISTRY: docker.io
  8.   IMAGE_NAMESPACE: lixd96
  9. jobs:
  10.   build:
  11.     name: Build and Push Multi-Architecture Image
  12.     runs-on: ubuntu-20.04
  13.     steps:
  14.       # Checkout the repository
  15.       - name: Checkout repository
  16.         uses: actions/checkout@v2
  17.       # Set up QEMU for cross-platform builds
  18.       - name: Set up QEMU for multi-arch support
  19.         uses: docker/setup-qemu-action@v1
  20.       # Build the Docker image using Buildah
  21.       - name: Build multi-architecture image
  22.         id: build-image
  23.         uses: redhat-actions/buildah-build@v2
  24.         with:
  25.           image: ${{ env.IMAGE_NAME }}
  26.           tags: ${{ env.IMAGE_TAG }}
  27.           archs: amd64,ppc64le,s390x,arm64  # Specify the architectures for multi-arch support
  28.           dockerfiles: |
  29.             ./Dockerfile
  30.       # Push the built image to the specified container registry
  31.       - name: Push image to registry
  32.         id: push-to-registry
  33.         uses: redhat-actions/push-to-registry@v2
  34.         with:
  35.           image: ${{ steps.build-image.outputs.image }}
  36.           tags: ${{ steps.build-image.outputs.tags }}
  37.           registry: ${{ env.IMAGE_REGISTRY }}/${{ env.IMAGE_NAMESPACE }}
  38.           username: ${{ secrets.REGISTRY_USERNAME }}  # Secure registry username
  39.           password: ${{ secrets.REGISTRY_PASSWORD }}  # Secure registry password
  40.       # Print the image URL after the image has been pushed
  41.       - name: Print pushed image URL
  42.         run: echo "Image pushed to ${{ steps.push-to-registry.outputs.registry-paths }}"
复制代码
验证

提交代码后,Workflow 会自动运行,到 Dockerhub 查看镜像是否成功推送:
2.png

可以看到,指定的 4 个架构都成功构建并推送过来了。
【Kubernetes 系列】持续更新中,搜索公众号【探索云原生】订阅,阅读更多文章。
3.png

6.小结

Buildah 提供了一种灵活且高效的镜像构建方式,无需 Docker 依赖,且支持 rootless 安全模式,适用于各种 DevOps 和 CI/CD 环境。它支持命令式和 Dockerfile 构建方式,还能进行多阶段构建和多架构镜像构建。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册