Hands on with deploying Microservices into Docker Containers using OpenShift S2I

--

In a typical deployment when we are using a VM’s then we need to prepare the environment by installing the needed dependencies and then build the JAR and execute the command to start the application. In the other way, if we are using the docker, then we need to prepare the Dockerfile with the instructions and then the docker will do the deployment along with the code changes we had. Along with this, when OpenShift came in, it has introduced a mechanism called source-to-image. This has reduced the complexity of working with building docker images in the developer perspective.

Let’s see how we can setup a environment with source to image build and deployment.

Step — 1:

Go to https://github.com/openshift/source-to-image/releases and download the binaries.

After extraction setup the location of the directory as a PATH variable in ~/.bashrc

Step — 2:

To prepare for the custom builder image creation execute the below command to create the directory structure.

Once successful the below directory structure will be created.

Step — 3

Before we start the scripting, we will look into what we are going to do through a flow diagram.

Here, first we are getting a ubuntu image from docker hub and then to create a builder image we are setting up the dependencies for our spring boot microservice through the Dockerfile. Below is the Dockerfile i have created for this demonstration. I have mentioned comments to get understand why they were used.

Next file we need to update is the assemble file. This is needed to build the application image.

Then the run file which used to run the application.

Finally the usage file.

Here we do not need any update on the save-artifacts file as it is used for incremental builds. Refer [1] for more information on incremental builds.

https://docs.openshift.com/enterprise/3.0/dev_guide/builds.html#incremental-builds

Note: The test feature is not discussed in this — will do a demonstration with a sample in my next blog. For now I’m skipping the test implementation and using the default.

Step — 4:

Now we are ready to go with the execution.

Build the builder Image

Execute:

Results:

Testing the builder Image

If we want to check our builder image we can use the below two commands but here the test script kept is the default one and will do a demo on it in my next blog.

Creating the Application Image through S2I

Run the Application

By inspecting the container we can get more information on the image and the application we are executing:

Also to confirm whether our application hosted properly execute the below curl command.

That’s all on this and the s2i code [1]and the sample application code [2]can be found at [1] and [2].

[1] https://github.com/ajanthanblog/msa-ubuntu-builder.git

[2] https://github.com/ajanthanblog/greeting.git

Note: In the git [1] removed the “packs/jdk-8u221-linux-x64.tar.gz” due to the size limit you can download and copy that to that folder while doing the execution.

--

--

Ajanthan Eliyathamby 🇱🇰

Associate Architect — Enterprise Integration | 14x WSO2 | 1x HashiCorp | 1× Azure | Runner-Up WCPY 2020 | https://ajanthane.blogspot.com