前面《Springboot整合MongoDB的Docker開發,其它應用也相似》講解了如何作Docker
開發、如何把Springboot
應用打包成一個鏡像,但它是手動的,本文將講解如何經過maven
一鍵打包部署。html
可使用maven
插件實現一鍵部署,這兩個插件仍是同一個公司的產品,就是著名的音樂流服務平臺Spotify
。git
該插件能夠實現鏡像打包和push
到倉庫,無Dockerfile
和有Dockerfile
兩種方式均可以,建議使用Dockerfile
,更靈活。在maven
的pom.xml
文件加入如下插件配置:github
<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
:這是鏡像名稱;spring
imageTags
:標籤,支持多標籤,即同一個鏡像文件多個標籤;我指定了一個參數imageVersion
,能夠命令行傳入,方便後續整合Jenkins
。docker
forceTags
:是否覆蓋原有標籤;apache
dockerDirectory
:Dockerfile文件所在的位置;並且該目錄下的全部文件都會被複制到${project.build.directory}/docker
。由於個人Dockerfile
放在項目根目錄,因此整個項目的文件都複製過去了,包括源代碼等。不得不吐槽一下這個設計,這是在強迫你們換個位置放Dockerfile
嗎?json
resources
:用來添加dockerDirectory
外的其它資源文件。安全
添加後,經過如下命令執行:springboot
$ mvn clean package docker:build -DimageVersion=0.0.4
經過命令docker images
查當作功,運行也正常。bash
經過下面命令能夠push到registry
:
mvn clean package docker:build -DpushImage mvn clean package docker:build -DpushImageTag
能夠經過添加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
便可。
首先,插件可使用配置在本地 ~/.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>
由於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
帳號能夠配置在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
經過maven
插件,能夠快速方便地一鍵打包、部署,很是方便,對後續的整個DevOps
整合也是頗有益的。
參考資料:
docker-maven-plugin:https://github.com/spotify/docker-maven-plugin
dockerfile-maven:https://github.com/spotify/dockerfile-maven
歡迎訪問南瓜慢說 www.pkslow.com獲取更多精彩文章!
歡迎關注微信公衆號<南瓜慢說>,將持續爲你更新...
多讀書,多分享;多寫做,多整理。
歡迎你們關注、分享。