Recently, we faced the task of launching spring boot 2 applications in a kubernetes cluster using a docker image. This problem is not new, quickly enough we found examples in Google and prepared our application. We were very surprised not to find the alpine image for jdk11 and hoped that slim would be small enough, but when we sent the image to the docker registry, we noticed that its size was almost 422 megabytes. Below is a description of how we reduced the docker image with our spring boot and java 11 to 144 megabytes.

Application

As we mentioned earlier, our application is built using spring boot 2 and is a REST API wrapper over a relational database (using @RepositoryRestResource). Our dependencies include:

The assembled jar file has a size of: 37.6 megabytes.

Dockerfile:

As a result of the assembly, we get an image of size: 422 mb according to the output of the docker images command. Interestingly, if to use the outdated 8-jdk-slim image, the size is reduced to 306 mb.

Attempt 1: Another Basic Image

The first logical step was an attempt to find a more lightweight image, preferably based on alpine. We scanned to the most popular Java repositories:

  • https://hub.docker.com/_/openjdk
  • https://hub.docker.com/r/adoptopenjdk/openjdk11
  • https://hub.docker.com/r/adoptopenjdk/openjdk11-openj9
  • https://hub.docker.com/r/adoptopenjdk/openjdk8

(11 as the current LTS release and 8 as there are still a sufficient number of applications that could not migrate to more modern versions)

A table with some images and tags (~ 2700), their sizes at the time of writing:

 

Thus, if you change the base image to adoptopenjdk / openjdk11: alpine-jre, you can reduce the image with the application to 177 mb.

Attempt 2: Custom runtime

Since the moment of jdk9 release and modularization, it became possible to build your own runtime that contains only those modules that are necessary for your application.

Let’s try to determine the necessary modules for the test spring boot application:

Ok, it seems that jdeps cannot handle the fat-jar created with spring boot, but we can unzip the archive and write the classpath:

 

About this case, is currently open a bug: https://bugs.openjdk.java.net/browse/JDK-8207162

We tried downloading jdk12 to get this information, but ran into the following error:

After a lot of tries and module search by ClassNotFoundException, we determined that our application needs the following modules:

  • java.base
  • java.logging
  • java.sql
  • java.naming
  • java.management
  • java.instrument
  • java.desktop
  • java.security.jgss

Runtime for them can be collected using:

Let’s try to build a basic docker image using this modules:

and collect it:

As a result, the size was 106 megabytes, which is much smaller than most found basic images with openjdk. If you use it for our application, then the resulting size will be 144 megabytes.

Further, we can use spring-boot-runtime: openjdk-11-slim as the base image for all spring boot applications if they have similar dependencies. In the case of various dependencies, it is possible to use a multistage image assembly for each of the applications where java runtime will be assembled in the first stage, and the archive with the application will be added in the second.

Summary

Currently, most docker images for java have a large enough volume, which can negatively affect the start time of the application, especially if the necessary layers are not yet on the server. Using tags with jre or java modularization, you can build your own runtime, which will significantly reduce the size of the application image.