Dockerize Spring Boot Wisely: 6 tips to improve the container images of your Spring Boot apps

 

Transcript

Your Spring Boot applications deserve a top-notch package! And so in this video, I will give you six tips to improving the container images for your Spring Boot apps. You may already be using some of them. Some of them may be new to you. But the best thing about all of these solutions is that they are compatible with any application. Plus, they are super easy to implement, so you can try them out right away.

Use Docker multi-stage builds

Each command in a Docker file creates a new layer. If all of this layers are added to the final image, it can get bloated. To keep your final images clean, you can use Docker multi-stage builds. They enable you to use several FROM statements in a Docker file. Each FROM statement uses its own base image, and you can copy only those artifacts from the previous stage that you need. The final layer won't contain the layersfrom the parent images, tools required for building the application, or files that weren't explicitly copied. As a result, the final image will be smaller.

Without multi-stage build:

FROM bellsoft/liberica-runtime-container:jdk-21-stream-musl as builder

WORKDIR /app

ADD spring-petclinic-main /app/spring-petclinic-main

RUN cd spring-petclinic-main && ./mvnw clean package

EXPOSE 8081

ENTRYPOINT java -jar /app/spring-petclinic-main/target/spring-petclinic*.jar

Resulting container size: 368MB

With multi-stage build:

FROM bellsoft/liberica-runtime-container:jdk-21-stream-musl as builder

WORKDIR /app

ADD spring-petclinic-main /app/spring-petclinic-main

RUN cd spring-petclinic-main && ./mvnw clean package

FROM bellsoft/liberica-runtime-container:jre-21-slim-musl

WORKDIR /app

EXPOSE 8081

ENTRYPOINT ["java", "-jar", "-XX:MaxRAMPercentage=80.0", "/app/petclinic.jar"]

