19.10. 在 Podman 容器内构建容器


要创建隔离的开发环境或自动 CI/CD 管道中的镜像构建,您可以从现有的 Podman 容器中构建容器镜像。您还可以在不修改主机系统配置的情况下测试并开发镜像。

先决条件

  • container-tools 元数据包已安装。

流程

  1. 登陆到 registry.redhat.io 注册中心:

    # podman login registry.redhat.io
  2. registry.redhat.io/rhel10/podman 镜像上运行基于容器的:

    # podman run --privileged --name podman_container -it \
      registry.redhat.io/rhel10/podman /bin/bash

    运行基于 registry.redhat.io/rhel10/podman 镜像的、名为 podman_container 的外部容器。--it 选项指定您要在容器内运行交互式 bash shell。--privileged 选项禁用将容器与主机隔离的安全功能。

  3. podman_container 容器中创建一个 Containerfile

    # vi Containerfile
    FROM registry.access.redhat.com/ubi10/ubi
    RUN dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
    RUN dnf -y install moon-buggy && dnf clean all
    CMD ["/usr/bin/moon-buggy"]

    Containerfile 中的命令会导致以下构建命令:

    • registry.access.redhat.com/ubi10/ubi 镜像构建容器。
    • 安装 epel-release-latest-8.noarch.rpm 软件包。
    • 安装 moon-buggy 软件包。
    • 设置容器命令。
  4. 使用 Containerfile 构建名为 moon-buggy 的新容器镜像:

    # podman build -t moon-buggy .
  5. 可选:列出所有镜像:

    # podman images
    REPOSITORY                  TAG      IMAGE ID      CREATED        SIZE
    localhost/moon-buggy  latest  c97c58abb564  13 seconds ago  1.67 GB
    registry.access.redhat.com/ubi10/ubi latest 4199acc83c6a  132seconds ago 213 MB
  6. moon-buggy 容器上运行一个新的基于容器的容器:

    # podman run -it --name moon moon-buggy
  7. 可选:标记 moon-buggy 镜像:

    # podman tag moon-buggy registry.example.com/moon-buggy
  8. 可选:将 moon-buggy 镜像推送到注册中心:

    # podman push registry.example.com/moon-buggy

    本例演示了 Podman 从此容器内构建并运行另一个容器。容器运行 Moon-buggy,它是一个简单的基于文本的游戏。

Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

關於紅帽

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

让开源更具包容性

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

关于红帽文档

Legal Notice

Theme

© 2026 Red Hat
返回顶部