Run a Spring Boot Application on Docker & Kubernetes


In my last tutorial, I explained the procedure about how to run a hello-node application on Kubernetes and covered some of the Core components usages.  In this tutorial, you will learn how to create your own simple web application that enables you to run on Kubernetes using Docker and Spring Boot. The focus of this tutorial is to install and create a Docker container image, develop Spring Boot application using maven tool and enable the orchestration for Spring Boot Docker Container on Kubernetes.

To get started, you will need to set up the following Prerequisite for your development environment.


  • CentOS 7 on VirtualBox
  • Docker Engine
  • Maven tool
  • Java 8
  • Git for Source Control
  • Minikube for Kubernetes
  • Basic knowledge about Linux Commands

In order to understand the entire flow of this article, I have created a diagram below. Hope, This would be easy for learning and follow my guideline.

Basic about Docker and Spring Boot

Docker simplifies the development of creating a container application in a distributed environment. It allows us easily to publish the created container image to Docker repository built on Public or Private environment.

Spring boot makes it easy to create a stand-alone, production-grade Spring based Application that you can easily run on Container and Orchestration Engine. It provides an ability to quickly construct and run a Micro-services in an easy way.

Installing Docker

Let us bring up CentOS 7 on VirtualBox via Vagrant. If you are new to this vagrant environment, you can refer my old posts to understand how to set up VirtualBox on CentOS using Vagrant.

You would be able to see the below sample output if vagrant is successfully brought up the CentOS Linux.

Next, ssh into CentOS 7 Linux and start installing the Docker Engine.

Ensure that your rpm packages are up to date. It is always advisable to run a yum update command before installing the packages. The latest Kernel and Some of the rpm packages updated after running yum update.

Restart the CentOS Linux to apply the latest Kernel.

Let’s install Docker engine after the successful login.

The following dependency installed as part of the Docker Engine.

To avoid a sudo command for a vagrant user, add a vagrant user in docker group. So, Let’s create a docker group and include a vagrant user in it. You need to exit and re-enter into the terminal to apply these changes.

Next step is to start Docker engine using Systemd service and to verify the service that has started, run systemctl status docker command on the terminal.

Checking the status help you to confirm the service started or not.

Enabling the service would allow the Docker engine to keep running even after the restart of the system.

We have successfully installed the docker. Enter docker info command in the terminal to know configuration details about a Docker service.

Installing Java 8 and Maven Tool

To develop Spring Applications, you will need to have the Java development Kit Properly installed on your CentOS Machine. To install Java 8, run the following command.

You would see the below output at the end of the OpenJDK installation.

Now! Java 8 installed on your CentOS machine. Let’s run java -version command to confirm JDK version.

Installing Maven Tool

Spring Boot application code can be compiled or built using any one of the tools such as Gradle, Maven or IDE. Since I am comfortable with the Maven tool, I will be using Maven to build the Spring example in this tutorial. In case, you are comfortable with Grade or IDE to build your spring application, you can refer the link. 

The following command will install maven command.

The display above output mvn -version command confirms that Maven installation completed successfully.  Now! you need a spring boot sample web application source code to build the jar file.  This can be done through spring Initializer or using a Spring Boot Source Code or IDE.  In this tutorial, I am going to use the source code which is available in the GitHub repository.

To pull the source code from the GitHub repository, run the following command in the terminal. Ensure that you have already installed git command in your CentOS Linux. if you are not sure about how to install git command in Linux, you can check my previous blogs. Let’s run git clone command to download the source code from GitHub repository.

Installing Git Command

Git command will help you to pull or push/commit changes on GitHub. For cloning the Repository, we need to install git command. Run the below command in terminal to install the packages.

If the installation is successful, you would see the output at the end of the installation.

Let’s run the git clone command to clone the repository.

Cloning is successful in my case.

Next step is to cd to source (i.e src ) directory and run mvn clean package command to build the jar the file. Ensure you run the command on the below mentioned path.

