Maven plugin for docker: embedding a Spring Boot application

Posted by {"name"=>"Palash Ray", "email"=>"paawak@gmail.com", "url"=>"https://www.linkedin.com/in/palash-ray/"} on November 01, 2017 · 2 mins read

We will take an example of how to use a Maven plugin for docker to embed a Spring Boot application. The Maven plugin that I am using is:
https://github.com/spotify/dockerfile-maven
These are the basic operations that I do with this plugin:

  1. Given a Dockerfile, I create a docker image by saying mvn dockerfile:build. This builds the docker image from the jar file in my target folder
  2. After building the docker file, I would like to push it to my docker hub repository by using the command mvn dockerfile:push.

This is how I define the docker plugin in my pom.xml:


com.spotify
dockerfile-maven-plugin


default

build
push




docker.io/paawak/spring-boot-demo
${project.version}

Note that I have defined my docker repository details in the repository tag.
This is how the Dockerfile looks like:

FROM java:8
MAINTAINER Palash Ray 
ADD target/spring-boot-demo-0.0.1-SNAPSHOT.jar //
ENTRYPOINT ["java", "-jar", "/spring-boot-demo-0.0.1-SNAPSHOT.jar"]

Providing encrypted credentials to Maven

In addition to the above, I also need to provide my credentials for docker.io. And, of course, the password should be encrypted. Maven supports encryption. I have largely followed the below link:
https://maven.apache.org/guides/mini/guide-encryption.html

Step 1: Create a master password

Create a master password by typing:

mvn --encrypt-master-password

Say, it gives you the below output:

{jSMOWnoPFgsHVpMvz5VrIt5kRbzGpI8u+9EF1iFQyJQ=}

Create a file: ~/.m2/settings-security.xml with the following content:


{jSMOWnoPFgsHVpMvz5VrIt5kRbzGpI8u+9EF1iFQyJQ=}

Step 2: Create an encrypted password

Create the encrypted password for your docker.io user. Type:

mvn --encrypt-password

Say, the output is:

{COQLCE6DU6GtcS5P=}

In the ~/.m2/settings.xml file, add the below lines under the servers tag.


docker.io
paawak
{COQLCE6DU6GtcS5P=}

Now, you should be all set. Use the below command to build the docker image and then push it:

mvn clean install dockerfile:build dockerfile:push

Sources

A working example of this can be found here:
https://github.com/paawak/blog/tree/master/code/spring-boot-demo