-
maven 插件支持
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <image> <name>afs.com/${project.artifactId}:v1</name> </image> </configuration> </plugin>
-
运行命令
mvn clean -Dmaven.test.skip spring-boot:build-image
-
问题
1. 网络问题 --> 升级 Spring Boot 到 2.4.1
-
安装
# RHEL/CentOS wget https://github.com/wagoodman/dive/releases/download/v0.9.2/dive_0.9.2_linux_amd64.deb sudo apt install ./dive_0.9.2_linux_amd64.deb # Ubantu/Debian wget https://github.com/wagoodman/dive/releases/download/v0.9.2/dive_0.9.2_linux_amd64.deb sudo apt install ./dive_0.9.2_linux_amd64.deb
-
使用
dive [image-tag]
-
maven 插件添加分层属性
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <image> <name>afs.com/${project.artifactId}:v1</name> </image> <layers> <enabled>true</enabled> </layers> </configuration> </plugin>
-
查看 jar 包的内部分层信息
[root@host]# java -Djarmode=layertools -jar [target-jar] list
dependencies
spring-boot-loader
snapshot-dependencies
application
- 编写分层的 Dockerfile
# the first stage of our build will extract the layers
FROM openjdk:8-jdk-alpine as builder
WORKDIR application
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
RUN java -Djarmode=layertools -jar app.jar extract
# the second stage of our build will copy the extracted layers
FROM openjdk:8-jdk-alpine
WORKDIR application
COPY --from=builder application/dependencies ./
COPY --from=builder application/spring-boot-loader ./
COPY --from=builder application/snapshot-dependencies ./
COPY --from=builder application/application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]
- dive 查看镜像层级信息
Creating Optimized Docker Images for a Spring Boot Application