经过一番折腾之后,还是觉得老老实实的写Dockerfile更香,Spring Boot 2.3.1layer jars的支持与2.3.0有所变化,网上的资料大部分都是过时了的,正确步骤如下:

  1. pom里开启layer jars支持

    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
    <layers>
    <enabled>true</enabled>
    </layers>
    </configuration>
    </plugin>
    </plugins>
    </build>
  2. 编写Dockerfile,下面是一个简单的例子

    FROM adoptopenjdk:8-jre-hotspot as builder
    WORKDIR application
    ARG JAR_FILE=target/*.jar
    COPY ${JAR_FILE} application.jar
    RUN java -Djarmode=layertools -jar application.jar extract
    FROM adoptopenjdk:8-jre-hotspot
    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"]
  3. 最后编译即可。

    docker build . -t tomu:v0.0.1