顧名思義,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:說明鏡像啟動后執行什么指令
