第 2 章 使用 Red Hat Software Collections 容器镜像构建应用镜像
您可以使用 Red Hat Software Collections 容器镜像构建应用程序镜像的几个选项:
- 使用红帽提供的容器镜像作为基础镜像
- 使用 S2I 脚本的 Dockerfile
-
在 OpenShift 中使用
Source-to-Image
-
使用
source-to-image
实用程序
2.1. 使用 Red Hat Software Collections 镜像构建应用程序镜像作为基础镜像
使用红帽提供的容器镜像作为基础镜像:
为您的应用程序镜像创建一个 Dockerfile,并确保该文件包含以下行:
FROM registry.redhat.io/rhscl_image_name
通过将以下行放在 Dockerfile 中,将应用程序代码添加到镜像中:
ADD src /opt/app-root/src
使用
podman
构建应用程序镜像:# podman build -t application_image_name .
使用
podman
运行应用程序镜像。例如,要在应用程序镜像中启动交互式 shell,请运行:# podman run -ti application_image_name /bin/bash -l
例 2.1. 使用 rhscl/python-38-rhel7
基础镜像从 Dockerfile 构建的 Django 应用
本例演示了一个 Dockerfile,可用于从 rhscl/python-38-rhel7
容器镜像创建简单 Django 应用。
# Set base image FROM registry.redhat.io/rhscl/python-38-rhel7 # Add application sources ADD --chown=1001:0 app-src . # Install the dependencies RUN pip install -U "pip>=19.3.1" && \ pip install -r requirements.txt && \ python manage.py collectstatic --noinput && \ python manage.py migrate # Run the application CMD python manage.py runserver 0.0.0.0:8080