飞道的博客

还在使用第三方Docker插件?SpringBoot官方插件真香!

285人阅读  评论(0)

为了方便为SpringBoot应用构建Docker镜像,我们经常会使用Maven插件来打包镜像。之前一直使用的是第三方插件,有spotifyfabric8出品的两种docker-maven-plugin。最近SpringBoot 2.4.0发布了,官方插件也增加了对Docker的支持,体验了一把发现也很好用,推荐给大家!

第三方插件使用

我们先了解下第三方插件的使用,方便和官方插件做对比,fabric8插件使用具体可以参考《还在手动部署SpringBoot应用?试试这个自动化插件!》

  • 值得注意的是,在我们使用插件时,需要自己定义镜像构建过程,比如在pom.xml中使用如下配置,<images>标签下的配置为镜像构建过程的配置;


   
  1. <build>
  2.     <plugins>
  3.         <plugin>
  4.             <groupId>io.fabric8</groupId>
  5.             <artifactId>docker-maven-plugin</artifactId>
  6.             <version> 0.33 .0</version>
  7.             <configuration>
  8.                 <!-- Docker 远程管理地址-->
  9.                 <dockerHost>http: //192.168.3.101:2375</dockerHost>
  10.                 <!-- Docker 推送镜像仓库地址-->
  11.                 <pushRegistry>http: //192.168.3.101:5000</pushRegistry>
  12.                 <images>
  13.                     <image>
  14.                         <!--由于推送到私有镜像仓库,镜像名需要添加仓库地址-->
  15.                         <name> 192.168 .3 .101: 5000/mall-tiny/${project.name}:${project.version}</name>
  16.                         <!--定义镜像构建行为-->
  17.                         <build>
  18.                             <!--定义基础镜像-->
  19.                             <from>java: 8</from>
  20.                             <args>
  21.                                 <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
  22.                             </args>
  23.                             <!--定义哪些文件拷贝到容器中-->
  24.                             <assembly>
  25.                                 <!--定义拷贝到容器的目录-->
  26.                                 <targetDir>/</targetDir>
  27.                                 <!--只拷贝生成的jar包-->
  28.                                 <descriptorRef>artifact</descriptorRef>
  29.                             </assembly>
  30.                             <!--定义容器启动命令-->
  31.                             <entryPoint>[ "java""-jar", "/${project.build.finalName}.jar"]</entryPoint>
  32.                             <!--定义维护者-->
  33.                             <maintainer>macrozheng</maintainer>
  34.                         </build>
  35.                     </image>
  36.                 </images>
  37.             </configuration>
  38.         </plugin>
  39.     </plugins>
  40. </build>
  • 或者先在Dockerfile文件中定义好镜像构建过程;


   
  1. # 该镜像需要依赖的基础镜像
  2. FROM java: 8
  3. # 将当前maven目录生成的文件复制到docker容器的/目录下
  4. COPY maven /
  5. # 声明服务运行在 8080端口
  6. EXPOSE  8080
  7. # 指定docker容器启动时运行jar包
  8. ENTRYPOINT [ "java""-jar", "/mall-tiny-fabric-0.0.1-SNAPSHOT.jar"]
  9. # 指定维护者的名字
  10. MAINTAINER macrozheng
  • 然后在插件中引用Dockerfile文件,用于构建镜像;


   
  1. <build>
  2.      <dockerFileDir>${project.basedir}</dockerFileDir>
  3. </build>
  • 其实对于SpringBoot应用来说,如何从应用Jar包构建Docker镜像,做法基本是差不多的,为什么非要自己定义镜像的构建过程呢?

官方插件使用

