Spring Boot application in Docker using Spotify Dockerfile Maven

Spring Boot application in Docker using Spotify Dockerfile Maven
Photo by Anisur Rahman on Unsplash

In this post I will show you how you can use the Spotify Dockerfile Maven plugin to build an image and then launch the container on its basis.

Project

We will use the ready project that was created in this post: Spring Initializer and a simple Web Spring Boot application. If you haven’t seen it before, I suggest you go to this post and create this test application first. Thanks to this you will get a contribution to the rest of this post.

Adding a plugin

First, we need to add the plugin and configure it in the pom.xml file. To do this, just add the following configuration in the <build><plugins> section:

<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>bigdataetl/webdemo-api</repository>
		<tag>latest</tag>
		<buildArgs>
			<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
		</buildArgs>
	</configuration>
</plugin>

Dockerfile

Then create the Dockerfile in the project root.

FROM openjdk:jre-alpine
MAINTAINER Pawel Cieśla <bigdataetlcom@gmail.com>
ENTRYPOINT ["java", "-jar", "/usr/webdemo-api.jar"]
ARG JAR_FILE
ADD target/${JAR_FILE} /usr/webdemo-api.jar

Create the docker image

When we have the plugin configuration ready and the Dockerfile file, all you have to do is run the command below, which will build a package that will be placed in the Docker image and create that image.

mvn clean package

After a while, you’ll see logs in which we can see that the Spotify plugin has been launched and the Docker image has been built.

[INFO] --- dockerfile-maven-plugin:1.4.13:build (default) @ webdemo ---
[INFO] dockerfile: null
[INFO] contextDirectory: /home/pawel/Desktop/Blog/webdemo
[INFO] Building Docker context /home/pawel/Desktop/Blog/webdemo
[INFO] Path(dockerfile): null
[INFO] Path(contextDirectory): /home/pawel/Desktop/Blog/webdemo
[INFO] 
[INFO] Image will be built as bigdataetl/webdemo-api:latest
[INFO] 
[INFO] Step 1/5 : FROM openjdk:jre-alpine
[INFO] 
[INFO] Pulling from library/openjdk
[INFO] Digest: sha256:1bed44170948277881d88481ecbd07317eb7bae385560a9dd597bbfe02782766
[INFO] Status: Image is up to date for openjdk:jre-alpine
[INFO]  ---> ccfb0c83b2fe
[INFO] Step 2/5 : MAINTAINER Pawel Cieśla <bigdataetlcom@gmail.com>
[INFO] 
[INFO]  ---> Using cache
[INFO]  ---> b9bec1cb33fb
[INFO] Step 3/5 : ENTRYPOINT ["java", "-jar", "/usr/webdemo-api.jar"]
[INFO] 
[INFO]  ---> Using cache
[INFO]  ---> 64f9521f8f9e
[INFO] Step 4/5 : ARG JAR_FILE
[INFO] 
[INFO]  ---> Using cache
[INFO]  ---> 48b7a34304cc
[INFO] Step 5/5 : ADD target/${JAR_FILE} /usr/webdemo-api.jar
[INFO] 
[INFO]  ---> 132eae60f938
[INFO] Successfully built 132eae60f938
[INFO] Successfully tagged bigdataetl/webdemo-api:latest
[INFO] 
[INFO] Detected build of image with id 132eae60f938
[INFO] Building jar: /home/pawel/Desktop/Blog/webdemo/target/webdemo-0.0.1-SNAPSHOT-docker-info.jar
[INFO] Successfully built bigdataetl/webdemo-api:latest
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

Let’s run the docker container

Let’s check to make sure our image is visible:

pawel@pawel:~/Desktop/Blog/webdemo$ docker images
REPOSITORY                                         TAG                 IMAGE ID            CREATED             SIZE
bigdataetl/webdemo-api                             latest              132eae60f938        3 minutes ago       101MB

And then we launch the container:

 docker run -p 8080:8080 bigdataetl/webdemo-api

Now we can see the logs that the application is running and then run in the browser: http://localhost:8080/hello to check if application is working.

pawel@pawel:~/Desktop/Blog/webdemo$ docker run -p 8080:8080 bigdataetl/webdemo-api

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.5.RELEASE)

2020-03-02 20:28:55.722  INFO 1 --- [           main] c.bigdataetl.webdemo.WebdemoApplication  : Starting WebdemoApplication v0.0.1-SNAPSHOT on 820e45ae4378 with PID 1 (/usr/webdemo-api.jar started by root in /)
2020-03-02 20:28:55.726  INFO 1 --- [           main] c.bigdataetl.webdemo.WebdemoApplication  : No active profile set, falling back to default profiles: default
2020-03-02 20:28:56.912  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-03-02 20:28:56.930  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-03-02 20:28:56.930  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-03-02 20:28:57.008  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-03-02 20:28:57.009  INFO 1 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1210 ms
2020-03-02 20:28:57.199  INFO 1 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-02 20:28:57.400  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-03-02 20:28:57.404  INFO 1 --- [           main] c.bigdataetl.webdemo.WebdemoApplication  : Started WebdemoApplication in 2.206 seconds (JVM running for 2.999)
2020-03-02 20:29:50.608  INFO 1 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-03-02 20:29:50.608  INFO 1 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-03-02 20:29:50.613  INFO 1 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 5 ms

If you enjoyed this post please add the comment below or share this post on your Facebook, Twitter, LinkedIn or another social media webpage.
Thanks in advanced!

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments