How To Dockerize Spring Boot Application
- Anand Nerurkar
- Jul 14, 2022
- 1 min read
How To Dockerize Sample Spring Boot Application-Microservices
Prerequisite
· JDK 11
· DockerHub account and repository
· Docker installed and running
· User must be login to Docker Desktop
· sample microservice – department-service
1. Create a Docker file for the application as below and save it as Dockerfile
--------------------Dockerfile--------------------------------------------
FROM openjdk:11
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} department-service.jar
ENTRYPOINT ["java","-jar","/department-service.jar"]
EXPOSE 9001
2. Edit pom.xml file and make entry for spotify docker plugin to build image and push
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.13</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>anandn76/${project.name}</repository>
<tag>${project.version}</tag>
<useMavenSettingsForAuth>true</useMavenSettingsForAuth>
</configuration>
<dependencies>
<!-- To make this work on JDK 9+ -->
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
</plugin>
Yellow block represent docker goals such as build and push
Green part represent your docker hub repository
For below block we need to make below entry in settings.xml file
<useMavenSettingsForAuth>true</useMavenSettingsForAuth>

3. Select your project->maven-> maven build and configure target as below

Click Run. It will build project, build image and push it to your docker hub repository as below.

Comments