Openshift S2I (Source-to-Image)

Ebru Dalkır
5 min readAug 29, 2021

Getting Build, as we all know, is the process of converting input parameters into a resulting object. Often the process is used to convert input parameters or source code into an executable image. A Build Config object is the description of the entire build process. OpenShift leverages Kubernetes by creating Docker containers from build images and pushing them to a Docker registry. The OpenShift build system provides extensible support for build strategies based on selectable types specified in the build API. There are three build strategies available:

  • Docker build
  • Source-to-Image (S2I) build
  • Custom build

Today I will talk about the use of Source-to-Image (S2I) build, which I used in the process of injecting the source code of an application into a docker image. In general, in the deployment processes, the source code of the application is built first, and then the resulting image is deployed according to the containerization tool you use. However, in a case I came across, this method did not meet my needs and I sought to find a different solution. After doing some research on the internet, I came across Openshift’s (S2I) solution. Well, I can hear you say what (S2I) is :) Let’s examine the usage stages of this structure together.

S2I is a framework that makes it easy to write images that take the application source code as input and produce a new image that runs the combined application as output. The OpenShift Container Platform provides an S2I (Source-to-Image) process for building and running applications where the source code of an application can be superimposed over a builder image (a technology image such as JBoss EAP). The S2I process first builds your app and then overlays it over the builder image to create an app image. After compilation is complete, the application image is sent to the integrated registry or a standalone registry in OpenShift.

I will explain this structure on the java application as an example. For other languages, you can refer to Openshift’s own documentation.

Red Hat Java S2I for OpenShift is a Source-to-Image (S2I) renderer image designed for use with OpenShift. It allows users to build and run simple Java applications within a containerized image in OpenShift.

1- First, log in to your OpenShift console by coming to your terminal screen, running the command below and providing the credentials.

$ oc login

2- Let’s create a new project.

$ oc new-project <project-name>

3- To create a new build configuration, create a new binary build by specifying the image stream and the application name.

$ oc new-build --binary=true  --name="jdk-app-name" --image-stream=redhat-openjdk18-openshift:1.8

Output: It should be like below.

--> Found image de256a7 (5 months old) in image stream "openshift/redhat-openjdk18-openshift" under tag "1.8" for "redhat-openjdk18-openshift:1.8"                                                                                                                                                                                                                          Java Applications                                                                                                       -----------------                                                                                                       Platform for building and running plain Java applications (fat-jar and flat classpath)                                                                                                                                                          Tags: builder, java                                                                                                                                                                                                                             * A source build using binary input will be created                                                                       * The resulting image will be pushed to image stream tag "jdk-app-name:latest"                                          
* A binary build was created, use 'start-build --from-dir' to trigger a new build --> Creating resources with label build=jdk-app-name ... imagestream.image.openshift.io "jdk-app-name" created buildconfig.build.openshift.io "jdk-app-name" created --> Success

You can see the image stream consisting of Builds>Image Streams from the OCP interface.

4- We do the build process with the new build configuration created. Start the binary build, in the oc executable we define the jar file it will use for the OpenShift build. You should run this command in the directory where the jar file is located or give the long path of the jar file.

$ oc start-build jdk-app-name --from-file=<jar-name>.jar

Note: Here you can parametrically add the arguments to the command according to the requests of the application you want to build.

Output: It should be like below.

Uploading file "<jar-name>.jar" as binary input for the build ...                                           ................................................................                                                        Uploading finished                                                                                                      build.build.openshift.io/jdk-app-name-1 started

5-With this step, we create a new OpenShift application based on the build step. You can add to the command as an argument. Here I added the arguments time-zone and allow-missing-imagestream-tags (indicating that image stream tags that do not exist with true value should still be used) to my command. You can run “oc --help” in your terminal to examine more command variants.

$ oc new-app jdk-app-name:latest --name=jdk-app-name --allow-missing-imagestream-tags=true -e TZ=Europe/Istanbul

Output: It should be like below.

--> Found image 655129d (3 minutes old) in image stream "****-*****/jdk-app-name" under tag "latest" for "jdk-app-name:latest"                                                                                                                                                                                                                                          Java Applications                                                                                    -----------------                                                                                                       Platform for building and running plain Java applications (fat-jar and flat classpath)                                                                                                                                                          Tags: builder, java                                                                                                                                                                                                                             * This image will be deployed in deployment config "jdk-app-name"                                                       * Ports 8080/tcp, 8443/tcp, 8778/tcp will be load balanced by service "jdk-app-name"                                      
* Other containers can access this service through the hostname "jdk-app-name"
--> Creating resources ... deploymentconfig.apps.openshift.io "jdk-app-name" created service "jdk-app-name" created --> Success Application is not exposed. You can expose services to the outside world by executing one or more of the commands below: 'oc expose svc/jdk-app-name' Run 'oc status' to view your app.

After these processes, you can see your application under Workloads > Deployment Configs in the OCP interface by clicking pod and examine the logs.

In addition, you can check the memory, cpu and other network usage values of the pod.

I hope it was a useful writing. Enjoyable reading already…

--

--