前面《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获取更多精彩文章!
欢迎关注微信公众号<南瓜慢说>,将持续为你更新...
多读书,多分享;多写做,多整理。
欢迎你们关注、分享。