DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Yuqing Ma
Yuqing Ma

Posted on

Four Ways to Create Docker Image for Java SpringBoot Project

In this lecture, we will discuss how to build up Docker Image for the SpringBoot project in 4 different ways.
(1) use spring-boot-maven-plugin built-in Build-Image (without Dockerfile)
(2) use Jib-Maven-Plugin (without Dockerfile)
(3) use dockerfle-maven-plugin (with Dockerfile)
(4) standard docerfile
Prepare:
Code:
https://github.com/davelms/medium-articles/tree/master/spring-maven-docker
make sure the following command can run spring boot Project
First, use spring-boot-maven-plugin built-in Build-Image
SpringBoot preinstalls the plugin for building the Docker image. We don’t need to do any modifications, we need to make sure that it is in the poxm.xml file (spring-boot-starter-parent)
All the setup is default setup and you don’t need to do any modification also.
Run the following command in the Spring Boot Root directory.
mvn spring-boot:build-image
Once you see Successfully built image, you can type:
_docker images
_For verifying if the image works with the name of the jar file
_docker run -p 9000:8080 -t name:version
_Then goes to localhost:9000 to test the run

Second, Jib-Maven-Plugin
You only need to run the following command
mvn compile com.google.cloud.tools:jib-maven-plugin:2.3.0:dockerBuild
~mvn compile
com.google.cloud.tools:jib-maven-plugin:2.3.0
use Jib plugin
once success you can see
Built image to Docker daemon as …

Third
We need a Dockerfile in the root directory.
FROM adoptopenjdk/openjdk11:alpine
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
VOLUME /tmp
ARG JAR_FILE
ADD ${JAR_FILE} /app/app.jar
EXPOSE 8080
ENTRYPOINT [β€œjava”,”-Djava.security.egd=file:/dev/./urandom”,”-jar”,”/app/app.jar”]
In pom.xml file, you need to add tags and some configurations.

Image description

Run command MVN PACKAGE

Fourth,with docker build command for docker images
With installed java 11 and maven, the following command can run
mvn spring-boot:run
Spring Boot framework
The docker file as followΒ :
FROM adoptopenjdk/openjdk11:alpine-jre
FROM maven:3.6.3 AS maven
LABEL MAINTAINER=”anyname@gmail.com”
WORKDIR /usr/src/app
COPYΒ . /usr/src/app
RUN mvn package
CMD [β€œmvn”,”spring-boot:run”]

Build the container image
docker build -t nametheimageΒ .
You can check the image in docker Images.
Done

Top comments (0)

Let's Get Wacky


Use any Linode offering to create something unique or silly in the DEV x Linode Hackathon 2022 and win the Wacky Wildcard category

β†’ Join the Hackathon <-