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


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

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

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

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

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

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

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

使用全新功能中的 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
    Copy to Clipboard Toggle word wrap

验证

  1. 保存并构建镜像。

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

    $ podman build -t quay.io/<namespace>/<image>:<tag> .
    Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2025 Red Hat