SpringBoot官方插件解决了上面的问题,无需自己编写Docker镜像构建过程,直接自动构建,是不是很方便!接下来我们来体验下它的强大之处!


   
  1. [root@linux-local ~]# docker ps
  2. CONTAINER ID        IMAGE                                                                COMMAND                  CREATED             STATUS              PORTS                                            NAMES
  3. 9ffec08467ac        joxit/docker-registry-ui:static                                       "/bin/sh -c entrypoi…"    2 hours ago         Up  2 hours           0.0 .0 .0: 8280-> 80/tcp                             registry-ui
  4. a809535ee2a2        registry: 2                                                            "/entrypoint.sh /etc…"    3 months ago        Up  7 hours           0.0 .0 .0: 5000-> 5000/tcp                           registry2
  • 然后我们需要把应用的版本升级到SpringBoot 2.4.0,之前的版本Docker支持没有这个完善;


   
  1. <parent>
  2.     <groupId>org.springframework.boot</groupId>
  3.     <artifactId>spring-boot-starter-parent</artifactId>
  4.     <version> 2.4 .0</version>
  5.     <relativePath/> <!-- lookup parent from repository -->
  6. </parent>
  • 然后修改pom.xml文件,对官方Maven插件进行配置,主要是对Docker相关功能进行配置;


   
  1. <plugin>
  2.     <groupId>org.springframework.boot</groupId>
  3.     <artifactId>spring-boot-maven-plugin</artifactId>
  4.     <configuration>
  5.         <image>
  6.             <!--配置镜像名称-->
  7.             <name> 192.168 .3 .101: 5000/mall-tiny/${project.name}:${project.version}</name>
  8.             <!--镜像打包完成后自动推送到镜像仓库-->
  9.             <publish> true</publish>
  10.         </image>
  11.         <docker>
  12.             <!--Docker远程管理地址-->
  13.             <host>http: //192.168.3.101:2375</host>
  14.             <!--不使用TLS访问-->
  15.             <tlsVerify> false</tlsVerify>
  16.             <!--Docker推送镜像仓库配置-->
  17.             <publishRegistry>
  18.                 <!--推送镜像仓库用户名-->
  19.                 <username>test</username>
  20.                 <!--推送镜像仓库密码-->
  21.                 <password>test</password>
  22.                 <!--推送镜像仓库地址-->
  23.                 <url>http: //192.168.3.101:5000</url>
  24.             </publishRegistry>
  25.         </docker>
  26.     </configuration>
  27. </plugin>
  • 如果你使用的是IDEA的话,直接双击SpringBoot插件的build-image命令即可一键打包并推送到镜像仓库;

  • 也可以在命令行使用如下Maven命令来打包构建镜像;

