Using Maven to build Docker images

Since the projects already use Maven as the dependency management and build tool, it makes sense to use a Maven plugin that can be used to build Docker images. For this purpose, Spotify's open source dockerfile-maven plugin is used in both the spring-boot-2-taxi-service and spring-boot-2-taxi-booking-service projects as follows:

<plugins>
...
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>${dockerfile-maven.version}</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<contextDirectory>${project.build.directory}</contextDirectory>
<repository>packtpub-spring-boot-2/${project.artifactId}
</repository>
<tag>${project.version}</tag>
</configuration>
</plugin>
</plugins>

This is with the Dockerfile, which is placed inside /src/resources/docker/Dockefile:

FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD PROJECT_JAR app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -jar /app.jar" ]

A Docker image can be built by issuing the following usual Maven build command while being inside the spring-boot-2-taxi project:

$ mvn clean install 

The preceding Dockerfile does the following code:

  • Downloads alpine-oraclejdk8 and uses it to run the Spring Boot app
  • Goes into the /tmp volume
  • Adds the file mentioned by the placeholder PROJECT_JAR (will be explained later) as app.jar
  • Creates an empty file by the name app.jar in the root
  • Sets the environment variable JAVA_OPTS
  • Creates the entry point into the Docker image with the Spring Boot 2.0 application, which is instructed to start app.jar using the java command

This Dockerfile is available for both spring-boot-2-taxi-service and spring-boot-2-taxi-booking-service. There are two more plugins used to help with this Dockerfile, which are listed as follows:

<plugins>
...

<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target</outputDirectory>
<resources>
<resource>
<directory>src/main/resources/docker</directory>
<includes>
<include>Dockerfile</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>${basedir}/target/Dockerfile</file>
<replacements>
<replacement>
<token>PROJECT_JAR</token>
<value>${project.build.finalName}.jar</value>
</replacement>
</replacements>
</configuration>
</plugin>
...
</plugins>

The maven-resource-plugin will copy the Dockerfile from the /src/resources/docker directory to the /target directory in order to create a Docker image successfully, while the replacer plugin will replace the PROJECT_JAR placeholder in the file /target/Dockerfile to have the Maven project build the final JAR name (an example is spring-boot-2-taxi-service-0.0.1-SNAPSHOT.jar).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.171.212