#1 spring-boot工程在pom文件中加入如下配置项

org.springframework.bootgroupId>
spring-boot-maven-pluginartifactId>

com.gongdear.gateway.ApplicationmainClass>
ZIPlayout>
configuration>



repackagegoal>
goals>
execution>
executions>
plugin>

com.spotifygroupId>
docker-maven-pluginartifactId>

${docker.imageName.prefix}/${project.artifactId}:${project.version}imageName>
java:8baseImage>
${docker.maintainer}maintainer>
/tmpvolumes>
["java", "-Djava.security.egd=file:/dev/./urandom","-jar", "${project.build.finalName}.jar"]entryPoint>


/targetPath>
${project.build.directory}directory>
${project.build.finalName}.jarinclude>
resource>
resources>
configuration>
plugin>
image.png

此处配置为免dockerfile直接打docker镜像的配置
打包时执行 mvn clean package docker:build 就可以在本地生成docker镜像
[INFO] Using authentication suppliers: [ConfigFileRegistryAuthSupplier]
[INFO] Copying /home/gongdear/src/gateway/target/gateway.jar -> /home/gongdear/src/gateway/target/docker/gateway.jar
[INFO] Building image dockerhub.gongdear.com/gateway:1.0-RELEASE
Step 1/5 : FROM java:8

---> d23bdf5b1b1b
Step 2/5 : MAINTAINER gongdear@gmail.com

---> Using cache
---> 4af248cad229
Step 3/5 : ADD /gateway.jar //

---> 1479acc37a0f
Step 4/5 : ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom","-jar", "gateway.jar"]

---> Running in 28897782db7f
Removing intermediate container 28897782db7f
---> 8e2f81aeda24
Step 5/5 : VOLUME /tmp

---> Running in 210d59cc1eb7
Removing intermediate container 210d59cc1eb7
---> 5558c2e2e97e
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 5558c2e2e97e
Successfully tagged dockerhub.gongdear.com/gateway:1.0-RELEASE
[INFO] Built dockerhub.gongdear.com/gateway:1.0-RELEASE
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 23.609 s
[INFO] Finished at: 2019-01-22T11:03:23+08:00
[INFO] ------------------------------------------------------------------------
[root@kvmmain gateway]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dockerhub.gongdear.com/gateway 1.0-RELEASE 5558c2e2e97e 8 seconds ago 676MB
dockerhub.gongdear.com/crawler 0.1.1 e667d480644e 2 hours ago 671MB
dockerhub.gongdear.com/gateway f6cca9232c8a 13 hours ago 676MB
dockerhub.gongdear.com/gateway 1.0-SNAPSHOT ac5cb8b0bb0d 18 hours ago 676MB

imagepng

进阶版 mvn指令可以直接将docker镜像推送到dockerhub或私有库,前提是上述配置文件中${docker.imageName.prefix} 是私库的地址
[root@kvmmain gateway]# mvn clean package docker:build -DpushImage
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.gongdear:gateway🏺1.0-RELEASE
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 113, column 21
[WARNING] 'build.plugins.plugin.version' for com.spotify:docker-maven-plugin is missing. @ line 95, column 21
[WARNING] 'build.plugins.plugin.version' for org.springframework.boot:spring-boot-maven-plugin is missing. @ line 79, column 21
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------< com.gongdear:gateway >------------------------
[INFO] Building gateway 1.0-RELEASE
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ gateway ---
[INFO] Deleting /home/gongdear/src/gateway/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ gateway ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/gongdear/src/gateway/src/main/resources
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ gateway ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /home/gongdear/src/gateway/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ gateway ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/gongdear/src/gateway/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ gateway ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ gateway ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ gateway ---
[INFO] Building jar: /home/gongdear/src/gateway/target/gateway.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.1.0.RELEASE:repackage (default) @ gateway ---
[INFO] Layout: ZIP
[INFO] Replacing main artifact /home/gongdear/src/gateway/target/gateway.jar
[INFO]
[INFO] --- docker-maven-plugin:1.2.0:build (default-cli) @ gateway ---
[INFO] Using authentication suppliers: [ConfigFileRegistryAuthSupplier]
[INFO] Copying /home/gongdear/src/gateway/target/gateway.jar -> /home/gongdear/src/gateway/target/docker/gateway.jar
[INFO] Building image dockerhub.gongdear.com/gateway:1.0-RELEASE
Step 1/5 : FROM java:8

