第 7 章 从头开始创建 bootc 镜像


使用从头开始创建的 bootc 镜像,您可以控制底层镜像内容,并根据您的要求定制您的系统环境。

您可以使用 bootc-base-imagectl 命令,使用现有 bootc 基础镜像作为构建环境,来从头开始创建 bootc 镜像,从而对构建过程中包含的内容提供更好的控制。此过程将用户 RPM 作为输入,因此如果 RPM 发生了变化,您需要重新构建镜像。

自定义库衍生于基础容器,除非使它们成为容器管道的一部分,否则不会自动消耗对默认基础镜像的更改。

您可以对任何 bootc 容器镜像使用 bootc-base-imagectl rechunk 子命令。

如果要执行内核管理,您不需要从头开始创建一个 bootc 镜像。请参阅 在 bootc 系统中管理内核参数

7.1. 使用固定内容构建镜像

为确保基础镜像版本包含特定版本的一组软件包,例如,由 lockfile 或 rpm-mdyum repository 定义的,您可以使用多个工具来管理 rpm-mdyum repository 存储库的快照。

使用 从头创建的 bootc 镜像 功能,您可以配置并覆盖源 RPM 存储库中的软件包信息,同时引用镜像的、固定的或快照的存储库内容。因此,您获得了对软件包版本和其依赖项的控制。

例如,在以下情况下,您可能希望获得对软件包版本和其依赖项的控制:

  • 由于严格的认证和合规性要求,您需要使用特定的软件包版本。
  • 您需要使用特定的软件版本,以支持关键依赖项。

先决条件

  • 一个标准的 bootc 基础镜像。

流程

  • 以下示例从头开始创建一个具有固定内容的 bootc 镜像:

    # Begin with a standard bootc base image that serves as a "builder" for our custom image.
    FROM registry.redhat.io/rhel10/rhel-bootc:latest
    # Configure and override source RPM repositories, if necessary. The following step is required when referencing specific content views or target mirrored/snapshotted/pinned versions of content.
    RUN rm -vf /etc/yum.repos.d
    COPY mypinnedcontent.repo /etc/yum.repos
    # Add additional repositories to apply customizations to the image. However, referencing a custom manifest in this step is not currently supported without forking the code.
    # Build the root file system by using the specified repositories and non-RPM content from the "builder" base image.
    # If no repositories are defined, the default build will be used. You can modify the scope of packages in the base image by changing the manifest between the "standard" and "minimal" sets.
    RUN /usr/libexec/bootc-base-imagectl build-rootfs --manifest=standard /target-rootfs
    # Create a new, empty image from scratch.
    FROM scratch
    # Copy the root file system built in the previous step into this image.
    COPY --from=builder /target-rootfs/ /
    # Apply customizations to the image. This syntax uses "heredocs" https://www.docker.com/blog/introduction-to-heredocs-in-dockerfiles/ to pass multi-line arguments in a more readable format.
    RUN <<EORUN
    # Set pipefail to display failures within the heredoc and avoid false-positive successful builds.
    set -xeuo pipefail
    # Install necessary packages, run scripts, etc.
    dnf -y install NetworkManager emacs
    # Remove leftover build artifacts from installing packages in the final built image.
    dnf clean all
    rm /var/{log,cache,lib}/* -rf
    EORUN
    # Define required labels for this bootc image to be recognized as such.
    LABEL containers.bootc 1
    LABEL ostree.bootable 1
    # Optional labels that only apply when running this image as a container. These keep the default entry point running under systemd.
    STOPSIGNAL SIGRTMIN+3
    CMD ["/sbin/init"]
    # Run the bootc linter to avoid encountering certain bugs and maintain content quality. Place this command last in your Containerfile.
    RUN bootc container lint

验证

  1. 保存并构建镜像。

    $ podman build -t quay.io/<namespace>/<image>:<tag> . --cap-add=all --security-opt=label=type:container_runtime_t --device /dev/fuse
  2. 使用当前目录中的 Containerfile 构建 <_image_> 镜像:

    $ podman build -t quay.io/<namespace>/<image>:<tag> .
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2026 Red Hat
返回顶部