2.2. 使用 S2I 脚本从 Dockerfile 构建应用程序镜像
您可以将 Red Hat Software Collections 容器镜像用作构建器镜像,并使用 assemble 并运行 构建器镜像中包含的 S2I 脚本从 Dockerfile 构建应用程序镜像。有关 assemble 和 run S2I 脚本的更多信息,请参阅 第 1.1 节 “Red Hat Software Collections Container Images 作为构建器镜像”。
要使用 S2I 脚本从 Dockerfile 创建应用程序镜像,请按照以下步骤操作:
登录到容器 registry:
# podman login registry.redhat.io拉取构建器镜像:
# podman pull registry.redhat.io/rhscl_image_name- 准备应用程序代码。
为应用程序镜像创建自定义 Dockerfile 并确保您:
使用这一行定义构建器镜像:
FROM registry.redhat.io/rhscl_image_name将应用程序源放在
src/目录中,并确保默认容器用户具有访问源的足够权限:ADD --chown=1001:0 src /tmp/src使用
/usr/libexec/s2i/assemble脚本安装依赖项:RUN /usr/libexec/s2i/assemble使用
/usr/libexec/s2i/run脚本在生成的镜像中设置默认命令:CMD /usr/libexec/s2i/run
使用 podman 构建应用程序镜像:
# podman build -t application_image_name .使用 podman 运行应用程序镜像。例如,要在应用程序镜像中启动交互式 shell,请运行:
# podman run -ti application_image_name /bin/bash -l
例 2.2. 使用 S2I 脚本从 Dockerfile 创建 Python 3.8 应用程序镜像
这个示例演示了如何使用构建器镜像提供的 S2I 脚本从 Dockerfile 构建并运行 Python 3.8 应用程序。
登录到容器 registry:
# podman login registry.redhat.io拉取构建器镜像:
# podman pull registry.redhat.io/rhscl/python-38-rhel7拉取位于 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 的示例。
使用以下内容创建 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从上一步中准备的 Dockerfile 构建新镜像:
# podman build -t python-app .使用您的 Python 应用程序运行生成的镜像:
# podman run -d python-app
其它资源
- 从 Dockerfile 构建镜像
- Dockerfile 参考文档
-
相应的构建器镜像 README 文件中的 Source-to-Image 环境变量 部分,它们位于镜像内的
/help.1文件中,或者位于上游 GitHub 存储库中。 - 环境变量也包括了 红帽生态系统目录 中镜像的详细描述。