Spring Boot App In Docker Using Spotify Dockerfile Maven – Make Your Life Easy And Enjoyable In 3 Mins!

Spring Boot: Aplikacja w Docker przy użyciu wtyczki Spotify Dockerfile Maven
Photo by Anisur Rahman on Unsplash
Share this post and Earn Free Points!

In this post I will show you how you can use the Spotify Dockerfile Maven plugin [ Spring Boot app in Docker using Spotify Dockerfile Maven ] to build an image and then launch the container on its basis.

Introduction

Spring Boot

Spring Boot is a popular Java-based framework for building web, standalone, and microservices applications. It is designed to make it easy to create and run production-grade applications, and it provides a range of features and tools to help developers build and deploy applications quickly and efficiently.

One of the main benefits of Spring Boot is that it simplifies the configuration and setup of Java applications. It provides a default configuration for most common application components, such as web servers, databases, and security, and it allows developers to override these defaults as needed. Spring Boot also provides a command-line interface (CLI) that can be used to create and run applications, and it integrates with various build tools, such as Maven and Gradle.

Spring Boot is built on top of the Spring framework, which is a comprehensive framework for building Java applications. It provides a range of modules and libraries for various purposes, including web development, data access, security, and more. Spring Boot makes it easy to use these modules in your applications, and it provides a number of features and tools to help you build and deploy your applications.

To use Spring Boot, you will need to install the Spring Boot dependencies in your project and configure your application To use Spring Boot. You can then use the Spring Boot CLI or your build tool to build and run your application. Spring Boot is widely used in the Java ecosystem, and it is supported by a large and active community of developers.

Docker And Dockerfile

Spring Boot is a popular Java-based framework for building web, standalone, and microservices applications. It is designed to make it easy to create and run production-grade applications, and it provides a range of features and tools to help developers build and deploy applications quickly and efficiently.

One of the main benefits of Spring Boot is that it simplifies the configuration and setup of Java applications. It provides a default configuration for most common application components, such as web servers, databases, and security, and it allows developers to override these defaults as needed. Spring Boot also provides a command-line interface (CLI) that can be used to create and run applications, and it integrates with various build tools, such as Maven and Gradle.

Spring Boot is built on top of the Spring framework, which is a comprehensive framework for building Java applications. It provides a range of modules and libraries for various purposes, including web development, data access, security, and more. Spring Boot makes it easy to use these modules in your applications, and it provides a number of features and tools to help you build and deploy your applications.

To use Spring Boot, you will need to install the Spring Boot dependencies in your project and configure your application To use Spring Boot. You can then use the Spring Boot CLI or your build tool to build and run your application. Spring Boot is widely used in the Java ecosystem, and it is supported by a large and active community of developers.

Spotify Maven Plugin

The Spotify Dockerfile Maven Plugin is a Maven plugin that allows you to build Docker images from a Maven project. It is developed by Spotify and is designed to make it easy to build Docker images as part of a Maven build process.

To use the Spotify Dockerfile Maven Plugin, you will need to add the plugin to your Maven project’s pom.xml file. You will also need to configure the plugin with the necessary settings, such as the location of the Dockerfile and the name of the image.

Once the plugin is configured, you can use the mvn package command to build the Docker image as part of the Maven build process. The plugin will use the Dockerfile to build the image and will push the image to a specified Docker registry.

The Spotify Dockerfile Maven Plugin provides a number of features and options to help you customize and control the build process. For example, you can specify the base image to use, the build context, the entrypoint and command, and the environment variables. You can also use the plugin to build and push multiple images in a single build.

The Spotify Dockerfile Maven Plugin is a useful tool for building and deploying Docker images as part of a Maven build process, and it is widely used in the Java ecosystem. It is compatible with all major Maven versions and is supported by a large and active community of developers.

Main Features Of Spotify Maven Plugin

Some of the main features of the Spotify Maven Plugin include:

  • Dockerfile Maven Plugin: This plugin allows you to build Docker images from a Maven project. It reads a Dockerfile and executes the instructions in the file to build the image, and it can push the image to a specified Docker registry.
  • Maven Wagon SSH: This plugin provides SSH support for the Maven Wagon transport, which allows you to transfer artifacts to and from a remote repository over SSH.
  • Maven Wagon HTTP: This plugin provides HTTP support for the Maven Wagon transport, which allows you to transfer artifacts to and from a remote repository over HTTP.
  • Maven Wagon HTTP Lightweight: This plugin provides a lightweight HTTP implementation for the Maven Wagon transport, which allows you to transfer artifacts to and from a remote repository over HTTP.

Spring Boot App In Docker Using Spotify Dockerfile Maven

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 To Maven Project

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: (Spring Boot app in Docker using Spotify Dockerfile Maven)

<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
Spring Boot app in Docker using Spotify Dockerfile Maven
Spring Boot App In Docker Using Spotify Dockerfile Maven - Make Your Life Easy And Enjoyable In 3 Mins! 4

Spring Boot App In Docker Using Spotify Dockerfile Maven

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. (Spring Boot app in Docker using Spotify Dockerfile Maven)

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. (Spring Boot app in Docker using Spotify Dockerfile Maven)

[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 Spring Boot application is running and then run in the browser: http://localhost:8080/hello to check if application is working (Spring Boot app in Docker using Spotify Dockerfile Maven)

Spring Boot app in Docker using Spotify Dockerfile Maven
Spring Boot App In Docker Using Spotify Dockerfile Maven - Make Your Life Easy And Enjoyable In 3 Mins! 5
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

Summary

The Spotify Maven Plugin is a Maven plugin developed by Spotify that provides a number of useful features and tools for working with Maven projects. It is designed to make it easy to manage and build Maven projects, and it provides a range of features and options to help you customize and control the build process.

That’s all about Spring Boot app in Docker using Spotify Dockerfile Maven. I hope it helped you with your development proces.

Could You Please Share This Post? 
I appreciate It And Thank YOU! :)
Have A Nice Day!

How useful was this post?

Click on a star to rate it!

Average rating 4.9 / 5. Vote count: 1381

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?