Maven is a comprehension and software project management tool. It is mainly based on the project object model concept, also known as POM. Things like projects' build, reporting, and the documentation from a central piece of information are managed by this Maven.

Using a Maven Docker image directly, we can run our Maven project; we just need to pass a Maven command to docker run:

$ docker run -it --rm --name my-maven-project -v "$(pwd)":/usr/src/mymaven -w /usr/src/mymaven maven:3.3-jdk-8 mvn clean install

PRINCE2® Certification Exam Made Easy to Crack

PRINCE2® Foundation & Practitioner CertificationExplore Course
PRINCE2® Certification Exam Made Easy to Crack

Now let see how we can build a docker image, though this part is optional.

Building Docker Image 

Docker Image is a base image that we can extend if we want, and for this, we need a bare minimum amount of packages.

If custom packages are added to the Dockerfile by us, then the local docker image can be made by us like this:

$ docker build --tag my_local_maven:3.5.2-jdk-8 .

Docker Maven Plugin

The docker Maven plugin allows us to manage all the docker images and pom.xml containers. The pom.xml file of a maven-based project holds all the dependencies, repositories, etc., needed to build and run such a project.

There are some predefined goals of Docker Maven plugins, such as:

  • To create and start containers, we use :docker:start
  • To destroy and stop our containers, we use : docker:stop
  • To build the images, we use :docker:build
  • For pushing the images to a registry, we use :docker:push
  • To remove the images from our local docker host, we generally use :docker:remove
  • To show our container logs, we use :docker:remove

Now we will see what the Docker Maven Plugin configuration is.

Docker Maven Plugin Configuration

Now we will see the Docker Maven configuration through our sample application:

To include the docker-maven-plugin, we need to update our pom.xml as:

plugin>

    <groupId>org.jolokia</groupId>

    <artifactId>docker-maven-plugin</artifactId>

    <version>0.11.5</version>

    <configuration>

        <images>

            <image>

                <alias>user</alias>

                <name>shivamchandra/javaee7-docker-maven</name>

                <build>

                    <from>shivamchandra/wildfly:8.2</from>

                    <assembly>

                        <descriptor>assembly.xml</descriptor>

                        <basedir>/</basedir>

                    </assembly>

                    <ports>

                        <port>8080</port>

                    </ports>

                </build>

                <run>

                    <ports>

                        <port>8080:8080</port>

                    </ports>

                </run>

            </image>

        </images>

    </configuration>

    <executions>

        <execution>

            <id>docker:build</id>

            <phase>package</phase>

            <goals>

                <goal>build</goal>

            </goals>

        </execution>

        <execution>

            <id>docker:start</id>

            <phase>install</phase>

            <goals>

                <goal>start</goal>

            </goals>

        </execution>

    </executions>

</plugin>

There is a total of three parts in each image configuration :

  • The first part is the image name and alias.
  • The Second part is <build>, which is there to define how the image is created. Some things need to be exposed, like base images, build artifacts, their dependencies, and their ports; these need to be included in the image. To specify the artifacts to be included, we generally use Assembly Descriptor Format, which is defined in the src/main/docker directory.                       

Earn 40% More Than Non-Certified Peers

Lean Six Sigma Expert Masters ProgramEnroll Now!
Earn 40% More Than Non-Certified Peers

So, in our case the assembly.xml looks as shown below: 

<assembly . . .>

   <id>javaee7-docker-maven</id>

   <dependencySets>

     <dependencySet>

       <includes>

         <include>org.javaee7.sample:javaee7-docker-maven</include>

       </includes>

       <outputDirectory>/opt/jboss/wildfly/standalone/deployments/</outputDirectory>

       <outputFileNameMapping>javaee7-docker-maven.war</outputFileNameMapping>

     </dependencySet>

   </dependencySets>

 </assembly>

  • The third part is about how our container will run, which is generally defined by <run>. All the ports that need to be exposed are specified in this part.

