archive2020

Smaller Docker rebuilds for Spring Boot 2.3 with layered jars

Spring Boot 2.3 can split a fat jar into layers. How to enable it, write the Dockerfile, and when buildpacks are the better choice.

2 min read#dev#java#spring-boot#docker

The simple Dockerfile for a Spring Boot app copies the fat jar into the image:

FROM adoptopenjdk:11-jre-hotspot
COPY target/app.jar /app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]

I used this for a long time. Every code change creates a new layer with the whole jar, often 50 MB or more, and most of it is dependencies that did not change. Pushes over my home connection were slow enough that I finally looked at the layer support in Spring Boot 2.3. The Spring team has a good overview in Creating Efficient Docker Images with Spring Boot 2.3.

Enable layers

In 2.3 layering must be switched on. With Maven:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
    <layers>
      <enabled>true</enabled>
    </layers>
  </configuration>
</plugin>

Build and list the layers:

./mvnw clean package
java -Djarmode=layertools -jar target/app.jar list
dependencies
spring-boot-loader
snapshot-dependencies
application

The order goes from “changes least” to “changes most”. Snapshot dependencies get their own layer because they change often.

The Dockerfile

FROM adoptopenjdk:11-jre-hotspot AS builder
WORKDIR /application
COPY target/*.jar application.jar
RUN java -Djarmode=layertools -jar application.jar extract

FROM adoptopenjdk:11-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"]

Each COPY is a Docker layer. When only your code changes, only the last layer changes. The dependency layer comes from the cache and is not pushed again.

The app is no longer a jar, so it starts with JarLauncher. My first try still had -jar in the entrypoint and of course failed. Also make sure target/ has only the jar you just built. A stale jar from an old build gives a confusing image.

Buildpacks

Spring Boot 2.3 can also build an image without a Dockerfile:

./mvnw spring-boot:build-image -Dspring-boot.build-image.imageName=example/app:1.0

This uses Cloud Native Buildpacks. It needs a running Docker daemon, and the first build downloads the builder images. You get good layering too, but less control over the base image. I would use it for a new project where nobody wants to maintain a Dockerfile. Where there is already a standard base image, or special packages are needed, I keep the Dockerfile.

Two more 2.3 settings

While I was in the Docker setup I also turned on server.shutdown=graceful. On SIGTERM the app stops taking new requests and finishes the running ones. And if you run on Kubernetes, 2.3 has liveness and readiness health groups for the probes (/actuator/health/liveness, /actuator/health/readiness). I do not use Kubernetes at home, so I have not tried those.