Build Spring Boot Microservice application on Docker - Learn about the benefits of Docker containers for deploying Spring Boot microservices and how to get started with a simple Spring Boot app.
Docker is currently a hot topic in container-based deployment, whereas Spring Boot is the same for microservice development. Both Spring Boot and Docker together form a great combo for developing microservice-based applications. In this article, I will try to explain in very simple words
A Docker container is a tool that makes it very easy to deploy and run an application using containers. A container allows a developer to create an all-in-one package of the developed application with all its dependencies. For example, 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 is a framework that eases the development of web applications. It has a lot of pre-configured modules that eliminate the manual addition of dependencies for developing an application with Spring. This is the sole reason for this being one of the favorites for creating microservices. Let's see how to create a Spring Boot Application in a few minutes.
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 STS as a Maven project.
That’s it! You have just created a Spring Boot application in the workspace. Now, we need to add a simple RestController so we can test the API.
Upon running the application and accessing the endpoint of the API, we will see the output “Simple Spring Boot Application” shown in the browser.
We have successfully created and run the application in the embedded server of the IDE, but now we will deploy it in a Docker container. For this, we have to create a Dockerfile that will contain the steps to be executed by Docker to create an image of this application and run that image from Docker.
You may also like: Spring Boot: Run and Build in Docker
In the pom.XML, we defined that the packaging will be JAR. Let us run the Maven commands to create a JAR file for us.
To do so, first clean up the target folder with
mvn clean (this can also be done from the IDE by running Maven Clean) and
mvn install (this can also be done from IDE by running Maven Install).
This command will create a “dockerdemo.jar” in the target directory of the working directory.
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.
Here is our Dockerfile. Create a simple file in the project folder and add these steps in that file:
These are the four steps for that will create an image of our Java application to be able to run Docker.
Okay, we have two pieces ready:
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.
Now that we have the image ready to run, let’s do that with the following command:
There you go — the Spring Boot application boots 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.
Originally published by Sovan Misra at dzone.com
This article looks at two common options for Dockerizing Spring Boot applications. We will use a simple REST application as a running example.
In this article we learn how to start the Spring Boot microservice project and run it fast with Kubernetes and Docker
In this video, You will learn how to create a Spring Boot + JPA/Hibernate + PostgreSQL RESTful CRUD API Example with a PostgreSQL database to run inside a Docker container and how to auditing, tracking and logging user activity across the application project by using JPA Auditing
In this Microservice configuration with Spring Boot tutorial, I cover some best practices and common patterns for using Spring Boot configuration for microservices including what config mechanism to use where and how to save sensitive config like passwords and connection strings. Microservices configuration Best Practices - Microservice configuration with Spring Boot
Microservices are being developed all around us nowadays. Many of these services are short-lived.