COPY --from=builder /app/spring-petclinic-main/target/*.jar /app/petclinic.jar

Resulting container size: 195MB

Choose a small base image

Reducing the size of container images will help you to reduce the drive space and network traffic in your clusters. So the easiest way to cut down the size of your container images is to use a base image based on a minimalistic Linux distribution. There are several popular Linux distributions for the cloud, and their size may vary. For example, let's compare the size of two container images. One of them is based on Liberica JDK Standard and Debian. Liberica JDK is recommended by Spring, so I will be using it as an example.

The container image with Debian:

FROM bellsoft/liberica-openjdk-debian:21 as builder

WORKDIR /app

ADD spring-petclinic-main /app/spring-petclinic-main

RUN cd spring-petclinic-main && ./mvnw clean package

FROM bellsoft/liberica-openjre-debian:21

WORKDIR /app

EXPOSE 8081

ENTRYPOINT ["java", "-jar", "-XX:MaxRAMPercentage=80.0", "/app/petclinic.jar"]

COPY --from=builder /app/spring-petclinic-main/target/*.jar /app/petclinic.jar

The container image with Debian takes 386MB.

And now let's use Liberica Runtime Container based on Liberica JDK Lite optimized for the cloud and minimalistic Alpaquita Linux.

The container image with Liberica JDK Lite and Alpaquita Linux:

FROM bellsoft/liberica-runtime-container:jdk-21-stream-musl as builder

WORKDIR /app

ADD spring-petclinic-main /app/spring-petclinic-main

RUN cd spring-petclinic-main && ./mvnw clean package

FROM bellsoft/liberica-runtime-container:jre-21-slim-musl

WORKDIR /app

EXPOSE 8081

ENTRYPOINT ["java", "-jar", "-XX:MaxRAMPercentage=80.0", "/app/petclinic.jar"]

COPY --from=builder /app/spring-petclinic-main/target/*.jar /app/petclinic.jar

This container image takes 195MB, which is almost two times smaller than the first image.

Opt for a layered JAR for efficient caching

A traditional way of building Spring Boot container images is to use a fat jar Av fat jar contains the application with all of its dependencies. But each time we change the application, we need to build a new artifact. Plus, pulling the container images of the application with all of its dependencies may take time. Spring Boot makes it possible to create layered jars. In a layer jar the application and its dependencies are stored in different layers. The most frequently updated layers, such as the application layer, are placed on the top in the container image. So every time you introduce changes to the application, only this layer is changed and others are pulled from the cache, and so the updates will be faster.

FROM bellsoft/liberica-runtime-container:jdk-21-stream-musl as builder

WORKDIR /home/app

ADD spring-petclinic-main /home/app/spring-petclinic-main

RUN cd spring-petclinic-main && ./mvnw -Dmaven.test.skip=true clean package

FROM bellsoft/liberica-runtime-container:jdk-21-stream-musl as optimizer

WORKDIR /home/app

COPY --from=builder /home/app/spring-petclinic-main/target/*.jar petclinic.jar

RUN java -Djarmode=layertools -jar petclinic.jar extract

FROM bellsoft/liberica-runtime-container:jre-21-stream-musl

ENTRYPOINT ["java", "org.springframework.boot.loader.launch.JarLauncher"]

COPY --from=optimizer /home/app/dependencies/ ./

COPY --from=optimizer /home/app/spring-boot-loader/ ./

COPY --from=optimizer /home/app/snapshot-dependencies/ ./

COPY --from=optimizer /home/app/application/ ./

Upgrade the JDK and Spring Boot versions

Java Virtual machine, the heart of the Java platform, is getting more powerful with each JDK release. Brand new features, improved features, numerous enhancements to the garbage collection, JIT compiler, and the intrinsics can make your application run faster even without code changes. Spring Boot also gets numerous improvements and new features with each release. For instance, Spring Boot 3 includes baked-in support for GraalVM Native Image, Spring Boot 3.3 includes support for AppCDS for faster startup, and Spring Boot 3.4 offers a smaller base image for Buildpacks. These are only few examples of how your favorite framework can help you boost the performance, and reduce the memory footprint of your container images, so updating the JDK and Spring Boot versions is essential to keep up with the performance requirements for modern applications.

Enable AOT and CDS for faster startup

If application startup time is critical for you, consider using Application Class Data Sharing and Ahead-of-time processing. AppCDS and AOT enable up to 50% faster startup of applications, and there are no need for code changes. Take Spring Petclinic, for example. It starts in a little bit more than 4 seconds on my machine in the container image, but with AppCDS and AOT enabled, it starts in less than 3 seconds. Note that the container with CDS enabled will be bigger than the usual one. Plus, if you need more drastic startup reduction, consider using GraalVM Native Image or Coordinated Restore at Checkpoint. Writing Dockerfiles is difficult and error-prone, and plus, maintaining them might be challenging. Besides, there is a risk of using outdated versions of base images if you don't update the Dockerfile on a regular basis. Buildpacks can help you solve these issues. They turn the application source code into a production-ready container image, and all you have to do is run just one command. And Buildpacks always use the latest version of software for base images, so you will always be up to date. And by the way, Buildpacks for Spring Boot automatically create layered JARs, so you don't have to do it manually. But of course you can configure Buildpacks, it is really easy to do. For instance, if you want to use AppCDS and AOT processing, all you have to do is to specify these two features in a configuration file, and that’s it! The Buildpacks will do everything for you.

FROM bellsoft/liberica-runtime-container:jdk-21-crac-cds-musl as builder

WORKDIR /home/app

ADD spring-petclinic /home/app/spring-petclinic-main

RUN cd spring-petclinic-main && ./mvnw -Dmaven.test.skip=true clean package

FROM bellsoft/liberica-runtime-container:jdk-21-cds-slim-musl as optimizer

WORKDIR /app

COPY --from=builder /home/app/spring-petclinic-main/target/*.jar petclinic.jar

RUN java -Djarmode=tools -jar petclinic.jar extract --layers --launcher

FROM bellsoft/liberica-runtime-container:jdk-21-cds-slim-musl

ENTRYPOINT ["java", "-Dspring.aot.enabled=true", "-XX:SharedArchiveFile=application.jsa", "org.springframework.boot.loader.launch.JarLauncher"]

COPY --from=optimizer /app/petclinic/dependencies/ ./

COPY --from=optimizer /app/petclinic/spring-boot-loader/ ./

COPY --from=optimizer /app/petclinic/snapshot-dependencies/ ./

COPY --from=optimizer /app/petclinic/application/ ./

RUN java -Dspring.aot.enabled=true \

-XX:ArchiveClassesAtExit=./application.jsa \

-Dspring.context.exit=onRefresh org.springframework.boot.loader.launch.JarLauncher

In this video, we’ve looked at different approaches for optimizing the Docker container images for Spring Boot applications. If this video was useful to you, don't forget to like it, subscribe to our channel. And until next time!

Summary

This video provides six tips for optimizing Docker container images for Spring Boot applications, including using Docker multi-stage builds to reduce image size, choosing minimal base images, and leveraging layered JARs for efficient caching. It also highlights the benefits of upgrading JDK and Spring Boot versions for better performance, enabling AOT and CDS for faster startup, and using Buildpacks to automate image creation and maintenance. By applying these strategies, developers can create leaner, more efficient, and high-performing Spring Boot container images.

About Catherine

Java developer passionate about Spring Boot. Writer. Developer Advocate at BellSoft

Social Media

Tags

Videos
card image
Mar 9, 2026
jOOQ Deep Dive: CTE, MULTISET, and SQL Pipelines

Some backend developers reach the point where the ORM stops being helpful. Complex joins, nested result graphs, or CTE pipelines quickly push frameworks like Hibernate to their limits. And when that happens, teams often end up writing fragile raw SQL strings or fighting performance issues like the classic N+1 query problem. In this video, we build a healthcare scheduling application NeonCare using jOOQ, Spring Boot 4, and PostgreSQL, and show how to write production-grade SQL directly in Java while keeping full compile-time type safety.

Videos
card image
Feb 27, 2026
Spring Developer Roadmap 2026: What You Need to Know

Spring Boot is powerful. But knowing the framework isn’t the same as understanding backend engineering. In this video, I walk through the roadmap I believe matters for a Spring developer in 2026. We start with data. That means real SQL — CTEs, window functions, normalization trade-offs — and understanding what ACID and BASE actually imply for system guarantees. Spring Data JPA is useful, but you still need to know what happens underneath. Then architecture: microservices vs modular monolith, serverless, CQRS, and when HTTP, gRPC, Kafka, or WebSockets make sense. Not as buzzwords — but as design choices with trade-offs. Security and infrastructure follow: OWASP Top 10, AuthN vs AuthZ, encryption in transit and at rest, Docker, Kubernetes, Infrastructure as Code, and observability with Micrometer, OpenTelemetry, and Grafana. This roadmap isn’t about mastering every tool. It’s about knowing what affects reliability in production.

Further watching

Videos
card image
Apr 2, 2026
Java Memory Options You Need in Production

JVM memory tuning can be tricky. Teams increase -Xmx and assume the problem is solved. Then the app still hits OOM. Because maximum heap size is not the only thing that affects memory footprint. The JVM uses RAM for much more than heap: metaspace, thread stacks, JIT/code cache, direct buffers, and native allocations. That’s why your process can run out of memory while heap still looks “fine”. In this video, we break down how JVM memory actually works and how to control it with a minimal, production-safe set of flags. We cover heap sizing (-Xms, -Xmx), dynamic resizing, direct memory (-XX:MaxDirectMemorySize), and total RAM limits (-XX:MaxRAMPercentage) — especially in containerized environments like Docker and Kubernetes. We also explain GC choices such as G1, ZGC, and Shenandoah, when defaults are enough, and why GC logging (-Xlog:gc*) is mandatory before tuning. Finally, we show how to diagnose failures with heap dumps and OOM hooks. This is not about adding more flags. It’s about understanding what actually consumes memory — and making decisions you can justify in production.

Videos
card image
Mar 26, 2026
Java Developer Roadmap 2026: From Basics to Production

Most Java roadmaps teach tools. This one teaches order — the only thing that actually gets you to production. You don’t need to learn everything. You need to learn the right things, in the right sequence. In this video, we break down a practical Java developer roadmap for 2026 — from syntax and OOP to Spring, databases, testing, and deployment. Structured into 8 levels, it shows how real engineers grow from fundamentals to production-ready systems. We cover what to learn and what to ignore: core Java, collections, streams, build tools, Git, SQL and JDBC before Hibernate, the Spring ecosystem, testing with JUnit, and deployment with Docker and CI/CD. You’ll also understand why most developers get stuck — jumping into frameworks too early, skipping SQL, or treating tools as knowledge. This roadmap gives you a clear path into real-world Java development — with priorities, trade-offs, and production context.

Videos
card image
Mar 19, 2026
TOP-5 Lightweight Linux Distributions for Containers

In this video, we compare five lightweight Linux distributions commonly used as base images: Alpine, Alpaquita, Chiseled Ubuntu, RHEL UBI Micro, and Wolfi. There are no rankings or recommendations — just a structured look at how these distros differ so you can evaluate them in your own context.