Tuesday, May 6, 2025

Optimizing SpringBoot App Deployment with Layers

Stop copying around your dependencies over and over again!!  Most of the time only the application classes change.  Rebuilds should only push the changes! 

Sample docker file for layered extraction of java libraries.  What this does:

  1. extracts your fat spring-boot-generated JAR into separate layer directories
  2. from here, creates a docker image layer by layer
  3. typically only the application/ docker layer will have changed - the rest will load from docker cache

FROM openjdk:17.0.1-jdk-slim as builder

WORKDIR application
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=tools -jar application.jar extract --layers --launcher

FROM openjdk:17.0.1-jdk-slim
WORKDIR application
COPY --from=builder /application/application/dependencies/ ./
COPY --from=builder /application/application/spring-boot-loader/ ./
COPY --from=builder /application/application/snapshot-dependencies/ ./
COPY --from=builder /application/application/application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]