Deploy your Spring Boot applications on OpenShift

[Updated!] Spring Boot is a new paradigm for developing Spring applications with more agility while focusing on your business methods rather than the thought of configuring Spring itself. In this tutorial we will learn how to deploy a Spring Boot application on Openshift Container Platform.

First of all, we will need a Spring Boot application to be deployed. We can use for this purpose the following application available on Github: https://github.com/fmarchioni/masterspringboot/tree/master/demo-docker

For more details on how this application has been created, please check How to deploy Spring Boot applications on Docker

Now start your Openshift Container Platform and login:

$ oc login 

We will create a project named myproject:

$ oc new-project myproject

Now in order to build our project we will use the redhat-openjdk-18 builder image which takes your application source or binary artifacts, builds the source using Maven, and assembles the artifacts with any required dependencies to create a new, ready-to-run image containing your Java application. This resulting image can be run on Openshift Container Platform Online or run directly with Docker.

Therefore, create a new application named “springboot-demo-docker”, which uses this image, available at the github repository we have mentioned at the beginning of this tutorial:

$ oc new-app registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift~https://github.com/fmarchioni/masterspringboot.git --context-dir=demo-docker --name=springboot-demo-docker

Please note that the context-dir has been used to create the application. This option allows us to build a project which is not in the root folder of your Git repository

Check your output, which will inform you that the Service is being created:

Found container image 74c8511 (13 days old) from registry.access.redhat.com for "registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift"

    Java Applications 
    ----------------- 
    Platform for building and running plain Java applications (fat-jar and flat classpath)

    Tags: builder, java

    * An image stream tag will be created as "openjdk18-openshift:latest" that will track the source image
    * A source build using source code from https://github.com/fmarchioni/masterspringboot.git will be created
      * The resulting image will be pushed to image stream tag "springboot-demo-docker:latest"
      * Every time "openjdk18-openshift:latest" changes a new build will be triggered
    * This image will be deployed in deployment config "springboot-demo-docker"
    * Ports 8080/tcp, 8443/tcp, 8778/tcp will be load balanced by service "springboot-demo-docker"
      * Other containers can access this service through the hostname "springboot-demo-docker"

-- Creating resources ...
    imagestream.image.openshift.io "openjdk18-openshift" created
    imagestream.image.openshift.io "springboot-demo-docker" created
    buildconfig.build.openshift.io "springboot-demo-docker" created
    deploymentconfig.apps.openshift.io "springboot-demo-docker" created
    service "springboot-demo-docker" created
-- Success
    Build scheduled, use 'oc logs -f bc/springboot-demo-docker' to track its progress.
    Application is not exposed. You can expose services to the outside world by executing one or more of the commands below:
     'oc expose svc/springboot-demo-docker' .

Let’s check that the service has been created:

$ oc get services
NAME                     CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
springboot-demo-docker   172.30.54.29   <none>        8080/TCP   16m

Last thing will be exposing our service externally:

$ oc expose svc/springboot-demo-docker
route "springboot-demo-docker" exposed

Here’s your nice pod running on the Openshift Container Platform web console:

spring boot openshift tutorial spring boot openshift tutorial

You can click on it to check it’s working: nothing fancy but it does work:

spring boot tutorial openshift

Cool! We just deployed a sample Spring Boot application on Openshift Container Platform!

Another way to deploy Spring Boot applications (and generally speaking Java applications) on Openshift Container Platform is by using the OpenShift Maven plugin. You can check this tutorial for more details.

Do you want some more Spring stuff ? check Spring Boot Tutorials !

Found the article helpful? if so please follow us on Socials