Dockerize spring boot application with Maven

Hi Folks,

In my previous post, I explained about how to create a docker image from a spring boot application in which we used DockerFile and we run some commands of docker in order to create a docker image.

In today’s post, I will cover about how to create docker image with maven plugins.

Maven provides many plugins for docker to create an image and push it to the repositories. For my post I will use following plugin.

dockerfile-maven-plugin

Let’s start with the implementaion. Just download a sample spring boot application, created in my previous post, and add following configuration in pom.xml

<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>

<contextDirectory>${project.basedir}</contextDirectory>
<!-- replace `username` with your docker id--><repository>${username_of_docker_hub}/${project.artifactId}</repository>
<useMavenSettingsForAuth>true</useMavenSettingsForAuth>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
<executions>
<execution>
<id>default</id>
<phase>install</phase>
<goals>
<goal>build</goal>
<goal>tag</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
</plugin>

Here I used maven dockerfile plugin provided by spotify. This plugin will do following things:

  • Build an image of docker
  • Tag that image with the repository name provided in plugin configuration
  • Push that image to docker hub repository

This plugin has following things to configure:

  • contextDirectory – Base path of the dockerFile.
  • repository – repository name which will be created on docker hub along with the docker hub userId. Here {username_of_docker_hub} is the username of your docker hub account. If you don’t have any account on docker then click here to register on docker hub.
  • tag – tag is used to create image tag. It is kind of version for the image. In this example, It is project version.
  • useMavenSettingsForAuth – It is used to tell whether maven settings.xml file will be used to authenticate docker hub repository or not. In this example, it is true so it will use maven settings.xml file to authenticate docker hub repository account. For more details click here. Before using this parameter there is one thing need to be added in settings.xml of maven as below:
<server>
    <!-- Repository url of docker -->
    <id>docker.io</id>
    <username>{usr_name_of_docker_hub_account}</username>
    <password>{password_of_docker_hub_account}</password>
</server>
  • buildArgs – It is used to specify variables which will be used when it will build image file. In this exmple, JAR_FILE is a variable which is specifying the path of the jar file of the application.
  • execution – In this, specify three goals which will be executed when maven will create a build of the project.
    • build – To build an image file.
    • tag – To tag the image
    • push – To push the image file on docker hub repository.

Everything has been setup for create an image so let’s run it.

Maven provides following commands in order to create image:

  • mvn dockerfile:build – This command will build a docker image of the application.
  • mvn dockerfile:tag – This command will tag the image.
  • mvn dockerfile:push – This command will push the created image on docker hub repository.

If anyone doesn’t want to use above individual commands then just run following command and it will automatically build, tag and push the image on docker repository.

mvn clean install

When run this command it will create image and deploy it on docker repository.

This is all about the basic use of dockerfile plugin of maven. For more detail about it click here and for how to configure it click here.

Download the source code from here.

One thought on “Dockerize spring boot application with Maven

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s