Containerizing a SpringBoot Application using a Jenkins pipeline and publish it to DockerHub.

Edmond Tchamie
6 min readFeb 27, 2023
A sample Jenkins pipeline containerizing a SpringBoot App.

When you are a beginner or carrier transiter from a non IT background to a field which is in vogue, it’s very difficult get in. And that’s what is happening in **Dev & Operations** culture …

What’s The goal ?

In this mini story, I would like to share with someone a mini basic DevOps project where you will be introduced to a CI/CD Tool — Jenkins, the public container Repository (Docker Hub ) , and GitHub (The Code Repository ). By following the project step to step, you will be able to build a sample complete Jenkins pipeline.

What’s the pipeline workflow defined in our mini project ? By looking at the story cover image, the pipeline works like this : In the First phase the developer push / commit his code to GitHub, then in phase 2 by Jenkins magic … 😅 the code is retrieved in the Jenkins server machine for Analysis, Compilation, Testing, etc... In fact the second phase defines a Continuous Integration & Continuous Delivery meaning we provide to Jenkins the credentials and required permissions to access the code from GitHub and then perform a job with the code got. Now come the third phase where we tell Jenkins to build a Docker Image from the code received in the phase 2, so here we wrote a Jenkinsfile (It’s a Jenkins script file containing all tasks or stages to be performed by Jenkins to attend a desired station …). After successfully accomplish the phase 3, we got a Docker Image of our Application from Jenkins, and from there we’re free to launch directly a local docker container to test effectively the working of our Application or to publish the Docker Image of our App to a Central registry Hub. But in this project, as we are the one wrote and tested the code before pushing the code to GitHub, we published the Image got from Jenkins to DockerHub in the phase 4 (Here also we gave to Jenkins permissions & credentials to access our DockerHub account otherwise we won’t go further…). And in the final phase, we launched a Docker container of the App either by pulling again from the Central Hub or by running the local image of the App.

Prerequisites

To elaborate the project , all we need are some basic setups like :

Basic Information about Tech used in this project in case you don’t know

  • GitHub Repo: I bet everyone know what is GitHub, right 🤗 ??? Simply GitHub is cloud based platform for software development , projects collaborations, webpages creation, central user’s code repositories and a Continuous Integration with features like Github Actions, git, VCS, etc…
  • Jenkins: It is CI/CD Continuous Integration & Continuous Delivery Tool used in DevOps culture to automate software development Integration, Building, Testing, Analyzing, and deployment … Know more
  • Docker : Docker is the most used Tool for packaging Software Application with their minimal dependencies in containers. It allow us to have many versions of sample application running and shareable for Testing or micro services building. Know More …
  • DockerHub: It’s simply the central repository for Docker Image publications. Know More…
  • Spring Boot : It is a framework that facilitates the development of applications based on Spring by offering tools allowing to obtain an application packaged in JAR, completely autonomous. This is of particular interest to us, since we are dealing with micro application in container. Know More…

We can’t produce something wonderful without a plan ? right 🥲 … So let’s divide the project in mini steps by which we can follow to complete the project.

  • Step 1: Launch The Jenkins server and Configure a new Pipeline Job.
  • Step 2: Build the Docker Image of the Application with Jenkins help.
  • Step 3: Use Jenkins to push the Docker Image of the Application to DockerHub.
  • Step 4: Use Jenkins to test our App by launching a Docker container of the Application.

Time of Get our Hand Dirty 👾

First Apologize us, because We are unable to put here all the projects steps because it required lots of pics and sub-steps to complete the project…But you can follow directly this link to start building the project.. https://github.com/Tcarters/mini-DevOps-Project_jenkins-springBoot-Docker

Step 1: Launch The Jenkins server and Configure a new Pipeline Job.

  • In this step, we have start our Jenkins machine
  • Install the Maven plugin as we are dealing a java spring App.
  • And then we launch a ``Pipeline`` Job by writing a Jenkins script file.
Pipeline build …
  • Step 2: Build the Docker Image of the Application with Jenkins help.

In this step , we have to create a Dockerfile which contains all stage to package the Application and it’s dependencies in a container. And after we have update our Jenkinsfile with a new stage to build the docker Image of the App.

  • Step 3: Use Jenkins to push the Docker Image of the Application to DockerHub.

Now that Jenkins build the Docker Image for our application, we’ll tell him to push the Image to our public docker Hub registry… And to accomplish it we have just to modify again our pipeline script and That’s it.

  • Step 4: Use Jenkins to test our App by launching a Docker container of the Application.

And finally , we define a new stage in which Jenkins will launch a container running our Application.

The complete Jenkinsfile used is :

Link for access: https://github.com/Tcarters/SpringBootApp_and_DevOps/blob/master/Jenkinsfile

pipeline {
agent any
tools {
maven 'maven_3.6.3'
}
stages {
stage('Build a Maven Project '){
steps{
echo 'Start The checking of github repository'
checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/Tcarters/SpringBootApp_and_DevOps']])
echo 'Cleaning the Project'
sh 'mvn clean install'
}
}
stage ('Build SpringApp Docker Image ') {
steps{
script {
echo 'Checking if docker service is available ...'
sh 'systemctl is-active --quiet docker && echo "Service is running ..."'
echo 'Starting Docker Image building'
sh 'docker build -t tcdocker2021/springbt-in-docker:latest .'
}
}
}
stage ('Pushing the SpringApp Docker Image to Docker Registry') {
steps {
script {
echo 'Logging to Docker registry.....'
withCredentials([string(credentialsId: 'mytcdocker-hub', variable: 'mydockerhubpwd')]) {
sh 'docker login -u tcdocker2021 -p ${mydockerhubpwd}' // some block
}
echo 'Starting the push of Docker Image ....'
sh ' docker push tcdocker2021/springbt-in-docker:latest '
}
}
}
stage ('Testing the deployment') {
steps {
script {
echo 'Starting a local container of the App ....'
sh 'docker run -dit --name springapp -p 2000:8080 tcdocker2021/springbt-in-docker:latest '
echo 'The App is now available at Port 2000 ....'
}
}
}
}
}

Pipeline dashboard View :

Pipeline Result..

Final Application viewed on the Browser ….

Running Application…

Project complete code and Steps link is below:

https://github.com/Tcarters/mini-DevOps-Project_jenkins-springBoot-Docker

ThANKS & PEACE OUT ☺️ 😊

--

--

Edmond Tchamie

CI/CD Tech Explorer / ( DevOps & Cybersecurity Enthusiast ) on Lunar Planet🚀