docker-maven-plugin

顾名思义,docker-maven-plugin是一个docker的maven插件,用来执行docker镜像的制作和上传,他的地址是https://github.com/spotify/docker-maven-plugin,里面有详细的说明

有两种方式

1、使用Dockerfile

2、不使用Dockerfile,直接在pom中定义

第二种方式有一些局限性,有一些Dockerfile的指令是不支持的。

介绍两种方式之前,需要先修改maven的setting文件,如果要上传镜像到私服,就必须要修改此文件。

<servers>   
  <server>
    <id>harbor</id>
    <username>admin</username>
    <password>Harbor12345</password>
    <configuration>
      <email>*******</email>
    </configuration>
  </server>
</servers> 

在pom文件中增加两个属性

<properties>
        <docker.registry>10.10.20.202/library</docker.registry>
        <tag>v7</tag>
    </properties>

docker.registry用来定义docker私服的前缀

tag用来定义标签

以下分别介绍这两种方式,

【第一种:使用Dockerfile】

1、在java工程根目录下创建Dockerfile,文件内容如下。

FROM 10.10.20.202/library/tomcat8:v1
ADD target/portal-0.0.1-SNAPSHOT /root/apache-tomcat-8.0.18/webapps/portal
ENTRYPOINT /root/apache-tomcat-8.0.18/bin/catalina.sh run
WORKDIR /root/apache-tomcat-8.0.18/webapps

2、在pom文件中,增加插件

<plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.4.11</version>
                <executions>
                    <execution>
                        <id>build-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                        <configuration>
                            <dockerDirectory>${project.basedir}</dockerDirectory>
                            <imageName>${docker.registry}/${project.artifactId}:${tag}</imageName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>push-image</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>push</goal>
                        </goals>
                        <configuration>
                            <serverId>harbor</serverId>
                            <imageName>${docker.registry}/${project.artifactId}:${tag}</imageName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

上述插件将创建镜像、打标签与package绑定,将上传镜像与deploy绑定,其中dockerDirectory定义了Dockerfile所在的路径,当使用dockerDirectory这个标签时,诸如baseImage、entryPoint等标签就不在生效了。

【第二种:不使用Dockerfile】

1、在pom文件中增加插件

<plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.4.11</version>
                <executions>
                    <execution>
                        <id>build-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                        <configuration>
                            <baseImage>10.10.20.202/library/tomcat8:v1</baseImage>
                            <resources>
                                <resource>
                                    <targetPath>/root/apache-tomcat-8.0.18/webapps/emp-portal</targetPath>
                                    <directory>${project.build.directory}/${project.build.finalName}</directory>
                                </resource>
                            </resources>
                            <imageName>${docker.registry}/${project.artifactId}:${tag}</imageName>
                            <entryPoint>["/root/apache-tomcat-8.0.18/bin/catalina.sh", "run"]</entryPoint>
                        </configuration>
                    </execution>
                    <execution>
                        <id>push-image</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>push</goal>
                        </goals>
                        <configuration>
                            <serverId>harbor</serverId>
                            <imageName>${docker.registry}/${project.artifactId}:${tag}</imageName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

与第一种方式不同的地方,在于创建镜像的定义中,增加了一些标签

baseImage:生命基础镜像,相当于Dockerfile中的FROM

recerouse:说明将哪个目录拷贝到镜像的哪个路径下

entrypoint:说明镜像启动后执行什么指令

原文地址:https://www.cnblogs.com/puroc/p/5799228.html