mvn spring-boot:build-image
  • 镜像构建过程中会输出如下信息,由于很多依赖会从Github上下载,网络不好的情况下会下载失败,多试几次就好:


   
  1. [INFO]  > Pulling builder image  'docker.io/paketobuildpacks/builder:base'  100%
  2. [INFO]  > Pulled builder image  'paketobuildpacks/builder@sha256:9d377230ba8ee74d8619178fd318b1b87a7da1a88bdb198afd14dd7de9e8ea6a'
  3. [INFO]  > Pulling run image  'docker.io/paketobuildpacks/run:base-cnb'  100%
  4. [INFO]  > Pulled run image  'paketobuildpacks/run@sha256:33d37fc9ba16e220f071805eaeed881a508ceee5c8909db5710aaed7e97e4fc2'
  5. [INFO]  > Executing lifecycle version v0 .9 .3
  6. [INFO]  > Using build cache volume  'pack-cache-5641f846df6.build'
  7. [INFO] 
  8. [INFO]  > Running creator
  9. [INFO]     [creator]     ===> DETECTING
  10. [INFO]     [creator]      5 of  18 buildpacks participating
  11. [INFO]     [creator]     paketo-buildpacks/ca-certificates    1.0 .1
  12. [INFO]     [creator]     paketo-buildpacks/bellsoft-liberica  5.2 .1
  13. [INFO]     [creator]     paketo-buildpacks/executable-jar     3.1 .3
  14. [INFO]     [creator]     paketo-buildpacks/dist-zip           2.2 .2
  15. [INFO]     [creator]     paketo-buildpacks/spring-boot        3.5 .0
  16. [INFO]     [creator]     ===> ANALYZING
  17. [INFO]     [creator]     Restoring metadata  for  "paketo-buildpacks/ca-certificates:helper" from app image
  18. [INFO]     [creator]     Restoring metadata  for  "paketo-buildpacks/bellsoft-liberica:helper" from app image
  19. [INFO]     [creator]     Restoring metadata  for  "paketo-buildpacks/bellsoft-liberica:java-security-properties" from app image
  20. [INFO]     [creator]     Restoring metadata  for  "paketo-buildpacks/bellsoft-liberica:jre" from app image
  21. [INFO]     [creator]     Restoring metadata  for  "paketo-buildpacks/bellsoft-liberica:jvmkill" from app image
  22. [INFO]     [creator]     Restoring metadata  for  "paketo-buildpacks/executable-jar:class-path" from app image
  23. [INFO]     [creator]     Restoring metadata  for  "paketo-buildpacks/spring-boot:helper" from app image
  24. [INFO]     [creator]     Restoring metadata  for  "paketo-buildpacks/spring-boot:spring-cloud-bindings" from app image
  25. [INFO]     [creator]     Restoring metadata  for  "paketo-buildpacks/spring-boot:web-application-type" from app image
  26. [INFO]     [creator]     ===> RESTORING
  27. [INFO]     [creator]     ===> BUILDING
  28. [INFO]     [creator]     
  29. [INFO]     [creator]     Paketo CA Certificates Buildpack  1.0 .1
  30. [INFO]     [creator]       https: //github.com/paketo-buildpacks/ca-certificates
  31. [INFO]     [creator]       Launch Helper: Reusing cached layer
  32. [INFO]     [creator]     
  33. [INFO]     [creator]     Paketo BellSoft Liberica Buildpack  5.2 .1
  34. [INFO]     [creator]       https: //github.com/paketo-buildpacks/bellsoft-liberica
  35. [INFO]     [creator]       Build Configuration:
  36. [INFO]     [creator]         $BP_JVM_VERSION               8.*             the Java version
  37. [INFO]     [creator]       Launch Configuration:
  38. [INFO]     [creator]         $BPL_JVM_HEAD_ROOM            0               the headroom in memory calculation
  39. [INFO]     [creator]         $BPL_JVM_LOADED_CLASS_COUNT   35% of classes  the number of loaded classes in memory calculation
  40. [INFO]     [creator]         $BPL_JVM_THREAD_COUNT         250             the number of threads in memory calculation
  41. [INFO]     [creator]         $JAVA_TOOL_OPTIONS                           the JVM launch flags
  42. [INFO]     [creator]       BellSoft Liberica JRE  8.0 .275: Reusing cached layer
  43. [INFO]     [creator]       Launch Helper: Reusing cached layer
  44. [INFO]     [creator]       JVMKill Agent  1.16 .0: Reusing cached layer
  45. [INFO]     [creator]       Java Security Properties: Reusing cached layer
  46. [INFO]     [creator]     
  47. [INFO]     [creator]     Paketo Executable JAR Buildpack  3.1 .3
  48. [INFO]     [creator]       https: //github.com/paketo-buildpacks/executable-jar
  49. [INFO]     [creator]       Process types:
  50. [INFO]     [creator]         executable-jar: java org.springframework.boot.loader.JarLauncher
  51. [INFO]     [creator]         task:           java org.springframework.boot.loader.JarLauncher
  52. [INFO]     [creator]         web:            java org.springframework.boot.loader.JarLauncher
  53. [INFO]     [creator]     
  54. [INFO]     [creator]     Paketo Spring Boot Buildpack  3.5 .0
  55. [INFO]     [creator]       https: //github.com/paketo-buildpacks/spring-boot
  56. [INFO]     [creator]       Creating slices from layers index
  57. [INFO]     [creator]         dependencies
  58. [INFO]     [creator]         spring-boot-loader
  59. [INFO]     [creator]         snapshot-dependencies
  60. [INFO]     [creator]         application
  61. [INFO]     [creator]       Launch Helper: Reusing cached layer
  62. [INFO]     [creator]       Web Application Type: Contributing to layer
  63. [INFO]     [creator]         Servlet web application detected
  64. [INFO]     [creator]         Writing env.launch/BPL_JVM_THREAD_COUNT. default
  65. [INFO]     [creator]       Spring Cloud Bindings  1.7 .0: Reusing cached layer
  66. [INFO]     [creator]        4 application slices
  67. [INFO]     [creator]       Image labels:
  68. [INFO]     [creator]         org.opencontainers.image.title
  69. [INFO]     [creator]         org.opencontainers.image.version
  70. [INFO]     [creator]         org.springframework.boot.spring-configuration-metadata.json
  71. [INFO]     [creator]         org.springframework.boot.version
  72. [INFO]     [creator]     ===> EXPORTING
  73. [INFO]     [creator]     Reusing layer  'paketo-buildpacks/ca-certificates:helper'
  74. [INFO]     [creator]     Reusing layer  'paketo-buildpacks/bellsoft-liberica:helper'
  75. [INFO]     [creator]     Reusing layer  'paketo-buildpacks/bellsoft-liberica:java-security-properties'
  76. [INFO]     [creator]     Reusing layer  'paketo-buildpacks/bellsoft-liberica:jre'
  77. [INFO]     [creator]     Reusing layer  'paketo-buildpacks/bellsoft-liberica:jvmkill'
  78. [INFO]     [creator]     Reusing layer  'paketo-buildpacks/executable-jar:class-path'
  79. [INFO]     [creator]     Reusing layer  'paketo-buildpacks/spring-boot:helper'
  80. [INFO]     [creator]     Reusing layer  'paketo-buildpacks/spring-boot:spring-cloud-bindings'
  81. [INFO]     [creator]     Reusing layer  'paketo-buildpacks/spring-boot:web-application-type'
  82. [INFO]     [creator]     Reusing  4/ 5 app layer(s)
  83. [INFO]     [creator]     Adding  1/ 5 app layer(s)
  84. [INFO]     [creator]     Reusing layer  'launcher'
  85. [INFO]     [creator]     Reusing layer  'config'
  86. [INFO]     [creator]     Reusing layer  'process-types'
  87. [INFO]     [creator]     Adding label  'io.buildpacks.lifecycle.metadata'
  88. [INFO]     [creator]     Adding label  'io.buildpacks.build.metadata'
  89. [INFO]     [creator]     Adding label  'io.buildpacks.project.metadata'
  90. [INFO]     [creator]     Adding label  'org.opencontainers.image.title'
  91. [INFO]     [creator]     Adding label  'org.opencontainers.image.version'
  92. [INFO]     [creator]     Adding label  'org.springframework.boot.spring-configuration-metadata.json'
  93. [INFO]     [creator]     Adding label  'org.springframework.boot.version'
  94. [INFO]     [creator]     Setting  default process  type  'web'
  95. [INFO]     [creator]     *** Images (d5e1771dac7b):
  96. [INFO]     [creator]            192.168 .3 .101: 5000/mall-tiny/mall-tiny-docker-plugin: 0.0 .1-SNAPSHOT
  97. [INFO] 
  98. [INFO] Successfully built image  '192.168.3.101:5000/mall-tiny/mall-tiny-docker-plugin:0.0.1-SNAPSHOT'
  99. [INFO] 
  100. [INFO]  > Pushed image  '192.168.3.101:5000/mall-tiny/mall-tiny-docker-plugin:0.0.1-SNAPSHOT'
  101. [INFO] ------------------------------------------------------------------------
  102. [INFO] BUILD SUCCESS
  103. [INFO] ------------------------------------------------------------------------
  104. [INFO] Total time:  01: 06 min
  105. [INFO] Finished at:  2020 -11 -27T15: 07: 46+ 08: 00
  106. [INFO] Final Memory:  37M/ 359M
  107. [INFO] ------------------------------------------------------------------------
  • 镜像构建成功后,可以从镜像仓库查看到我们的镜像:


   
  1. [root@linux-local ~]# docker images
  2. REPOSITORY                                            TAG                 IMAGE ID            CREATED             SIZE
  3. paketobuildpacks/run                                  base-cnb            a717358311fc         9 days ago           87.2MB
  4. java                                                   8                   d23bdf5b1b1b         3 years ago          643MB
  5. 192.168 .3 .101: 5000/mall-tiny/mall-tiny-docker-plugin    0.0 .1-SNAPSHOT      d5e1771dac7b         40 years ago         244MB
  6. pack.local/builder/fewqajyqsc                         latest              f15fad05a5ba         40 years ago         558MB
  7. pack.local/builder/kirivtcqtu                         latest              f15fad05a5ba         40 years ago         558MB
  8. paketobuildpacks/builder                              base                 511452064e06         40 years ago         558MB
  • 我们可以从Docker Registry UI中查看镜像仓库中的镜像,访问地址:http://192.168.3.101:8280/

  • 接着使用如下命令启动我们的SpringBoot应用:


   
  1. docker run -p  8080: 8080 --name mall-tiny-docker-plugin \
  2. --link mysql:db \
  3. -v /etc/localtime:/etc/localtime \
  4. -v /mydata/app/mall-tiny-docker-plugin/logs:/ var/logs \
  5. -d  192.168 .3 .101: 5000/mall-tiny/mall-tiny-docker-plugin: 0.0 .1-SNAPSHOT
  • 启动成功后,可以成功访问到SpringBoot应用的Swagger页面,访问地址:http://192.168.3.101:8080/swagger-ui.html

总结

SpringBoot官方Maven插件避免了编写Docker镜像构建过程,同时充分利用了SpringBoot 2.3以后的Jar分层技术,但对于需要自定义构建镜像的场景造成了一定的麻烦。

参考资料

官方文档:https://docs.spring.io/spring-boot/docs/2.4.0/maven-plugin/reference/htmlsingle/#build-image

项目源码地址

https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-docker-plugin

推荐阅读


欢迎关注,点个在看


转载:https://blog.csdn.net/zhenghongcs/article/details/110848544
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场