Keerthana Thiagaraj
6 min readMay 8, 2020

Dockerizing Spring Boot Application

We are in the world of developing applications using multiple languages ,frameworks, architectures for each life cycle stage which creates enormous complexity. There is an urge to do container based deployment. Spring Boot and Docker together is a great combo to develop a RESTful webservice application.

Spring Boot+Docker
Spring Boot+Docker

In this article, I will try to explain:

  • Docker and it’s benefits.
  • Creating a Spring Boot Application.
  • Hosting the Spring Boot Application in Docker.

Docker:

Docker is an open-source technology used mostly for developing, shipping and running applications. The brilliance of Docker is that, once you package an application and all its dependencies into a Docker container, you ensure it will run in any environment. A Docker container image is a lightweight, standalone, executable package of software that has everything you need to run an application!!

For example, creation of a Java application requires Java libraries, and when we deploy it on any system or VM, we need to install Java first. But, in a container, everything is kept together and shipped as one package, such as in a Docker container. Read the documentation for more information about Docker containers.

Spring Boot Application:

Spring Boot is a micro service-based framework and making a production-ready application in it takes very less time.

For example, when you list “MySQL” as a dependency, it will configure your Spring application with the “MySQL connector” included.There’s no need to deploy your application to a web server. You simply enter the run command to start the application. Let’s see how to create a sample Spring Boot application.

Open Spring Starter to create a Java Maven application with Spring Starter libraries.

Provide the Artifact Group and Name, and in dependencies, add “Web” and leave everything else with the default, which will create a Maven project with Java and Spring Boot. This will generate a ZIP which is to be imported into IDE as a Maven project.

That is all!💃 Having imported the application, create a sample endpoint to check the functionality of the application.

Post running the application and accessing the end point in the browser, you will be able to see the result as below:

Ideally, the project structure will look like:

Now lets dive into a basic overview of all files:🏃🏼‍♂️

  • ApplianceController: Contains the business logic for all the Rest Endpoints.
  • ApplianceRepository: Responsible for providing CRUD operations on database tables.
  • ApplianceEntity: Specifies that the class is an entity and is mapped to a database table.
  • pom.xml: Contains all the dependencies required for the application.
  • application.properties: Used for configuring web properties and database configurations.
  • Dockerfile: Docker gives the user the capability to create their own Docker images and deploy them in Docker. To create your own Docker image, we have to create our own Dockerfile. Basically, a Dockerfile is a simple text file with all the instructions required to build the image
  • docker-compose.yml: Specifies which services/containers should be deployed and define how the images for the containers are built.
  • schema.sql: Contains the scripts to perform database operation.

Let’s walk through by some of the important classes:

HouseHoldApplication:

This is a Spring Boot main class. A Spring Boot REST application loads through this class. We can also see that this class is created with the annotation @SpringBootApplication .

ApplianceController:

This class is responsible for creating RESTful end points necessary for the application and accessing the database with the help of repository defined separately. The @RequestMapping annotation maps all HTTP operations by default and, in this application, it ensures that HTTP requests to /appliances/ are mapped to the getAllAppliances() method. As we are done with GET call, we need to implement RESTful POST call with the same path.

The POST call enables the user to create an appliance or entry in database. Similarly we need to implement RESTful services for Update, Search, and Delete operations. Refer here.

ApplianceEntity:

This is the Appliance entity. Each entity must have at least two annotations defined: @Entity and @Id.

The @Entity annotation specifies that the class is an entity and is mapped to a database table. The @Table annotation specifies the name of the database table to be used for mapping. The @Id annotation specifies the primary key of an entity and the @GeneratedValue provides for the specification of generation strategies for the values of primary keys.Refer here.

Having written all the business logic,

and framed the endpoints, we need to follow the below steps for creating JAR of the application.

  • mvn clean install
  • mvn clean package

As a result, a JAR is created as below:

After creation of JAR,we need to dockerize the application by defining the below files:

Dockerfile:

  • FROM openjdk:8 means this is a Java application and will require all the Java libraries. So it will pull all the Java-related libraries and add them to the container.
  • COPY copies the JAR generated by the application and add them to the container.
  • CMD specifies that this is a JAR and we need to run this JAR from within Docker.
FROM openjdk:8-jdk-alpine
COPY target/household-0.0.1-SNAPSHOT.jar /app/app.jar
CMD ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app/app.jar"]

docker-compose.yml:

  • It contains keywords postgres and web which defines services for web and database portions of the application.
  • The image keyword is used to specify the image from dockerhub for our postgres and web containers
  • For the database, we are using the ports keyword to mention the ports that need to be exposed for postgres.
  • Finally,we need to specify the environment variables for postgres which are required to run postgres.
version: '3.2'
services:
postgres:
restart: always
container_name: sample_db
image: postgres:10.4
ports:
- '5432:5432'
environment:
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_DB=${POSTGRES_DB}
# APP**
web:
build: .
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/test
expose:
- '8080'
ports:
- '8080:8080'

Now, we have arrived at a stage where we have the following:

  1. Java — Spring Boot application
  2. Dockerfile that will create the image to be run in the Docker container.

To load these up in the Docker container, we have to first create the image and then run that image from the Docker container. We need to run certain commands in the folder that contains the Dockerfile.

This will create our image in Docker and load it up to the container.

We have successfully built the image that is ready to run as follows:

Yesss! The Spring Boot application is up and the server is running on port 8080.

The Spring Boot Application is running from the Docker container.

Here is the repository that contains the code for this article.

Do visit my YouTube channel and subscribe for interesting tech videos!🔄

Keerthana Thiagaraj
Keerthana Thiagaraj

No responses yet