Summary of the dockerfile-maven-plugin usage guide

Summary of the dockerfile-maven-plugin usage guide

Recently, when deploying an application to a container platform, I needed to generate a docker image when packaging. I first searched for the docker-maven-plugin plug-in on the Internet, but it was very troublesome to use, and I had to do a lot of extra configuration in maven and dockerfile. Later, I saw on the official Github that the author recommended the use of the new plug-in dockerfile-maven-plugin, so I replaced it with this one, but there is little relevant information about this plug-in on the Internet. Record it here

pom configuration

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>

            <!-- Dockerfile maven plugin -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.4.10</version>
                <executions>
                    <!--<execution>-->
                        <!--<id>default</id>-->
                        <!--<goals>-->
                            <!--&lt;!&ndash;If you don't want to use docker packaging, comment out this goal&ndash;&gt;-->
                            <!--<goal>build</goal>-->
                            <!--<goal>push</goal>-->
                        <!--</goals>-->
                    <!--</execution>-->
                </executions>
                <configuration>
                    <repository>docker-reg.****.com/feedback/${artifactId}-${profiles.active}</repository>
                    <tag>${project.version}</tag>
                    <buildArgs>
                        <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                </configuration>
            </plugin>

Setting.xml configuration

This file is in the Maven directory and can be accessed by cd $M2_HOME/conf.

Add a com.spotify in pluginGroups

  <pluginGroups>
    <pluginGroup>com.spotify</pluginGroup>
  </pluginGroups>

Login status

Login required

There are many pitfalls in how to verify login. If you set your warehouse to private in habor, you must log in and follow the official configuration as follows.

 <plugin>
    <groupId>com.spotify</groupId>
    <artifactId>dockerfile-maven-plugin</artifactId>
    <version>${version}</version>
    <configuration>
        <username>repoUserName</username>
        <password>repoPassword</password>
        <repository>${docker.image.prefix}/${project.artifactId}</repository>
        <buildArgs>
            <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
        </buildArgs>
    </configuration>
</plugin>

No login required

But because I configured k8s to automatically obtain the image from habor, I set it to public. In this case, there is no need to log in, but sometimes the execution fails. At this time, you need to delete the configuration of this website in ~/.docker/config.json.

cat ~/.docker/config.json

{
    "auths": {
        "192.168.87.110:5000": {
            "auth": "YWRtaW46JKDtaW4xMjM="
        }(delete here)
    },
    "HttpHeaders": {
        "User-Agent": "Docker-Client/18.09.0 (linux)"
    }
}

After confirming that this is empty, if an error is still reported, you can execute docker login ... again, and it will succeed.

Maven multi-module configuration

In the case of multiple modules, the packaging plug-in must be placed in the Application submodule. If it is placed in the root pom, packaging will fail.

The following situations:

  • -app
  • -common
  • -file
  • -mail
  • -application
  • ​ -pom.xml

In this case, we can divide it into two steps

The first step is to package all modules in the root directory

mvn clean package -P test

The second step is to execute the deploy command in the submodule to package the image

mvn dockerfile:build dockerfile:push

In this way, the submodule can be successfully packaged into a mirror and pushed.

Jenkins

After testing locally, you need to move this process to Jenkins and do some configuration.

Install Docker on Jenkins Server

I will not go into details here. Maven's setting.xml and other configurations are the same as local ones.

Modify the Jenkins project configuration

At this point, since the project needs to be packaged twice (once in the root directory and the second time in the subdirectory as a mirror), the mvn command needs to be executed twice, which is different from before, so the first execution still uses the Jenkins Build module.

insert image description here

The second execution is placed in the post steps and executed by command

Mkb3FJ.md.png

cd submodule directory mvn clean package -P $env dockerfile:build dockerfile:push

In this way, you can complete the steps of packaging and making images.

This is the end of this article about the dockerfile-maven-plugin usage guide. For more information about the use of dockerfile-maven-plugin, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • The docker-maven-plugin plugin cannot pull the corresponding jar package
  • Detailed usage of docker-maven-plugin
  • Use of Maven plugin docker-maven-plugin
  • dockerfile-maven-plugin minimalist tutorial (recommended)
  • Teach you how to use docker-maven-plugin to automate deployment

<<:  How to handle long data when displaying it in html

>>:  Introduction to JavaScript strict mode use strict

Recommend

Analysis and description of network configuration files under Ubuntu system

I encountered a strange network problem today. I ...

CentOS8 installation tutorial of jdk8 / java8 (recommended)

Preface At first, I wanted to use wget to downloa...

How to install PostgreSQL11 on CentOS7

Install PostgreSQL 11 on CentOS 7 PostgreSQL: The...

How to adjust the log level of nginx in Docker

Table of contents Intro Nginx Dockerfile New conf...

The latest virtual machine VMware 14 installation tutorial

First, I will give you the VMware 14 activation c...

Detailed steps for installing rockerChat in docker and setting up a chat room

Comprehensive Documentation github address https:...

Collapsed table row element bug

Let's take an example: The code is very simple...

Implementation code for using CSS text-emphasis to emphasize text

1. Introduction In the past, if you wanted to emp...

JavaScript macrotasks and microtasks

Macrotasks and Microtasks JavaScript is a single-...

Perfect solution to the problem of webpack packaging css background image path

Inside the style tag of the vue component, there ...

CSS form validation function implementation code

Rendering principle In the form element, there is...

Native JS to implement real-time clock

Share a real-time clock effect implemented with n...

Summary of MySQL database like statement wildcard fuzzy query

MySQL error: Parameter index out of range (1 >...

How to deploy redis in linux environment and install it in docker

Installation Steps 1. Install Redis Download the ...