How to automate Docker container deployment via Maven

Background Image Courtesy — https://cdn.pixabay.com/ | Created Via https://www.canva.com/

Step 1 | Create the Dockerfile

# Pull base image
FROM tomcat:8.0.30-jre7

# Maintainer
MAINTAINER "ravindu@emojot.com"
# Set Environment properties
ENV JAVA_OPTS
=-Denvironment=production

# Copy war file to tomcat webapps folder
COPY /dockermavensample.war /usr/local/tomcat/webapps/

Step 2 | Update the pom.xml to copy all Docker-related resources into the target directory

<plugin>
<artifactId>
maven-resources-plugin</artifactId>
<executions>
<execution>
<id>
copy-resources</id>
<phase>
validate</phase>
<goals>
<goal>
copy-resources</goal>
</goals>
<configuration>
<outputDirectory>
${basedir}/target</outputDirectory>
<resources>
<resource>
<directory>
src/main/docker</directory>
<filtering>
true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

Step 3 | Update the pom.xml to allow build and tag the Docker image via Maven’s Ant plugin

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>prepare-package</id>
<phase>package</phase>
<inherited>false</inherited>
<configuration>
<target>
<exec executable="docker">
<arg value="build"/>
<arg value="-t"/>
<arg value="dockermavensample:${project.version}"/>
<arg value="target"/>
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
docker build -t dockermavensample:1.0.0 target

Step 4 | Update the pom.xml file to allow pushing Docker Image to remote Docker repository

<plugin>
<groupId>
org.apache.maven.plugins</groupId>
<artifactId>
maven-antrun-plugin</artifactId>
<version>
1.6</version>
<executions>
<execution>
<phase>
install</phase>
<inherited>
false</inherited>
<configuration>
<target>
<exec executable="
docker">
<arg value="
tag"/>
<arg value="
dockermavensample:${project.version}"/>
<arg value="
dockermavensample:latest"/>
</exec>
<exec executable="
docker">
<arg value="
push"/>
<arg value="
dockermavensample:latest"/>
</exec>
</target>
</configuration>
<goals>
<goal>
run</goal>
</goals>
</execution>
</executions>
</plugin>
<profile>
<id>dockerBuild</id>

<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target</outputDirectory>
<resources>
<resource>
<directory>src/main/docker</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>prepare-package</id>
<phase>package</phase>
<inherited>false</inherited>
<configuration>
<target>
<exec executable="docker">
<arg value="build"/>
<arg value="-t"/>
<arg value="dockermavensample:${project.version}"/>
<arg value="target"/>
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- docker Image push and release profile -->
<profile>
<id>dockerRelease</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>install</phase>
<inherited>false</inherited>
<configuration>
<target>
<exec executable="docker">
<arg value="tag"/>
<arg value="dockermavensample:${project.version}"/>
<arg value="dockermavensample:latest"/>
</exec>
<exec executable="docker">
<arg value="push"/>
<arg value="dockermavensample:latest"/>
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
mvn clean install -P dockerBuild,dockerRelease
After running dockerBuild profile, the Docker image should be available locally
Run dockermavensample:1.0.0 Docker Image
Apache Tomcat Home Page
Kaboom! :)

--

--

We’ve moved to https://freecodecamp.org/news and publish tons of tutorials each week. See you there.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store