1 前言
前面《Springboot整合MongoDB的Docker開發,其它應用也類似》講解了如何做Docker開發、如何把Springboot應用打包成一個鏡像,但它是手動的,本文將講解如何通過maven一鍵打包部署。
2 兩個maven插件搞定
可以使用maven插件實現一鍵部署,這兩個插件還是同一個公司的產品,就是著名的音樂流服務平台Spotify。

2.1 spotify/docker-maven-plugin
2.1.1 基礎用法
該插件可以實現鏡像打包和push到倉庫,無Dockerfile和有Dockerfile兩種方式都可以,建議使用Dockerfile,更靈活。在maven的pom.xml文件加入以下插件配置:
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.2.2</version>
<configuration>
<imageName>pkslow/springboot-mongo</imageName>
<imageTags>
<imageTag>${imageVersion}</imageTag>
<imageTag>latest</imageTag>
</imageTags>
<!-- optionally overwrite tags every time image is built with docker:build -->
<forceTags>true</forceTags>
<dockerDirectory>${project.basedir}</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
-
imageName:這是鏡像名稱; -
imageTags:標簽,支持多標簽,即同一個鏡像文件多個標簽;我指定了一個參數imageVersion,可以命令行傳入,方便后續整合Jenkins。 -
forceTags:是否覆蓋原有標簽; -
dockerDirectory:Dockerfile文件所在的位置;而且該目錄下的所有文件都會被復制到${project.build.directory}/docker。因為我的Dockerfile放在項目根目錄,所以整個項目的文件都復制過去了,包括源代碼等。不得不吐槽一下這個設計,這是在強迫大家換個位置放Dockerfile嗎? -
resources:用來添加dockerDirectory外的其它資源文件。
添加后,通過以下命令執行:
$ mvn clean package docker:build -DimageVersion=0.0.4
通過命令docker images查看成功,運行也正常。
通過下面命令可以push到registry:
mvn clean package docker:build -DpushImage
mvn clean package docker:build -DpushImageTag
2.1.2 與maven生命周期綁定
可以通過添加executions配置實現與maven生命周期的綁定。
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>VERSION GOES HERE</version>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
<execution>
<id>tag-image</id>
<phase>package</phase>
<goals>
<goal>tag</goal>
</goals>
<configuration>
<image>my-image:${project.version}</image>
<newName>registry.example.com/my-image:${project.version}</newName>
</configuration>
</execution>
<execution>
<id>push-image</id>
<phase>deploy</phase>
<goals>
<goal>push</goal>
</goals>
<configuration>
<imageName>registry.example.com/my-image:${project.version}</imageName>
</configuration>
</execution>
</executions>
</plugin>
有了這些綁定配置后,要打包鏡像,直接mvn clean package即可。
2.1.3 倉庫登陸信息配置
首先,插件可以使用配置在本地 ~/.dockercfg 或 ~/.docker/config.json的驗證信息,或者可以顯式地配置在maven上。
如配置在settings.xml文件:
<servers>
<server>
<id>docker-hub</id>
<username>foo</username>
<password>secret-password</password>
<configuration>
<email>foo@foo.bar</email>
</configuration>
</server>
</servers>
密碼是可以加密的,詳情請查看: Maven's built in encryption function 。
在項目的pom.xml中使用:
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>VERSION GOES HERE</version>
<configuration>
[...]
<serverId>docker-hub</serverId>
<registryUrl>https://index.docker.io/v1/</registryUrl>
</configuration>
</plugin>
</plugins>
2.2 spotify/dockerfile-maven
2.2.1 更簡潔的插件
因為docker-maven-plugin有一些Bugs,所以Spotify開發了更方便簡潔的插件dockerfile-maven。

dockerfile-maven-plugin的配置更簡單:
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>${dockerfile-maven-version}</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>spotify/foobar</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
配置好后,執行以下maven命令即可打包成鏡像並推送到倉庫:
mvn deploy
2.2.2 倉庫驗證
賬號可以配置在pom.xml中,如下:
<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>
當然,也可以配置在maven的配置文件settings.xml中,這樣更安全,請參考:https://github.com/spotify/dockerfile-maven/blob/master/docs/authentication.md
3 總結
通過maven插件,可以快速方便地一鍵打包、部署,非常方便,對后續的整個DevOps整合也是很有益的。
參考資料:
docker-maven-plugin:https://github.com/spotify/docker-maven-plugin
dockerfile-maven:https://github.com/spotify/dockerfile-maven
歡迎訪問南瓜慢說 www.pkslow.com獲取更多精彩文章!
歡迎關注微信公眾號<南瓜慢說>,將持續為你更新...

多讀書,多分享;多寫作,多整理。
歡迎大家關注、分享。
