Creating Efficient Docker Images with Spring Boot 2.3

Spring Boot 2.3 brings with it some interesting new features that can help you package up your Spring Boot application into Docker images.

The first problem with common docker techniques is that the jar file is not unpacked. There’s always a certain amount of overhead when running a fat jar, and in a containerized environment this can be noticeable. It’s generally best to unpack your jar and run in an exploded form.

The second issue with the file is that it isn’t very efficient if you frequently update your application. Docker images are built in layers, and in this case your application and all its dependencies are put into a single layer. Since you probably recompile your code more often than you upgrade the version of Spring Boot you use, it’s often better to separate things a bit more. If you put jar files in the layer before your application classes, Docker often only needs to change the very bottom layer and can pick others up from its cache.

Two new features are introduced in Spring Boot 2.3 to help improve on these existing techniques: buildpack support and layered jars.

If you’ve ever used an application platform such as Cloud Foundry or Heroku then you’ve probably used a buildpack, perhaps without even realizing it! Buildpacks are the part of the platform that takes your application and converts it into something that the platform can actually run. For example, Cloud Foundry’s Java buildpack will notice that you’re pushing a .jar file and automatically add a relevant JRE.

Until relatively recently buildpacks were tightly coupled to the platform and you couldn’t easily use them independently. Thankfully they’ve now broken free, and with Cloud Native Buildpacks you can use them to create Docker compatible images that you can run anywhere.

Spring Boot 2.3.0 includes buildpack support directly for both Maven and Gradle. This means you can just type a single command and quickly get a sensible image into your locally running Docker daemon. For Maven you can type mvn spring-boot:build-image, with Gradle it’s gradle bootBuildImage. The name of the published image will be your application name and the tag will be the version.

Building Docker images with Spring Boot plugin

The plugin uses a technology called Cloud Native Buildpacks (CNB), which is an abstraction on top of the Dockerfile providing a best-practice approach to building Docker images. From my testing the integration with this technology is seamless, automatically supplying the correct Java 17 JRE in the image based on my Gradle configuration.

Running the Spring Boot plugin

No additional configuration is required to build the image, as the plugin by default uses the project’s name and version for the image name. So we just need to run the bootBuildImage Gradle task.

Publishing images with the Spring Boot Gradle plugin

The plugin can also push an image to your Docker registry of choice.


References: buildpacks.io,  creating-docker-images-with-spring-boot-2-3-0

Comments

Popular posts from this blog

Debug Java Stream in Intellij Idea

CRUD Goes Even Easier With JPABuddy