---> d23bdf5b1b1b
Step 2/5 : MAINTAINER gongdear@gmail.com

---> Using cache
---> 4af248cad229
Step 3/5 : ADD /gateway.jar //

---> d5c56be69deb
Step 4/5 : ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom","-jar", "gateway.jar"]

---> Running in 150cd4f5e379
Removing intermediate container 150cd4f5e379
---> 3f3c12c969d2
Step 5/5 : VOLUME /tmp

---> Running in 0e77cd1d901e
Removing intermediate container 0e77cd1d901e
---> 4a3428f8e7a6
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 4a3428f8e7a6
Successfully tagged dockerhub.gongdear.com/gateway:1.0-RELEASE
[INFO] Built dockerhub.gongdear.com/gateway:1.0-RELEASE
[INFO] Pushing dockerhub.gongdear.com/gateway:1.0-RELEASE
The push refers to repository [dockerhub.gongdear.com/gateway]
28e4eb162bb6: Pushed
35c20f26d188: Layer already exists
c3fe59dd9556: Layer already exists
6ed1a81ba5b6: Layer already exists
a3483ce177ce: Layer already exists
ce6c8756685b: Layer already exists
30339f20ced0: Layer already exists
0eb22bfb707d: Layer already exists
a2ae92ffcd29: Layer already exists
1.0-RELEASE: digest: sha256:3211630fa47d6f799efadcdd3d6a39508da24fab5e3c65b1baa6e2b65fab69ae size: 2212
null: null
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 05:30 min
[INFO] Finished at: 2019-01-22T13:13:36+08:00
[INFO] ------------------------------------------------------------------------
[root@kvmmain gateway]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dockerhub.gongdear.com/gateway 1.0-RELEASE 4a3428f8e7a6 5 minutes ago 676MB
09b5b4dd5bbf 11 minutes ago 676MB
dockerhub.gongdear.com/crawler 0.1.1 e667d480644e 4 hours ago 671MB
dockerhub.gongdear.com/gateway 1.0-SNAPSHOT ac5cb8b0bb0d 20 hours ago 676MB
bestwu/kubernetes-dashboard-amd64 v1.6.3 691a82db1ecd 18 months ago 139MB
java 8 d23bdf5b1b1b 2 years ago 643MB
mirrorgooglecontainers/skydns 2015-10-13-8c72f8c 718809956625 3 years ago 40.6MB
mirrorgooglecontainers/kube2sky 1.11 2c2534f5ba34 3 years ago 19.2MB

可见加上-DpushImage参数后会自动push当前镜像到私库
此时基本已经具备编写jenkins脚本的基础
#2 jenkins的脚本配置
imagepng
imagepng
imagepng

此时就可以进行参数化构建 构建完成以后自动将镜像push到私库中
imagepng

imagepng

可是这样还有个问题 如果都是同一个版本的代码,构建完成以后会留下tag为none的历史版本
执行 docker images|grep none|awk '{print $3}'|xargs docker rmi -f 可以将所有标签为none的镜像删除
imagepng
imagepng

可见此时已经可以不需要手动输入任何命令就可以将代码直接打包成私库中的镜像,并且版本号和pom文件中版本保持一致

5d149901bcdf40669042d2e6f6649688image.png5d149901bcdf40669042d2e6f6649688image.png

宝剑锋从磨砺出,梅花香自苦寒来.