Chapter 2. Building Application Images Using Red Hat Software Collections Container Images You have several options how to build your application images using Red Hat Software Collections container images: Use container images provided by Red Hat as base images Use a Dockerfile with S2I scripts Use Source-to-Image in OpenShift Use the source-to-image utility 2.1. Building Application Images Using Red Hat Software Collections Images as Base Images To use container images provided by Red Hat as base images: Create a Dockerfile for your application image and ensure it contains the following line: Add your application code in the src/ directory to the image by putting the following line into the Dockerfile: Build your application image using podman : Run your application image using podman . For example, to launch an interactive shell within your application image, run: Example 2.1. A Django application built from a Dockerfile using the rhscl/python-38-rhel7 base image This example shows a Dockerfile that you can use for creating a simple Django application from the rhscl/python-38-rhel7 container image. Additional Resources Building an image from a Dockerfile Dockerfile reference document 2.2. Building Application Images from Dockerfiles Using S2I Scripts You can use Red Hat Software Collections container images as builder images and build your application images from Dockerfile using the assemble and run S2I scripts included in the builder images. For more information about the assemble and run S2I scripts, see Section 1.1, "Red Hat Software Collections Container Images as Builder Images" . To create an application image from a Dockerfile using S2I scripts, follow these steps: Log in to the container registry: Pull a builder image: Prepare an application code. Create a custom Dockerfile for your application image and ensure you: Define the builder image with this line: Put the application source in the src/ directory into the container and ensure that the default container user has sufficient permissions to access the source: Install dependencies using the /usr/libexec/s2i/assemble script: Set the default command in the resulting image using the /usr/libexec/s2i/run script: Build your application image using podman: Run your application image using podman. For example, to launch an interactive shell within your application image, run: Example 2.2. Creating a Python 3.8 application image from a Dockerfile using S2I scripts This example shows how to build and run a Python 3.8 application from a Dockerfile with S2I scripts provided by the builder image. Log in to the container registry: Pull a builder image: Pull an application code available at https://github.com/sclorg/django-ex.git : Alternatively, use examples available at https://github.com/sclorg/s2i-python-container/tree/master/examples . Create a Dockerfile with this content: Build a new image from a Dockerfile prepared in the step: Run the resulting image with your Python application: Additional Resources Building an image from a Dockerfile Dockerfile reference document The Environment variables for Source-to-Image section in the respective builder image README file, which is located in the /help.1 file inside the image, or in the upstream GitHub repository . The environment variables are also documented in the detailed description of the image in the Red Hat Ecosystem Catalog . 2.3. Building application images using Source-to-Image in OpenShift Source-to-Image (S2I) in OpenShift is a framework which enables you to write images that take application source code as an input, use a builder Red Hat Software Collections container image, and produce a new image that runs the assembled application as an output. To create an application using S2I in OpenShift: Build an application using an image available through OpenShift: For example, to build a Python 3.8 application using the supported image available through the python:3.8 imagestream tag in OpenShift, run: List available pods (instances): Execute a selected pod on localhost: Additional Resources OpenShift Container Platform documentation S2I Requirements source-to-image README file on GitHub The Environment variables for Source-to-Image section in the respective builder image README file. 2.4. Building application images using the source-to-image utility The Red Hat Software Collections offering provides the source-to-image utility, which you can use without OpenShift on Red Hat Enterprise Linux 7 Server. Note The source-to-image utility is available only for Red Hat Enterprise Linux 7 and works only with images pulled by docker . You cannot use podman with the source-to-image utility. The build process consists of the following three fundamental elements, which are combined into a final container image: Source code of your application, written in a programming language or framework. A builder image, which is a Red Hat Software Collections container image that supports building images using the source-to-image utility. S2I scripts that are part of the builder image. For more information about these scripts, see Section 1.1, "Red Hat Software Collections Container Images as Builder Images" . During the build process, the source-to-image utility creates a .tar file that contains the source code and scripts, then streams that file into the builder image. To use the source-to-image utility on your system: Subscribe to Red Hat Software Collections. For instructions, see Getting Access to Red Hat Software Collections . Enable the Red Hat Software Collections Server repository, which provides the source-to-image package, and the Red Hat Enterprise Linux 7 Server repository, which includes the docker package, required by source-to-image : Install the source-to-image package: Log in to the container registry: Pull a builder image: Build an application image from the application source code: Run the resulting image using docker . Example 2.3. Building a Python 3.8 application from a Git repository using the source-to-image utility This example shows how to build a test application available from a public Git repository using the rhscl/python-38-rhel7 builder image and the source-to-image utility. Log in to the container registry: Pull the rhscl/python-38-rhel7 builder image: Build the test application from the GitHub s2i-python repository, in the 3.8/test/setup-test-app/ directory: This produces a new application image, python-38-rhel7-app . Run the resulting python-38-rhel7-app image: Fetch the resulting example document from http://localhost:8080/ : Stop the container: Additional Resources S2I Requirements source-to-image README file on GitHub The Environment variables for Source-to-Image section in the respective builder image README file, which is located in the /help.1 file inside the image, or in the upstream GitHub repository .

Commands:
FROM registry.redhat.io/ rhscl_image_name
ADD src /opt/app-root/src
podman build -t application_image_name .
podman run -ti application_image_name /bin/bash -l
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
podman login registry.redhat.io
podman pull registry.redhat.io/ rhscl_image_name
FROM registry.redhat.io/ rhscl_image_name
ADD --chown=1001:0 src /tmp/src
RUN /usr/libexec/s2i/assemble
CMD /usr/libexec/s2i/run
podman build -t application_image_name .
podman run -ti application_image_name /bin/bash -l
podman login registry.redhat.io
podman pull registry.redhat.io/rhscl/python-38-rhel7
git clone https://github.com/sclorg/django-ex.git app-src
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
podman build -t python-app .
podman run -d python-app
oc new-app openshift_image_name ~ path_to_application_source_code
oc new-app python:3.8~https://github.com/sclorg/django-ex.git
oc get pods
oc exec pod -- curl 127.0.0.1:8080
subscription-manager repos --enable rhel-server-rhscl-7-rpms --enable rhel-7-server-extras-rpms
yum install source-to-image
docker login registry.redhat.io
docker pull registry.redhat.io/ rhscl_image_name
s2i build path_to_application_source_code_repository --context-dir= source_code_context_directory application_image_name
docker login registry.redhat.io
docker pull registry.redhat.io/rhscl/python-38-rhel7
s2i build https://github.com/sclorg/s2i-python-container.git --context-dir=3.8/test/setup-test-app/ registry.redhat.io/rhscl/python-38-rhel7 python-38-rhel7-app
docker run -d -p 8080:8080 --name example-app python-38-rhel7-app
wget http://localhost:8080/
docker stop example-app