mvn clean package command starts downloading pom and dependency jar files in order to compile and build the source code for Spring boot application. Ensure that you have enough internet bandwidth before running mvn command.

The build is successful and jar file created under the target folder. If you are interested in knowing how to create a java class for hello world docker, please refer to this link. for the tutorial on setup a spring boot app.

tree command will show the structure of the files placed inside the target folder.

Finally, Let’s run java command to start the spring boot application.

Cool!. Spring boot application has started. You will need to run curl command to verify the application is up and running. By default, spring boot application listens on port 8080. Ensure that there is no port conflicting with any other application. You may need to login into another terminal using vagrant ssh to run curl command to verify the application.

So far, we have completed to set up and run spring boot application using Java and Maven but the spring boot framework itself does not provide a way to run the server in the background. It usually needs to be implemented in the process management tool such as Systemd and Supervisord. Thus, automated deployment will be beneficial for operation and maintenance for containerized the spring boot application using Docker and run an orchestration engine such as Kubernetes on top it will provide highly scalable and reliable application runtime.

Containerizing Spring Boot Application

Docker provides a file in Dockerfile format to build the application image. Now, let’s create the Dockerfile for the spring boot application. Ensure that you have copied the jar file ( which is created under target folder gs-spring-boot-docker-0.1.0.jar) in the same location where Dockerfile created.

We will need to build the docker images now using Dockerfile.  Let’s run docker build command to create an image.

If a build is successful, you can see an alpine image is being pulled and created the images.

Spring Boot Docker Image has successfully created to run spring boot application in Docker Container. You can run docker images command to check spring boot images that created on docker.

Now, run the following docker run command to start the spring boot application.

To verify the spring-boot-app is running on docker, run docker ps command and check. The output confirms that the spring boot application is up and running. let’s run the same curl command to check the spring boot web application.

A container is running based on docker ps command.

Curl command will confirm the application running state.

Pushing Images to Docker Hub

Successfully, Spring boot web application has been containerized in Docker. Now, Spring boot web application created as Docker image. We can reuse this image for external application by pushing the image in Docker hub or Private registry.  I will push this image to docker hub and pull it in Kubernetes later.

Before pushing the docker image to docker hub, you need to create an account in docker hub. Go and create an account and try to push the image.  I have uploaded the spring boot application publicly so you can use the image if you are not interested in creating an account.

Use docker tag command to tag the image version and run docker push command to push the image. Ensure that you have already logged in using docker login before pushing the command.

Pushing the spring boot web application to Docker hub has completed successfully. Now, we need to use this image in Kubernetes to pull and run the spring boot application for enabling the Orchestration.

Running Spring Boot on Kubernetes

Let’s start the Minikube to bring up the single-node Kubernetes cluster. If you are new to Kubernetes and Minikube, you can check my previous blogs to know more about it. One more thing,  We cannot run the Minikube inside the CentOS 7 VirtualBox. So, run the minikube start command in Ubuntu Linux as explained in my earlier post.

Minikube has started the nodes successfully. Let’s create deployment and service object which requires Kubernetes to deploy a spring boot application.

You will need to create a deployment yaml file to create hello spring boot Kubernetes container. Go ahead and use the below yaml file to create the deployment. The key to note here is of image name under Containers section. Ensure that you use the same image name which you pushed in case you are using own docker hub.

Let’s create the Kubernetes deployment and pod using the below command and find out whether deployment and pod are successfully created.

Cool!. Pod has created and is showing running for last 3ms. Next, run a kubectl expose command to expose the Pod to the public internet.

will verify the serivce created by below command.

Finally! run minikube service command to find the url where spring boot application is listening on.

Wow! Hello Docker World is running in Kubernetes.


I hope this article has been helpful for you to get started with Spring boot application running on Docker and Kubernetes. You can use this same procedure to explore on a cloud platform as well.


Leave a Comment

Your email address will not be published. Required fields are marked *

17 − sixteen =