Dockerizing a Spring Boot Application
Learn how to deploy Spring Boot application in Docker in easy way!
Join the DZone community and get the full member experience.Join For Free
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 web service application. In this article, I will try to explain:
- Docker and its benefits.
- Creating a Spring Boot Application.
- Hosting the Spring Boot Application in 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, the 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 microservice-based framework and making a production-ready application in it takes very little time. Developers can automatically configure their Spring application. However, Spring Boot is also capable of changing the configuration based on the dependencies you list. 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 endpoint in the browser, you will be able to see the result as below:
The project structure will look like:
A brief 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 each classes:
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. As per the Spring documentation, the annotation @SpringBootApplication is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan, and these annotations are frequently used together. Most of the time, in Spring Boot development, the main class is always annotated with all three of these important annotations.
And we are going to modify the @SpringBootApplication (given below in the Java class) with a component path. Without that, the application cannot find out the controller classes.
This class is responsible for creating RESTful endpoints necessary for the application and accessing the database with the help of a 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 the database. Similarly, we need to implement RESTful services for Update, Search, and Delete operations.
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.
The @Repository annotation is used to define a repository.
This file is responsible for the creation of database entries.
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:
- 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.
- 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.
Now, we have arrived at a stage where we have the following:
- Java – Spring Boot application
- 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.
Please visit my below channel for a detailed explanation and subscribe.
Opinions expressed by DZone contributors are their own.