Note: The package phase is tied to the docker:build goal, and the install phase is tied to the docker:start goal.

Build Docker Image: Using the Dockerfile-Maven-Plugin From Spotify

Now let us go through an example where we can see the building of a docker image using the dockerfile-maven-plugin from Spotify. We must have a docker file for this particular approach.

Alongside our pom.xml, we need to have our docker file as well.

FROM adoptopenjdk/openjdk11:alpine

RUN addgroup -S spring && adduser -S spring -G spring

USER spring:spring

VOLUME /tmp

ARG JAR_FILE

ADD ${JAR_FILE} /app/app.jar

EXPOSE 8080

ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app/app.jar"]

After this, in the pom.xml, some of the required configurations need to be added.

As we can see in our above code reference, an argument for the JAR_FILE and specifically a repository tag should be added.

<plugin>

 <groupId>com.spotify</groupId>

 <artifactId>dockerfile-maven-plugin</artifactId>

 <version>1.4.13</version>

 <executions>

   <execution>

     <id>default</id>

     <goals>

       <goal>build</goal>

       <goal>push</goal>

     </goals>

   </execution>

 </executions>

 <configuration>

   <repository>${project.artifactId}</repository>

   <tag>${project.version}</tag>

   <buildArgs>

     <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>

   </buildArgs>

 </configuration>

</plugin> 

Then the standard command called nvm package is required to be run. On completion of the creation of the docker image, we will be able to see a message on our screen indicating the Docker image has been successfully created.

We can run our new Docker image once it is built.

PRINCE2® Certification Exam Made Easy to Crack

PRINCE2® Foundation & Practitioner CertificationExplore Course
PRINCE2® Certification Exam Made Easy to Crack

Build Docker Image: Using jib-maven-plugin From Google 

We use this jib-maven plugin to create the Docker images for Java applications. It is mainly a Maven and Gradle plugin.

The main advantage of using this plugin is that we don’t need to install Docker locally, which is helpful in building servers or continuous integration. This plugin builds and pushes the image to the Docker registry of choice, 

Now we need to run the following command to build the docker image:

mvn compile com.google.cloud.tools:jib-maven-plugin:2.3.0:dockerBuild

On the completion of building the Docker image, we will see a success message that generally indicates that our docker image has been created successfully.

We can run our image once it is built successfully.

Choose The Right DevOps Program

This table compares various DevOps programs offered by Simplilearn, based on several key features and details. The table provides an overview of the courses' duration, skills you will learn, additional benefits, among other important factors, to help you make an informed decision about which course best suits your needs.

Program Name DevOps Engineer Masters Program Post Graduate Program in DevOps
Geo All All
University Simplilearn Caltech
Course Duration 11 Months 9 Months
Coding Experience Required Basic Knowledge Basic Knowledge
Skills You Will Learn 40+ Skills Including Ansible, Puppet, Chef, Jenkins, etc. 10+ Skills Including CI,CD, DevOps on Cloud, Deployment Automation, etc.
Additional Benefits Masters Certification
Real Life Projects
Learn 40+ Skills and Tools
Caltech Campus Connect
Career Services
Masterclasses by Caltech Instructors
Cost $$ $$$
Explore Program Explore Program

Conclusion

So we can see that, using Docker Maven, our application is made resilient against Docker API  changes. It makes our build self-contained, and the setup of build slave becomes super easy. 

If you want to enhance your skills further, we would highly recomment you to check Simplilearn's Post Graduate Program in DevOps. This course can help you hone the right DevOps skills and make you job-ready in no time.

If you have any questions or doubts, feel free to post them in the comments section below. Our team will get back to you at the earliest!

Our DevOps Program Duration and Fees

DevOps programs typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Post Graduate Program in DevOps

Cohort Starts: 8 May, 2024

9 Months$ 4,849
DevOps Engineer11 Months$ 2,000