Chapter 2. Using Source-to-Image (S2I)

Source-to-Image (S2I) is a framework and a tool that allows you to write images which use the application source code as an input and produce a new image that runs the assembled application as an output. The main advantage of using the S2I tool for building reproducible container images is the ease of use for developers.

To use the S2I tool on your system, subscribe to Red Hat Software Collections and run the following command to install the source-to-image package:

# yum install source-to-image

Use the RHSM channel: rhel-server-rhscl-7-rpms. Note that the source-to-image package requires the docker package from the Red Hat Enterprise Linux Extras channel.

Alternatively, you can use the rhel-x86_64-server-7-rhscl-1 RHN channel, but note that the RHN channel is accessible only through Red Hat Satellite instances.

For details about subscribing to Red Hat Software Collections, see Getting Access to Red Hat Software Collections.

More information about the S2I tool is available at GitHub.

Note

Similarly to Red Hat Software Collections container images, the S2I tool runs only on Red Hat Enterprise Linux 7 Server, not on Red Hat Enterprise Linux 7 Workstation.

2.1. Build Process

The build process consists of the following three fundamental elements, which are combined into a final container image:

  • The source code of your application—​written in a programming language or framework.
  • Builder image—​container image provided by Red Hat that supports building images using the S2I tool.
  • S2I scripts that are part of the builder image.

During the build process, S2I creates a tar file that contains the source code and scripts, then streams that file into the builder image.

For more information on the Source-to-Image framework, see S2I Requirements.

2.2. Example: building a Python application from Git using S2I

This example shows how to build:

  • A new container image from the python-35-rhel7 builder image that is available in the Red Hat Container Registry, and
  • A test application available from a public Git repository in the GitHub sti-python repository, in the 3.5/test/setup-test-app/ directory.

    1. Install the S2I tool from the Red Hat Software Collections repository:

      # yum install source-to-image
    2. Pull the builder image:

      # docker pull registry.access.redhat.com/rhscl/python-35-rhel7
    3. Build the test application from the GitHub sti-python repository, in the 3.5/test/setup-test-app/ directory:

      # s2i build https://github.com/openshift/sti-python.git --context-dir=3.5/test/setup-test-app/ rhscl/python-35-rhel7 python-35-rhel7-app

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

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

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

      $ wget http://localhost:8080/

      The example document is returned.

    6. Stop the container:

      # docker stop example-app