This is a Maven plugin helps Maven integrate with Docker.

  • Make the Docker build process integrate with the Maven build process. If you bind the default phases, when you type mvn package, you get a Docker image. When you type mvn deploy, your image gets pushed.
  • You can type mvn dockerfile:build and later mvn dockerfile:tagand later mvn dockerfile:push without problems. This also eliminates the need for something like mvn dockerfile:build -DalsoPush; instead you can just say mvn dockerfile:build dockerfile:push.
  • This is useful when you want to run integration tests involving multiple services.

This configures the actual plugin to build your image with mvn package and push it with mvn deploy. Of course you can also say mvn dockerfile:build explicitly.


    <plugin>
        <groupId>com.spotify</groupId>
        <artifactId>dockerfile-maven-plugin</artifactId>
        <version>1.4.9</version>
        <configuration>
            <!-- replace `mydockerreddah` with your docker id-->
            <repository>mydockerreddah/${project.artifactId}</repository>
            <tag>${project.version}</tag>
            <tag>latest</tag>
            <buildArgs>
                <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
            </buildArgs>
            <serverId>docker-hub</serverId>
            <registryUrl>https://index.docker.io/v1/</registryUrl>
            <useMavenSettingsForAuth>true</useMavenSettingsForAuth>
        </configuration>	
        <executions>
            <execution>
                <id>default</id>
                <goals>
                    <goal>build</goal>
                    <goal>push</goal>
                </goals>
            </execution>
            <execution>
                <id>tag-image</id>
                <phase>deploy</phase>
                <goals>
                    <goal>tag</goal>
                </goals>
                <configuration>
                    <repository>mydockerreddah/${project.artifactId}</repository>
                </configuration>
            </execution>
        </executions>	
    </plugin>

A corresponding Dockerfile could look like:

FROM openjdk:8-jre
MAINTAINER Reddah Cherara <cherara.reddah@icloud.com>

ENTRYPOINT ["/usr/bin/java", "-jar", "/usr/share/myservice/myservice.jar"]

# Add Maven dependencies (not shaded into the artifact; Docker-cached)
ADD target/lib           /usr/share/myservice/lib
# Add the service itself
ARG JAR_FILE
ADD target/${JAR_FILE} /usr/share/myservice/myservice.jar

You no longer have to say something like:

mvn package
mvn dockerfile:build
mvn verify
mvn dockerfile:push
mvn deploy

Instead, it is simply enough to say:

mvn deploy

With the basic configuration, this will make sure that the image is built and pushed at the correct times.

You have to created a serverId in your ~/.m2/settings.xml file?
Like here:

<servers>
  <server>
    <id>docker-hub</id>
    <username>your_docker_hub_username</username>
    <password>you_docker_hub_password</password>
    <configuration>
      <email>your_docker_hub_email@example.com</email>
    </configuration>
  </server>
</servers>

You then need to refer to it in your pom.xml (serverId).

For the Encryption of your Docker Hub Password use this link :  

Generate a temporary BCrypt hash (Find a generator online. I used this one: https://passwordhashing.com/BCrypt). Or

If you want to encrypt that password, please follow this mini guide --> https://maven.apache.org/guides/mini/guide-encryption.html

https://maven.apache.org/guides/mini/guide-encryption.html

curl -s -L http://registry.hub.docker.com/v1/repositories/mydockerreddah

Depend on Docker images of other services :

You can depend on the Docker information of another project, because this plugin attaches project metadata when it builds Docker images. Simply add this information to any project:

<dependency>
  <groupId>com.spotify</groupId>
  <artifactId>foobar</artifactId>
  <version>1.0-SNAPSHOT</version>
  <type>docker-info</type>
</dependency>

Now, you can read information about the Docker image of the project that you depended on:

String imageName = getResource("META-INF/docker/com.spotify/foobar/image-name");

This is great for an integration test where you want the latest version of another project's Docker image.

Note that you have to register a Maven extension in your POM (or a parent POM) in order for the docker-info type to be supported:

<build>
  <extensions>
    <extension>
      <groupId>com.spotify</groupId>
      <artifactId>dockerfile-maven-extension</artifactId>
      <version>${version}</version>
    </extension>
  </extensions>
</build>

Or Use other Docker tools that rely on Dockerfiles with Fig or docker-compose or some other system that works with Dockerfiles. For example, a docker-compose.yml