7.2. 从最小状态构建基础镜像
在以前的版本中,您可以使用 RHEL 的镜像模式只构建标准镜像。标准镜像大致是一个无头服务器安装,虽然您也可以将其用于桌面,并包含用于网络、CLI 工具的许多建议软件包。
现在,您可以选择从标准镜像生成一个新的最小镜像,该镜像仅从 bootc、kernel 和 dnf 开始。然后可在多阶段构建中进一步扩展此镜像。在当前时间,registry 中未预先提供该镜像。
基础镜像包括 /usr/libexec/bootc-base-imagectl
工具,可让您生成自定义基础镜像。通过使用该工具,您可以构建基于基础镜像中选择的 RPM 软件包的根文件系统。
先决条件
- 标准 bootc 基础镜像。
流程
以下示例创建自定义最小基础镜像:
Begin with a standard bootc base image that is reused as a "builder" for the custom image. Configure and override source RPM repositories, if necessary. This step is not required when building up from minimal unless referencing specific content views or target mirrored/snapshotted/pinned versions of content. 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. Create a new, empty image from scratch. Copy the root file system built in the previous step into this image. 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. Set pipefail to display failures within the heredoc and avoid false-positive successful builds. Install required packages for our custom bootc image. Note that using a minimal manifest means we need to add critical components specific to our use case and environment. Remove package caches Clean up all logs and caches Run the bootc linter to perform build-time verification. Keep this as the last command in your build instructions. Close the shell command. Define required labels for this bootc image to be recognized as such. Optional labels that only apply when running this image as a container. These keep the default entry point running under systemd.
# Begin with a standard bootc base image that is reused as a "builder" for the custom image. FROM registry.redhat.io/rhel10/rhel-bootc:latest # Configure and override source RPM repositories, if necessary. This step is not required when building up from minimal unless referencing specific content views or target mirrored/snapshotted/pinned versions of content. # 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=minimal /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 required packages for our custom bootc image. # Note that using a minimal manifest means we need to add critical components specific to our use case and environment. dnf -y install NetworkManager openssh-server # Remove package caches dnf clean all # Clean up all logs and caches rm /var/{log,cache,lib}/* -rf # Run the bootc linter to perform build-time verification. Keep this as the last command in your build instructions. bootc container lint # Close the shell command. 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"]
Copy to Clipboard Copied!