Build docker images with maven and docker command line

Mradul Pandey
Jinternals
Published in
1 min readSep 15, 2019

Containerisation of applications became an essential part of most software projects, as it helps in bundling the application with an environment that provides consistent deployment in different environments (dev/prod). Containerized applications need to be built with specific application version and commit information. In this blog we will use maven to filter Dockerfile with application version and commit information:

  1. Create docker file template with maven variables in src/main/docker folder:
FROM java:8-jre
MAINTAINER Mradul Pandey <pandeymradul@gmail.com>

LABEL
VERSION="@project.version@"
LABEL
DESCRIPTION="@project.description@"
LABEL
GIT_COMMIT_ID="@git.commit.id@"

ADD
./@project.build.finalName@.jar /app/
CMD
["java", "-Xmx200m", "-jar", "/app/@project.build.finalName@.jar"]

EXPOSE 8080

2. Update pom.xml to filter Dockerfile and git commit id plugin:

<?xml version="1.0" encoding="UTF-8"?>
<project ... >
...
<build>
<resources>
<resource>
<directory>src/main/docker</directory>
<filtering>true</filtering>
<includes>
<include>Dockerfile</include>
</includes>
<targetPath>${project.build.directory}/docker-resources</targetPath>
</resource>
</resources>

<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

3. Build application:

mvn clean install

4. Build Docker image:

docker build -t docker-image:latest -f target/docker-resources/Dockerfile target/

Working code is available in the following Github repository: https://github.com/jinternals/docker-image

--

--