顾名思义,docker-maven-plugin是一个docker的maven插件,用来执行docker镜像的制做和上传,他的地址是https://github.com/spotify/docker-maven-plugin,里面有详细的说明java
有两种方式git
一、使用Dockerfilegithub
二、不使用Dockerfile,直接在pom中定义web
第二种方式有一些局限性,有一些Dockerfile的指令是不支持的。docker
介绍两种方式以前,须要先修改maven的setting文件,若是要上传镜像到私服,就必需要修改此文件。apache
<servers> <server> <id>harbor</id> <username>admin</username> <password>Harbor12345</password> <configuration> <email>*******</email> </configuration> </server> </servers>
在pom文件中增长两个属性tomcat
<properties> <docker.registry>10.10.20.202/library</docker.registry> <tag>v7</tag> </properties>
docker.registry用来定义docker私服的前缀app
tag用来定义标签webapp
如下分别介绍这两种方式,maven
【第一种:使用Dockerfile】
一、在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
二、在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】
一、在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:说明镜像启动后执行什么指令