第 2 章 构建和测试 RHEL bootc 镜像
您可以使用 Podman 和 Containerfiles 构建和测试 RHEL 容器镜像,以便跨环境有效地创建、自定义和共享可引导的 RHEL 系统镜像。您也可以使用其他工具,如 OpenShift Container Platform。有关使用容器配置 RHEL 系统的更多信息,请参阅 rhel-bootc-examples 存储库。
图 2.1. 使用 Containerfile 中的指令构建镜像,测试容器,将镜像推送到 registry,并与其他人共享它

通用 Containerfile
结构如下:
FROM registry.redhat.io/rhel10/rhel-bootc:latest RUN dnf -y install [software] [dependencies] && dnf clean all ADD [application] ADD [configuration files] RUN [config scripts]
FROM registry.redhat.io/rhel10/rhel-bootc:latest
RUN dnf -y install [software] [dependencies] && dnf clean all
ADD [application]
ADD [configuration files]
RUN [config scripts]
可以在 Containerfile
和 Dockerfile
中使用的命令一样。
但是,当将 rhel-10-bootc
镜像安装到系统时,Containerfile
中的以下命令会被忽略:
-
ENTRYPOINT
和CMD
(OCI:Entrypoint/Cmd
):您可以设置CMD /sbin/init
。 -
ENV
(OCI:Env
): 更改systemd
配置以配置全局系统环境。 -
EXPOSE
(OCI:exposePorts
):它独立于系统防火墙和运行时网络功能。 -
USER
(OCI:User
):配置 RHEL bootc 中的单个服务,以作为非特权用户运行。
rhel-10-bootc
容器镜像重复利用 OCI 镜像格式。
-
rhel-10-bootc
容器镜像在安装到系统时会忽略容器配置部分(Config
)。 -
当使用容器运行时(如
podman
或docker
)运行此镜像时,rhel-10-bootc
容器镜像不会忽略容器配置部分(Config
)。
2.1. 构建容器镜像
要使用 Containerfile
中的指令构建镜像,请使用 podman build
命令。
先决条件
-
container-tools
元数据包已安装。
流程
创建
Containerfile
:cat Containerfile FROM registry.redhat.io/rhel10/rhel-bootc:latest RUN dnf -y install cloud-init && \ ln -s ../cloud-init.target /usr/lib/systemd/system/default.target.wants && \ ln -s ../cloud-init.target /usr/lib/systemd/system/default.target.wants && \ dnf clean all dnf clean all
$ cat Containerfile FROM registry.redhat.io/rhel10/rhel-bootc:latest RUN dnf -y install cloud-init && \ ln -s ../cloud-init.target /usr/lib/systemd/system/default.target.wants && \ dnf clean all
Copy to Clipboard Copied! 这个
Containerfile
示例添加了cloud-init
工具,因此它可以自动获取 SSH 密钥,并运行基础设施中的脚本,也可以从实例元数据中收集配置和 secret。例如,您可以将此容器镜像用于预生成的 AWS 或 KVM 客户机操作系统。使用当前目录中的
Containerfile
构建<image>
镜像:podman build -t quay.io/<namespace>/<image>:<tag> .
$ podman build -t quay.io/<namespace>/<image>:<tag> .
Copy to Clipboard Copied!
验证
列出所有镜像:
podman images
$ podman images REPOSITORY TAG IMAGE ID CREATED SIZE localhost/<image> latest b28cd00741b3 About a minute ago 2.1 GB
Copy to Clipboard Copied!