How to Dockerize Spring Boot Applications

Overview:

In this article, I am going to discuss an interesting part of your newly created application, i.e. how to dockerize spring boot applications. Before jumping into the solution, let’s have a brief idea – why we should dockerize an application ?

Why Docker ?

Well, There is huge list of pros. (also cons.), but for me the advantage is, any Operating System which can run docker, will be able to run your application. If you are new to Docker, I will suggest you to have quick read of this article here.

The Requirements:

Spring Boot 2 Application :

I believe you have a Spring Boot 2 Application with you to make a docker image of it. If you don’t, please have a look into one of my article here on Creating a basic Spring Boot 2 Application.

Docker Registry :

You must have a docker registry where you can store your docker images and maintain those by providing proper tags.

If you don’t have any docker registry, you can create free at Canister. Just go ahead and create an account.

The Dependency:

Now, it is time to add the dependency to your pom.xml file. Well, there are multiple plugins to create docker from maven projects but I liked the one created by Spotify. I found it easy to configure and run. So, here it is – Just use the below code snippet in your <build> part and replace with your docker registry username, password and repository url for dockers.

<build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
   <plugin>
    <groupId>com.spotify</groupId>
    <artifactId>dockerfile-maven-plugin</artifactId>
    <version>${dockerfile-maven-version}</version>
    <executions>
     <execution>
      <id>default</id>
      <goals>
       <goal>build</goal>
       <goal>push</goal>
      </goals>
     </execution>
    </executions>
    <configuration>
     <username>############</username>
     <password>############</password>
     <repository>################</repository>
     <tag>${project.version}</tag>
     <buildArgs>
      <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
     </buildArgs>
    </configuration>
   </plugin>
  </plugins>
 </build>

You’re almost done. Just one step left which is to create the docker file.

The Dockerfile:

This docker file will be used by the dockerfile-maven-plugin during the build phase. You need to create a file named “Dockerfile” at the same level of your pom.xml. The content of this file should look like below –

FROM openjdk:8-jre
ARG JAR_FILE
ADD target/${JAR_FILE} /opt/<YOUR_PROJECT_NAME>/<YOUR_ARTIFACT_NAME>.jar
ENTRYPOINT ["java", "-jar", "/opt/<YOUR_PROJECT_NAME>/<YOUR_ARTIFACT_NAME>.jar"]

Steps During the Docker Build Phase:

  • The plugin will use OpenJDK 8 JRE as the base of your container.
  • The ARG JAR_FILE will extract the project name from the pom.xml because in the plugin configuration you have mentioned it.
  • Now, it will put the jar from the target folder to its destination of your container. (Don’t miss the <space> between “target/${JAR_FILE}” and “/opt/<YOUR_PROJECT_NAME>/<YOUR_ARTIFACT_NAME>.jar”)
  • It creates an ENTRYPOINT which means, when this docker will start running, it will execute the java -jar command.
  • Finally when the docker build is completed, it will push the image to your remote docker registry.

Conclusion:

I hope you have successfully pushed your docker image to your docker registry and you liked my post on how to dockerize spring boot applications. If you are facing any problem, please don’t hesitate to leave a message below.

Check out my other articles on Java and Spring.

Parijat Mukherjee

Hi There !!! I am a Software Engineer and I am always in a process of learning something new. I love the works that are outside my comfort zone. Rather than focusing on a particular programming language, I try to focus on the design patterns and technologies because I believe, if someone understands the "What" and the "Why" part of a problem and the technology or tool, then the "How" part can be easily done by any of the programming language available in the market.

Recent Posts

Spring Security Architecture – Authentication

The Spring Security project is perhaps the biggest and one of the best projects so far by Spring. When I…

2 weeks ago

Make Use of SOLID Coding Principles

We all know what are the pros and cons of Object Oriented Programming, but what about writing better codes? What…

2 weeks ago

Free Birthday and Anniversary Mail Sender Software

Open Source Application to send Birthday and Anniversary greetings to the employees

1 month ago

Create a Spring Boot 2 Application & Register it in Eureka

Spring boot tutorial and a eureka client example including eureka client properties to guide the microservice beginners.

3 months ago

Create Eureka Server Using Spring Cloud

Quick beginners guide to Eureka Server using Spring Cloud with all the necessary eureka server dependency maven dependencies and eureka…

3 months ago

How to create MariaDB Galera Cluster in CentOS 6.x

Galera cluster tutorial and overview for the beginners followed by galera cluster setup centos 6 and galera cluster configuration.

2 years ago