docker-maven-plugin 与 dockerfile-maven-plugin 对springboot自动打包docker镜像并推送harbor的区别
1.环境说明
harbor在服务器192.168.2.125 未开启https校验;
idea打包使用本地docker打包引擎(docker-desktop(windows安装需要开启hype-v等)),不采用远程2375端口通信;
下面是目录结构
2.老插件
docker-maven-plugin插件1.0.0、1.2.2都会提示Failed to push 192.168.2.125/product-sim2/cybereng-asset:1.0.0, retrying in 10 seconds (1/5).换成1.1.0就正常
<plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.1.0</version> <executions> <execution> <id>build-image</id> <phase>package</phase> <goals> <goal>build</goal> </goals> </execution> <execution> <id>push-image</id> <phase>package</phase> <goals> <goal>push</goal> </goals> <configuration> <imageName> ${docker.repository}/${docker.registry.name}/${project.artifactId}:${project.version} </imageName> </configuration> </execution> </executions> <configuration> <serverId>192.168.2.125</serverId> <dockerDirectory>${project.build.directory}/docker</dockerDirectory> <imageName> ${docker.repository}/${docker.registry.name}/${project.artifactId} </imageName> <imageTags> <!--docker的tag为项目版本号、latest--> <imageTag>${project.version}</imageTag> </imageTags> <resources> <rescource><!-- 将打包文件放入dockerDirectory指定的位置 --> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </rescource> </resources> </configuration> </plugin>
dockerfile文件
#FROM openjdk:8-jre-slim FROM 192.168.2.125/product-sim2/openjdk/8-jdk-stretch-po-fonts # 环境变量 ENV WORK_PATH /home/project/fawkes ENV APP_NAME @project.build.finalName@.@project.packaging@ ENV APP_VERSION @project.version@ EXPOSE 11401 #USER #USER user:group #VOLUME VOLUME ["/home/project", "/tmp/data"] #ADD #COPY COPY $APP_NAME $WORK_PATH/ #LABEL #STOPSIGNAL #ARG #ONBUILD # WORKDIR WORKDIR $WORK_PATH RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime RUN echo 'Asia/Shanghai' >/etc/timezone # ENTRYPOINT ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom"] CMD ["-jar", "-Xmx1024m", "@project.build.finalName@.@project.packaging@"] #CMD ls -ll
老的方式,docker的配置文件在整个项目下,可以访问到@project.build.finalName@.@project.packaging@变量非常方便。
命令mvn clean package -DskipTests docker:build docker:push -s D:\program\apache-maven-3.8.1\conf\settings_zfyz.xml
最终的docker目录
3.新插件
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.13</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<useMavenSettingsForAuth>
true
</useMavenSettingsForAuth>
<repository>${docker.repository}/${docker.registry.name}/${project.artifactId}</repository>
<dockerfile>Dockerfile</dockerfile>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>${project.build.finalName}.${project.packaging}</JAR_FILE>
<JAR_NAME>${project.build.finalName}.${project.packaging}</JAR_NAME>
<JAR_VERSION>${project.version}</JAR_VERSION>
</buildArgs>
</configuration>
</plugin>
dockerfile
#FROM openjdk:8-jre-slim FROM 192.168.2.125/product-sim2/openjdk/8-jdk-stretch-po-fonts ARG JAR_NAME ARG JAR_VERSION # 环境变量 ENV WORK_PATH /home/project/fawkes ENV APP_NAME $JAR_NAME ENV APP_VERSION $JAR_VERSION EXPOSE 11401 #USER #USER user:group #VOLUME VOLUME ["/home/project", "/tmp/data"] #ADD #COPY COPY target/$JAR_FILE $WORK_PATH/$JAR_FILE #LABEL #STOPSIGNAL #ARG #ONBUILD # WORKDIR WORKDIR $WORK_PATH RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime RUN echo 'Asia/Shanghai' >/etc/timezone # ENTRYPOINT CMD ["sh", "-c", "java -Djava.security.egd=file:/dev/./urandom -jar -Xmx1024m ${APP_NAME}"] #CMD ls -ll
因为dockerfile文件必须在项目根目录下,无法引用@project.build.finalName@,最后通过docker环境变量+sh命令的方式来传递参数
命令mvn clean package -DskipTests dockerfile:build dockerfile:push -s D:\program\apache-maven-3.8.1\conf\settings_zfyz.xml
最终的docker目录