2.2. 使用 S2I 脚本从 Dockerfile 构建应用程序镜像


您可以将 Red Hat Software Collections 容器镜像用作构建器镜像,并使用 assemble 并运行 构建器镜像中包含的 S2I 脚本从 Dockerfile 构建应用程序镜像。有关 assemblerun S2I 脚本的更多信息,请参阅 第 1.1 节 “Red Hat Software Collections Container Images 作为构建器镜像”

要使用 S2I 脚本从 Dockerfile 创建应用程序镜像,请按照以下步骤操作:

  1. 登录到容器 registry:

    # podman login registry.redhat.io
  2. 拉取构建器镜像:

    # podman pull registry.redhat.io/rhscl_image_name
  3. 准备应用程序代码。
  4. 为应用程序镜像创建自定义 Dockerfile 并确保您:

    1. 使用这一行定义构建器镜像:

      FROM registry.redhat.io/rhscl_image_name
    2. 将应用程序源放在 src/ 目录中,并确保默认容器用户具有访问源的足够权限:

      ADD --chown=1001:0 src /tmp/src
    3. 使用 /usr/libexec/s2i/assemble 脚本安装依赖项:

      RUN /usr/libexec/s2i/assemble
    4. 使用 /usr/libexec/s2i/run 脚本在生成的镜像中设置默认命令:

      CMD /usr/libexec/s2i/run
  5. 使用 podman 构建应用程序镜像:

    # podman build -t application_image_name .
  6. 使用 podman 运行应用程序镜像。例如,要在应用程序镜像中启动交互式 shell,请运行:

    # podman run -ti application_image_name /bin/bash -l

例 2.2. 使用 S2I 脚本从 Dockerfile 创建 Python 3.8 应用程序镜像

这个示例演示了如何使用构建器镜像提供的 S2I 脚本从 Dockerfile 构建并运行 Python 3.8 应用程序。

  1. 登录到容器 registry:

    # podman login registry.redhat.io
  2. 拉取构建器镜像:

    # podman pull registry.redhat.io/rhscl/python-38-rhel7
  3. 拉取位于 https://github.com/sclorg/django-ex.git 的应用代码:

    $ git clone https://github.com/sclorg/django-ex.git app-src

    或者,使用位于 https://github.com/sclorg/s2i-python-container/tree/master/examples 的示例。

  4. 使用以下内容创建 Dockerfile:

    FROM registry.redhat.io/rhscl/python-38-rhel7
    
    # Add application sources to a directory that the assemble script expects them
    # and set permissions so that the container runs without root access
    USER 0
    ADD app-src /tmp/src
    RUN chown -R 1001:0 /tmp/src
    USER 1001
    
    # Install the dependencies
    RUN /usr/libexec/s2i/assemble
    
    # Set the default command for the resulting image
    CMD /usr/libexec/s2i/run
  5. 从上一步中准备的 Dockerfile 构建新镜像:

    # podman build -t python-app .
  6. 使用您的 Python 应用程序运行生成的镜像:

    # podman run -d python-app

其它资源

Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2026 Red Hat
返回顶部