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

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

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.                       

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

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.

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

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.

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

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 learn more about the same or any other topic related to web development, programming, or coding, you can enroll in Simplilearn’s Full Stack Web Development program and seek expertise in your subject to advance in your career and land better job opportunities.

Besides the varied specialization courses we provide, you can also sign up on the SkillUp platform, a Simplilearn initiative that offers numerous free online courses.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.
  • *According to Simplilearn survey conducted and subject to terms & conditions with Ernst & Young LLP (EY) as Process Advisors