maven项目构建docker镜像并上传到harbor私服

windows版本安装docker地址https://www.cnblogs.com/wyt007/p/10656813.html

windows本地安装Docker Desktop,并且用idea或者eclipse集成docker插件

1,项目中编写Dockerfile文件,放在与项目同级目录下:

Dockerfile

 
  1. FROM java:8
  2. VOLUME /tmp
  3. ARG JAR_FILE=target/build/*.jar
  4. COPY ${JAR_FILE} spring-crm.jar
  5. COPY target/build/lib lib
  6. COPY target/build/resources resources
  7. ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Dloader.path=.,resources,lib","-jar","/spring-crm.jar","--spring.profiles.active=test"]

2,pom文件末尾添加:

<docker.repository>34.92.172.25:8888</docker.repository>  docker的harbor私服在的服务地址
<docker.registry.name>xhl</docker.registry.name>`  harbor私服项目名



<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<!--<skipDocker>true</skipDocker>-->
<!--<skipDockerBuild>true</skipDockerBuild>-->
<!--<skipDockerPush>true</skipDockerPush>-->
<!--<skipDockerTag>true</skipDockerTag>-->
<serverId>docker-registry</serverId>  指定docker私服在maven的settings配置的熟悉
<imageName>${docker.repository}/${docker.registry.name}/${docker.image.prefix}-${project.artifactId}</imageName>
<registryUrl>${docker.repository}</registryUrl>
<pushImage>true</pushImage>  push镜像到私服
<dockerDirectory>./</dockerDirectory>
<!--<dockerHost>http://34.92.172.25:2375</dockerHost>-->
<imageTags>
<!--docker的tag为项目版本号、latest-->
<imageTag>latest</imageTag>
</imageTags>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>


</configuration>
<executions>
 <execution>
  <id>docker-build</id>  docker的build操作
  <phase>package</phase>
 <goals>
  <goal>build</goal>
 </goals>
</execution>
 <execution>
 <id>tag-image</id>  给docker镜像打tag
 <phase>package</phase>
 <goals>
  <goal>tag</goal>
 </goals>
<configuration>
<image>${docker.repository}/${docker.registry.name}/${docker.image.prefix}-${project.artifactId}</image>
<newName>${docker.repository}/${docker.registry.name}/${docker.image.prefix}-${project.artifactId}</newName>
</configuration>
</execution>
<execution>
<id>push-image</id>  docker的push操作
<phase>deploy</phase>
<goals>
<goal>push</goal>
</goals>
<configuration>
<imageName>${docker.repository}/${docker.registry.name}/${docker.image.prefix}-${project.artifactId}</imageName>
</configuration>
</execution>
</executions>
</plugin>

3,生成docker镜像需要把Dockerfile文件和源文件放在一起

4,使用mvn clean,package就会自动打包生成docker镜像

5,docker images查看是否生成新的docker镜像

6,docker run -it -v /tmp/:/tmp/ -p 7088:7088 springboot-crm /bin/bash

docker详细文档见https://blog.csdn.net/weixin_40816738/article/details/90169007

maven的settings配置文件需要添加,配置docker私服地址访问的用户名和密码以及邮件

 
  1. <server>
  2. <id>docker-registry</id>
  3. <username>xxx</username>
  4. <password>xxx</password>
  5. <configuration>
  6. <email>xxx@qq.com</email>
  7. </configuration>
  8. </server>

除了命令方式,还有直接通过k8s命令运行镜像

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值