In the age of containers and Kubernetes, applications needed to be configured with Dockerfiles and YAMLs. That can be another workload for developers and devops. Openshift provide a tool known as source-to-image (S2I) in order to simplify image creation from application sources. No additional configuration files are needed in this process. It takes application source and produces a runnable app in Openshift image registry or in external image registry. Then runs this image in a container. Let’s have a look at the command below.
Assuming openjdk-11-rhel7 exists in your Openshift image registry, which can be pulled from Redhat Container Catalog (https://catalog.redhat.com), this command tells Openshift to take application source from git repository and use openjdk-11-rhel7 as builder image to produce application image and other resources such as deploymentconfig, buildconfig and service.
We used openjdk-11-rhel7 here. What if Java 13,14 needed? What if OpenJ9 needed due to memory limits? You may not find the exact image you need in Redhat Container Catalog. Then you can produce your own builder image.
S2I builder images have assemble and run scripts. These scripts are must for a builder image. I will show how a custom builder image is built.
assemble
Following git clone, build pod runs sti-build container
- pulls image openjdk-11-rhel7 then runs a container (if incremental build is specified, pulls application image which was built from openjdk-11-rhel7 )
- application source was cloned with init container and assemble script runs /usr/local/s2i/assemble to compile, build and package your application.
-
commits new application image (s2i-springboot-sample-1) to image registry
- if incremental build is used maven artifacts are extracted from previous image and those are not downloaded again. Thus you save build time.
run
application image runs in a pod and runs script /usr/local/s2i/run
Building custom builder image
This guilde is to show you how you make changes in a builder image: https://github.com/gungor/s2i-builder-image . You can also setup any environment for any programming language. I used OpenJDK 14 with OpenJ9 and maven 3.6.3. Openshift cct_module has many scripts helping builder image. assemble, run, save-artifacts files and configuration files can be found there. I made minimum changes referencing openjdk-11-rhel7 image. Because several configuration files exists in cct_module and they use Openshift specific environment variables. Jvm start parameters adjusted by additional scripts in cct_module. Keeping them is useful. Dockerfile can be simplified though, since it contains several redundant files.
Changes I made seen above
- maven setup
- jdk setup
Minimum requirements for Builder Image
- assemble script in /usr/local/s2i
- run script in /usr/local/s2i
Dockerfile and all scripts are here: https://github.com/gungor/s2i-builder-image
Run build.sh, then you have a builder image in your docker registry. You can customize maven and jdk version in Dockerfile.
This guide is to just give you an idea about how builder images work and produced. Production usage of this image is not recommended.