Dockerizing a SpringBoot Application (Kotlin and Gradle) and Deploying it to your Local Kubernetes Cluster

Let’s look at what is docker and why do we need it.

An application works in developer’s laptop but not in testing or production.

This is due to difference in computing environments.

Developing an application requires starting several of microservices in one machine. So if you are starting five of those services you require five Virtual Machine’s on that host machine.

Using VM’s leads to wastage of resources.

Docker containers are light weight alternatives of VM’s. In docker container we don’t have to pre-allocate any RAM or disk space, they take RAM or disk space according to requirement and provide a consistent computing environment throughout the whole SDLC(Software Development Life Cycle).

Docker in a Nutshell: -

  1. Docker file builds a Docker image and that image contains all the project’s source code.
  2. You can run that image to create as many Docker containers as you want.
  3. Then this image be can uploaded on Docker hub, from docker hub anyone can pull the image and build a container.

Let’s take a look at Kubernetes now: -

Kubernetes is all about managing containers on virtual machines or nodes. The nodes in the containers they run are grouped together as a cluster , and each container has endpoints, DNS, storage and scalability.

In short Kubernetes is a Container Management Tool!.

Kubernetes is an opensource container management tool which automates container deployment, container (de)scaling and container load balancing.

So Docker is a containerization platform and Kubernetes is a container management tool.

Please note: Kubernetes is NOT for application with a simple architecture.

Let’s now try hands on Dockerizing a Simple Springboot Application using kotlin as the language and gradle as the build tool and deploying it to your local kubernetes cluster.

  1. Install Docker Desktop

2. Launch Docker Desktop , Go to setting -> Kubernetes -> select the checkbox Enable Kubernetes and it will download it automatically.

3. After doing the setup upon starting Docker Desktop you’ll see Docker Desktop and Kubernetes running green icon in the application.

Now let’s create a Simple Spring boot application using kotlin and gradle.

  1. Go to Spring Initializr

2. Select Gradle Project, Kotlin as the Language and add the following dependencies: SpringBoot DevTools, Spring Web, SpringBoot Actuator.

3. Generate the jar file, Download it and extract it.

4. Open it with Intellij IDEA.

5. The build.gradle.kts file looks something like this

6. Now let’s create a controller. Open the directory src/main/kotlin/com.example.projectName and create a new package and name it as ‘Controller’. Create a new file inside the Controller and name it as HelloController.kts.

As you can see I have created 2 endpoints here.

7. Now open the terminal and run the command

Gradlew build

This will generate the build files and also the jar.

To see if the jar is working properly run the following command in the terminal : ‘java -jar nameOftheJar.jar’ and check if the endpoints are giving the correct output.

8. Now let’s create the Docker files: Dockerfile and deployment.yml

9. Open the terminal and run the following commands:

docker build -t spring-app-demo .

docker run -d -p 5000:5000 — restart=always — name registry registry:2

docker ps

this will give list of all running images and here you’ll be able to see image registry:2 running.

docker tag spring-app-demo localhost:5000/spring-app-demo

docker tag spring-app-demo localhost:5000/spring-app-demo

docker push localhost:5000/spring-app-demo

kubectl apply -f deployment.yml

kubectl get pods

You’ll get the output as the name of the image, it’s status and age.

Run the command: kubectl describe nameOftheImageFromAbove

Eg: kubectl describe spring-app-demo-77b4b794b5-kg2zf

To get the full Description of the running Image.

Now if everything is working fine, You can simple go the nodePort given in the deployment.yml file to access your endpoints.

Eg: localhost:32000/helloworld

to get your desired output.

Get the whole source code from my git repo:

https://github.com/vedant-tiwari98/SampleSpringbootApp

I am a Software Development Engineer. Working towards becoming a Full Stack Polyglot Developer by building awesome projects.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store