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:

  1. Create a Dockerfile for your application image and ensure it contains the following line:

    FROM registry.redhat.io/rhscl_image_name
  2. Add your application code in the src/ directory to the image by putting the following line into the Dockerfile:

    ADD src /opt/app-root/src
  3. Build your application image using podman:

    # podman build -t application_image_name .
  4. Run your application image using podman. For example, to launch an interactive shell within your application image, run:

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

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.

# 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

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:

  1. Log in to the container registry:

    # podman login registry.redhat.io
  2. Pull a builder image:

    # podman pull registry.redhat.io/rhscl_image_name
  3. Prepare an application code.
  4. Create a custom Dockerfile for your application image and ensure you:

    1. Define the builder image with this line:

      FROM registry.redhat.io/rhscl_image_name
    2. 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:

      ADD --chown=1001:0 src /tmp/src
    3. Install dependencies using the /usr/libexec/s2i/assemble script:

      RUN /usr/libexec/s2i/assemble
    4. Set the default command in the resulting image using the /usr/libexec/s2i/run script:

      CMD /usr/libexec/s2i/run
  5. Build your application image using podman:

    # podman build -t application_image_name .
  6. Run your application image using podman. For example, to launch an interactive shell within your application image, run:

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

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.

  1. Log in to the container registry:

    # podman login registry.redhat.io
  2. Pull a builder image:

    # podman pull registry.redhat.io/rhscl/python-38-rhel7
  3. Pull an application code available at https://github.com/sclorg/django-ex.git:

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

    Alternatively, use examples available at https://github.com/sclorg/s2i-python-container/tree/master/examples.

  4. Create a Dockerfile with this content:

    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. Build a new image from a Dockerfile prepared in the previous step:

    # podman build -t python-app .
  6. Run the resulting image with your Python application:

    # podman run -d python-app

Additional Resources

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:

  1. Build an application using an image available through OpenShift:

    $ oc new-app openshift_image_name~path_to_application_source_code

    For example, to build a Python 3.8 application using the supported image available through the python:3.8 imagestream tag in OpenShift, run:

    $ oc new-app python:3.8~https://github.com/sclorg/django-ex.git
  2. List available pods (instances):

    $ oc get pods
  3. Execute a selected pod on localhost:

    $ oc exec pod -- curl 127.0.0.1:8080

Additional Resources

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:

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:

  1. Subscribe to Red Hat Software Collections. For instructions, see Getting Access to Red Hat Software Collections.
  2. 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:

    # subscription-manager repos --enable rhel-server-rhscl-7-rpms --enable rhel-7-server-extras-rpms
  3. Install the source-to-image package:

    # yum install source-to-image
  4. Log in to the container registry:

    # docker login registry.redhat.io

    Pull a builder image:

    # docker pull registry.redhat.io/rhscl_image_name

    Build an application image from the application source code:

    # s2i build path_to_application_source_code_repository --context-dir=source_code_context_directory application_image_name
  5. 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.

  1. Log in to the container registry:

    # docker login registry.redhat.io
  2. Pull the rhscl/python-38-rhel7 builder image:

    # docker pull registry.redhat.io/rhscl/python-38-rhel7
  3. Build the test application from the GitHub s2i-python repository, in the 3.8/test/setup-test-app/ directory:

    # 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

    This produces a new application image, python-38-rhel7-app.

  4. Run the resulting python-38-rhel7-app image:

    # docker run -d -p 8080:8080 --name example-app python-38-rhel7-app
  5. Fetch the resulting example document from http://localhost:8080/:

    $ wget http://localhost:8080/
  6. Stop the container:

    # docker stop example-app

Additional Resources