一、添加maven打包插件
<properties>
<!-- 依赖版本 -->
<spring-boot.version>2.3.3.RELEASE</spring-boot.version>
<!-- 应用所在的启动类 -->
<mainClass>com.business.BusinessApplication</mainClass>
<!-- 资源打包插件版本 -->
<maven-resources-plugin.version>3.1.0</maven-resources-plugin.version>
</properties>
<build>
<plugins>
...
<!-- springBoot 指定主类打包 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>${mainClass}</mainClass>
<executable>true</executable>
<fork>true</fork>
<!--
Enable the line below to have remote debugging of your application on port 5005
<jvmArguments>-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005</jvmArguments>
-->
</configuration>
</plugin>
...
</plugins>
</build>
二、添加docker镜像生成时相关文件
1、编写Dockerfile相关文件
# 这里为基础镜像,就是包含了jdk相关的docker镜像
FROM apline_openjdk8_fonts:latest
# 基本环境参数
ENV SPRING_OUTPUT_ANSI_ENABLED=ALWAYS \
APP_SLEEP=0 \
JAVA_OPTS="-Dfile.encoding=utf-8"
# Add a newuser user to run our application so that it doesn't need to run as root
RUN adduser -D -s /bin/sh newuser
WORKDIR /home/newuser
# 授权应用相关启动时的参数环境使用
ADD entrypoint.sh entrypoint.sh
RUN chmod 755 entrypoint.sh && chown newuser:newuser entrypoint.sh
USER newuser
# 将jar包添加到docker镜像中并且改名为app.jar
ADD *.jar app.jar
# docker启动时 需要执行的脚本
ENTRYPOINT ["./entrypoint.sh"]
EXPOSE 12001 5701/udp
2、编写entrypoint.sh脚本
#!/bin/sh
echo "The application will start in ${APP_SLEEP}s..." && sleep ${APP_SLEEP}
exec java ${JAVA_OPTS} -Djava.security.egd=file:/dev/./urandom -jar "${HOME}/app.jar" "$@"
3、在pom中指定需要maven构建时的相关资源
<build>
<plugins>
...
<!-- 资源打包插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>${maven-resources-plugin.version}</version>
<executions>
<execution>
<id>docker-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<!-- 输出目录 -->
<outputDirectory>target/</outputDirectory>
<resources>
<resource>
<!-- 资源目录 -->
<directory>src/main/docker/</directory>
<filtering>false</filtering>
<!-- 排除在外的资源 -->
<excludes>
<exclude>**/*.yml</exclude>
</excludes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
三、docker镜像生成,并且上传镜像到镜像仓库,继续为后面自动化部署做铺垫,这里的版本号可以使用时间戳之类的用于自动化部署
docker build -t harbor.cloud.com/test/app:test .
docker login -u admin -p Harbor12345 harbor.cloud.com
docker push harbor.cloud.com/test/app:test