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
Feb 6, 2026
Backend Developer Roadmap 2026: What You Need to Know

Backend complexity keeps growing, and frameworks can't keep up. In 2026, knowing React or Django isn't enough. You need fundamentals that hold up when systems break, traffic spikes, or your architecture gets rewritten for the third time.I've been building production systems for 15 years. This roadmap covers three areas that separate people who know frameworks from people who can actually architect backend systems: data, architecture, and infrastructure. This is about how to think, not what tools to install.

Videos
card image
Jan 29, 2026
JDBC Connection Pools in Microservices. Why They Break Down (and What to Do Instead)

In this livestream, Catherine is joined by Rogerio Robetti, the founder of Open J Proxy, to discuss why traditional JDBC connection pools break down when teams migrate to microservices, and what is a more efficient and reliable approach to organizing database access with microservice architecture.

Further watching

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.

Videos
card image
Feb 18, 2026
Build Typed AI Agents in Java with Embabel

Most Java AI demos stop at prompt loops. That doesn't scale in production. In this video, we integrate Embabel into an existing Spring Boot application and build a multi-step, goal-driven agent for incident triage. Instead of manually orchestrating prompt → tool → prompt cycles, we define typed actions and let the agent plan across deterministic and LLM-powered steps. We parse structured input with Ollama, query MongoDB deterministically, classify risk using explicit thresholds, rank affected implants, generate a constrained root cause hypothesis, and produce a bounded containment plan. LLM handles reasoning. Java enforces rules. This is about controlled AI workflows on the JVM — not prompt glue code.

Videos
card image
Feb 12, 2026
Spring Data MongoDB: From Repositories to Aggregations

Spring Data MongoDB breaks down fast once CRUD meets production—real queries, actual data volumes, analytics. What looks simple at first quickly turns into unreadable repository methods, overfetching, and slow queries. In this video, I walk through building a production-style Spring Boot application using Spring Data MongoDB — starting with basic setup and repositories, then moving into indexing, projections, custom queries, and aggregation pipelines. You'll see how MongoDB's document model changes data design compared to SQL, when embedding helps, and when it becomes a liability. We cover where repository method naming stops scaling, how to use @Query safely, when to switch to MongoTemplate, and how to reduce payload size with projections and DTOs. Finally, we implement real MongoDB aggregations to calculate analytics directly in the database and test everything against a real MongoDB instance using Testcontainers. This is not another MongoDB overview. It's a practical guide to actually using Spring Data MongoDB in production without fighting the database.