Deploying a Spring Boot App in Docker Hub

Soumya Gupta
Geek Culture
Published in
6 min readFeb 5, 2022

Since the inception of the Spring Boot framework, the creation of a Web-App is a lot simplified. As their official homepage says “Spring Boot makes it easy to create stand-alone”, production-grade Spring based Applications that you can “just run”

Today we are going to create a basic Spring Boot App using Eclipse and then will go all the way to write a Dockerfile, create a docker image and then push the same to the docker hub.

Let’s follow the below steps for the above process:

Step1) Install Java and Eclipse IDE along with STS(Spring Tool Suite) plugin in Eclipse

STS Plugin in Eclipse Marketplace

Once the above plugin gets installed, restart your eclipse and you are ready to create a basic Spring Boot Hello App

Step2) Within Eclipse let's create a Spring starter project

New Project -> Spring starter project

Starter Project in Spring

Let’s change a few fields on the next screen and we’ll be ready,

name = demo, java-version = 8 and I kept version as latest.

Keep everything else as default and you are ready to go

Step3) Now let’s create a Controller class that will be used for a basic rest call of Hello World and add spring-boot-web dependency in your POM.xml,

If you are new to POM and Maven, POM is an acronym for Project Object Model. The pom. xml file contains information of project and configuration information for the maven to build the project such as dependencies, build directory, source directory, test source directory, plugin, goals, etc. Maven reads the pom. xml file then executes the goal.

We need to add the below dependency in our POM.xml


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.3</version>
</dependency>

Now we’ll create the Controller class, which will be responsible for greeting the user with a “Hello” once the user hits our API

package com.example.demo.controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController { @GetMapping("/greeting") public String greetConsumer() { return "Hello from a basic app.";
}
}

Here we’ve added “@RestController”, this annotation is applied to a class to mark it as a request handler, and Spring will do the building and provide the RESTful web service at runtime. Also “@GetMapping” annotation is used for mapping HTTP GET requests onto specific handler methods. (“/greeting”)

Step4) Let’s start our application via the main class(created via Spring Boot started template), run the main method as Spring Boot App, the app will run automatically in the embedded tomcat server

Main Application
Spring app Started

Step5) Let’s test our app in the browser, hit the below URL on your browser, and voila, we received our welcome msg.

https://localhost:8080/greeting

This concludes the creation of our basic Spring Boot App, let’s now dive into the deployment of this app on Docker

Deployment on Docker

Docker is an open-source containerization platform. It enables developers to package applications into containers — standardized executable components combining application source code with the operating system (OS) libraries and dependencies required to run that code in any environment. Docker makes our life as a developer very easy.

We’ll be converting our app into a docker image and then publishing the same onto the docker hub, which is similar to a marketplace for all the images.

Step1) Package your application as a Jar file

Within Eclipse right-click on your project and select RunAs → Maven Build, this will create the required jar of your project, which can be further used for deployment on any platform. If Maven asks for a goal, provide clean and package as below image

Post completion of the above step, your demo-latest.jar should be available inside the target folder within your project directory.

Step2) Create a docker image of your app, follow the below steps for the same

a) Install docker desktop on windows

b) Sign-In to docker hub, if you don’t have an account, create one. This is required to publish your images on the Docker hub.

c) Create the Dockerfile

Create one Dockerfile in your project directory and add the below contents, Dockerfile is a simple file with the name “Dockerfile” without any extensions.

FROM openjdk:8-jdk-alpineEXPOSE 8080ADD target/demo-latest.jar demo.jarENTRYPOINT ["java","-jar","/demo.jar"]

Here we are using OpenJDK 8, exposing our app on 8080 port, picking our app from the target folder, and then providing ENTRYPOINT for run commands

d) Open CMD in your project directory and build the image using the below docker commands, here soumyansh is my docker hub username, change it with your username.

docker build -t soumyansh/demo.jar:latest -f Dockerfile .

e) Verify your image listing in the docker desktop dashboard

Step3) Push the docker image to the docker hub

Login to docker hub, using the docker login command on CMD, provide credentials, if you are already logged into docker desktop, you should get automatically authenticated

After this step, fire the below commands to push the image on the Docker hub, change the username according to your docker hub profile.

docker push soumyansh/demo.jar

Step4) Congrats on completing steps till here, now you have pushed your app on docker hub. Verify the same on the Dockerhub website under repositories

Step5) Now you can pull your image anytime on-demand from the docker hub and run it on your OS, please refer to the below commands for pulling and running your image from Dockerhub directly. Exposing our app to port 8080

docker run -p 8080:8080 soumyansh/demo.jar

This will automatically start our app on port 8080, and now you can hit the below URL in your favorite browser

https://localhost:8080/greeting

Congrats once again on the completion of all the above steps here.

Thank you for reading till here. In this post, we learned how to design a basic Web-based Rest-API using Spring-boot and also deployed the same on docker.

I hope you found this tutorial useful. I’m curious about what you think so hit me with some comments. You can also get in touch with me directly through email or connect with me on LinkedIn.

--

--