如果你不知道怎么搭建本地仓库,请看:docker搭建私有仓库 - (jianshu.com)
docker的maven插件
目前比较流行的maven-docker插件的话.有几种.spotify和fabric8io, 目前spotify是使用比较多的,所以今天,我们也是使用这款插件来进行打包.
docker开启远程访问
$ vim /lib/systemd/system/docker.service
1999224-20220925113643860-1213945951.png
把这行修改为:ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
# 然后就刷新配置
systemctl daemon-reload
# 重启docker
systemctl restart docker
# 测试下 curl http://your's ip:2375/version。出现下面这些就说明成功了
$ curl http://192.168.245.133:2375/version
{"Platform":{"Name":""},"Components":[{"Name":"Engine","Version":"18.06.3-ce","Details":{"ApiVersion":"1.38","Arch":"amd64","BuildTime":"2019-02-20T02:28:17.000000000+00:00","Experimental":"false","GitCommit":"d7080c1","GoVersion":"go1.10.3","KernelVersion":"3.10.0-1160.el7.x86_64","MinAPIVersion":"1.12","Os":"linux"}}],"Version":"18.06.3-ce","ApiVersion":"1.38","MinAPIVersion":"1.12","GitCommit":"d7080c1","GoVersion":"go1.10.3","Os":"linux","Arch":"amd64","KernelVersion":"3.10.0-1160.el7.x86_64","BuildTime":"2019-02-20T02:28:17.000000000+00:00"}
maven添加docker插件
org.springframework.boot
spring-boot-maven-plugin
com.liufei.mall.MallApplication
repackage
com.spotify
docker-maven-plugin
1.1.0
192.168.245.133:5000
http://192.168.245.133:2375
192.168.245.133:5000/mall-tiny/${project.name}:${project.version}
${project.version}
java
true
true
8080
/
${project.build.directory}
${project.build.finalName}.jar
["java", "-jar","/${project.build.finalName}.jar"]
首先我们需要把springboot打包,打成jar包
点击package 或 install 打成jar包
1999224-20220925113643860-1213945951.png
然后点击docker:build
1999224-20220925115709734-749635191.png
构建成功后,去registry-web页面看下,我们刚才创建的镜像,就存在了
1999224-20220925115709734-749635191.png
我们看下docker:build中自动生成的Dockerfile文件
FROM java
ADD /mall-tiny-docker-plugin-1.0.jar //
EXPOSE 8080
ENTRYPOINT ["java", "-jar","/mall-tiny-docker-plugin-1.0.jar"]
就是上面这些内容
测试镜像
# 查看刚才生成的镜像
[root@192 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.245.133:5000/mall-tiny/mall-tiny-docker-plugin 1.0 674602b3b443 6 minutes ago 691MB
[root@192 mall-maven-plugin]# docker run -it --name mall -p 8080:8080 192.168.245.133:5000/mall-tiny/mall-tiny-docker-plugin:1.0
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.6.5)
2022-09-25 04:10:15.437 INFO 1 --- [ main] com.liufei.mall.MallApplication : Starting MallApplication v1.0 using Java 1.8.0_111 on 6d962666c801 with PID 1 (/mall-tiny-docker-plugin-1.0.jar started by root in /)
2022-09-25 04:10:15.458 INFO 1 --- [ main] com.liufei.mall.MallApplication : The following 1 profile is active: "prod"
2022-09-25 04:10:21.983 WARN 1 --- [ main] o.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in '[com.liufei.mall]' package. Please check your configuration.
2022-09-25 04:10:21.989 INFO 1 --- [ main] o.s.c.a.ConfigurationClassPostProcessor : Cannot enhance @Configuration bean definition 'myBatisConfig' since its singleton instance has been created too early. The typical cause is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as 'static'.
2022-09-25 04:10:24.455 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-09-25 04:10:24.507 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-09-25 04:10:24.508 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.60]
2022-09-25 04:10:24.674 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/mall] : Initializing Spring embedded WebApplicationContext
2022-09-25 04:10:24.674 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 9026 ms
2022-09-25 04:10:26.244 INFO 1 --- [ main] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource
2022-09-25 04:10:26.736 INFO 1 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
2022-09-25 04:10:34.506 INFO 1 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path '/actuator'
2022-09-25 04:10:37.607 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '/mall'
2022-09-25 04:10:41.358 INFO 1 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/mall] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-09-25 04:10:41.358 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-09-25 04:10:41.432 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 74 ms
2022-09-25 04:10:48.415 INFO 1 --- [ main] com.liufei.mall.MallApplication : Started MallApplication in 34.464 seconds (JVM running for 37.818)
容器可以正常启动,就说明构建的镜像没有问题,如果有问题,自己去看下日志。
Q: 推送后是否覆盖已存在的标签镜,xml中有这个配置,我们看下现象?
true
我们再运行下docker:build
[root@192 mall-maven-plugin]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.245.133:5000/mall-tiny/mall-tiny-docker-plugin 1.0 674602b3b443 22 minutes ago 691MB
发现镜像没有更新。
这是因为jar包没有变化,这里没变化并不是代码和配置没更新,是因为每个jar包都有一个唯一的sha245.
要想镜像更新,此时我们重新mvn package 或 mvn install下,重新生成jar包
再运行下docker:build
[root@192 mall-maven-plugin]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.245.133:5000/mall-tiny/mall-tiny-docker-plugin 1.0 50763bda8f50 7 seconds ago 691MB
192.168.245.133:5000/mall-tiny/mall-tiny-docker-plugin 674602b3b443 25 minutes ago 691MB
此时就发现会生成一个新的tag=1.0的镜像,之前的镜像TAG=none了
绑定 mvn package
这样,maven package之后会直接将镜像推送到私有仓库
com.spotify
docker-maven-plugin
1.1.0
build-image
package
build
192.168.245.133:5000
http://192.168.245.133:2375
192.168.245.133:5000/mall-tiny/${project.name}:${project.version}
${project.version}
java
true
true
8080
/
${project.build.directory}
${project.build.finalName}.jar
["java", "-jar","/${project.build.finalName}.jar"]