Deploying Spring Boot on Docker

Docker is currently a hot cake in the container based deployment areas where as Spring Boot is the same for Mircoservice development. Both Spring Boot and Docker together forms a great combo for developing microservice based application(s). In this article I will try to explain in very simple words

  • What is Docker and its benefits.
  •  What is Spring Boot Application and how to create a simple Spring Boot Application.
  • Hosting the Spring Boot Application in a Docker Container.

DOCKER CONTAINER :

This is a tool that makes it very easy to deploy and run an application by using containers. A container allow a developer to create a all-in-one package of the developed application with all its dependancies. For example, a java application requires java libraries and when we deploy it in any system or VM, we need to install Java in that as first. But in a container everything is kept together and shipped as one package. Docker container. Read this article for more information about Docker Containers.

SPRING BOOT APPLICATION :

Spring Boot is a framework that ease the development of web applications. It has a lot of pre-configured modules that eliminates the manual addition of dependancies for developing an application with Spring. This is the sole reason of this being one of the favourites creating MicroServices. Lets see now how to create a Spring Boot Application in few minutes.

Open Spring Starter  to create a Java Maven Application with Spring Starter Libraries.

Screen Shot 2018-08-20 at 2.56.59 PM

Provide the Artifact Group & Name and in dependancies add “Web” and leave everything else with default which would create a Maven project with java & Spring Boot. This will generate a ZIP which is to be imported into STS as a Maven Project.

Screen Shot 2018-08-20 at 3.08.38 PM

That’s it!! You have just created a Spring Boot Application in the workspace. Now, we would need to add a simple RestController so we can test the API.

Screen Shot 2018-08-20 at 3.12.46 PM.png

Upon running the application and accessing the endpoint of the API we will see the output “Simple Spring Boot Application” will be shown in the browser.

Screen Shot 2018-08-20 at 3.20.32 PM

We have successfully created and run the application in the embedded server of the IDE, but now we deploy the same in the Docker Container. For this we would have to create a Docker File that will contain the steps that will executed by Docker to create an image of this application and will be running that image from docker.

JAR file of this application:

As in the POX.XML we have defined that the packaging will be of type JAR, let us run the maven commands to create a JAR file for us.
Screen Shot 2018-08-20 at 3.40.41 PM

To do so, first clean up the target folder.

mvn clean       [This can also be done from IDE, Run as Maven Clean]
mvn install      [This can also be done from IDE, Run as Maven Install]
These command will create a “dockerdemo.jar” in the target directory of the working directory.

Screen Shot 2018-08-20 at 3.43.03 PM

What is a Docker File?

Docker gives the user the capability of creating there own docker images and deploy the same in the docker. To create your own docker image we have to create out own docker file.  Basically a  Docker File is a simple text file with all the instructions required to build the image.

Here is our Docker File :
Create a simple File in the project folder and add these steps in that file.

Screen Shot 2018-08-20 at 3.27.39 PM.png

FROM java:8
This line means this is a Java Application and will require all the Java Libraries so it will pull all the java related libraries and add to the container,

EXPOSE 8080
This means that we would like to expose 8080 to the outside world to access our application.

ADD /target/dockerdemo.jar dockerdemo.jar
ADD <source from where docker should create the image> <destination>

ENTRYPOINT [“java”, “-jar”, “dockerdemo.jar”]
This will run the command as the entry point. As this is a JAR and we need to run this Jar from within the docker.

These are the four steps for that will create an image of our Java Application to be able to run the docker.

Okay!! We have two pieces ready…

  1.  Java – Spring Boot Application
  2. DockerFile that will create the Image to be run in the Docker Container.

For loading 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.

Screen Shot 2018-08-20 at 3.56.29 PM.png
This will create our image in the docker and loads up to the container.

Screen Shot 2018-08-20 at 4.00.43 PM

Now that we have the image ready to run… let’s do that with the following command…

Screen Shot 2018-08-20 at 4.06.44 PM.png There you go.. Spring Boot Application Boots up.. and the server is running on the port (8080).

Screen Shot 2018-08-20 at 4.28.34 PM

Here we go…. The spring boot application is running from Docker Container 🙂

Hope this will help get started with Spring Boot Application and Docker Container Deployment

Happy Coding!
Sovan

Leave a comment