Over a million developers have joined DZone.

Deploying Spring Boot Microservices on Docker

DZone 's Guide to

Deploying Spring Boot Microservices 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.

· Microservices Zone ·
Free Resource

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

  • What is Docker and what are its benefits?
  •  What Spring Boot is and how to create a simple Spring Boot application.
  • Hosting the Spring Boot application in a Docker container.

Docker Containers

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 Applications

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.

Screen Shot 2018-08-20 at 2.56.59 PM

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.

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 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” 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 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.

JAR File of This Application

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.
Screen Shot 2018-08-20 at 3.40.41 PM

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.

Screen Shot 2018-08-20 at 3.43.03 PM

What Is a 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.

Here is our Dockerfile. 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

  1. FROM java:8 means this is a Java application and will require all the Java librariesk so it will pull all the Java-related libraries and add them to the container.

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

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

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

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:

  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.

Screen Shot 2018-08-20 at 3.56.29 PM.png
This will create our image in Docker and load it 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 — the Spring Boot application boots up and the server is running on port 8080.

Screen Shot 2018-08-20 at 4.28.34 PM

The Spring Boot Application is running from the Docker container.

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

docker ,spring boot ,microservices ,tutorial ,containers

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}