Skip to content

Containerizing Spring Boot with Cloud Native Buildpacks

Cloud Native Buildpacks turn application source code into a container image without a Dockerfile. They detect the language and build tooling automatically, provision a JVM, manage layers and dependencies, and produce an optimized, reproducible OCI image. You keep build logic in your project instead of maintaining hand-written Dockerfiles.

Spring Boot integrates Cloud Native Buildpacks directly into its build plugins, so you can containerize an application with a single command. By default the Spring Boot builder uses Liberica JDK as the runtime, which means the images you produce already run on a verified, fully OpenJDK-compatible JVM.

This guide shows how to build an OCI image for a Spring Boot application, how to customize the JVM and base image, and how to run the result.

Before you start, make sure you have:

  • A Spring Boot project built with Maven or Gradle.
  • A running container daemon (Docker, or a compatible alternative such as Podman) that the build can talk to.

Spring Boot’s build plugins ship with buildpack support out of the box. There is nothing extra to add for a standard build.

The spring-boot-maven-plugin is already present in a project generated from start.spring.io:

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

Build the image with the build-image goal:

Terminal window
mvn spring-boot:build-image

When the build finishes, the buildpack reports the name of the image it created. By default the name is derived from the project’s artifact ID and version, for example docker.io/library/buildpacks-demo:0.0.1-SNAPSHOT.

Run the generated OCI image like any other container. Publish the application port and, optionally, set a memory limit so the JVM can size its heap to the container:

Terminal window
docker run --rm -p 8080:8080 -m 1g docker.io/library/buildpacks-demo:0.0.1-SNAPSHOT

The application starts on the JVM provided by the buildpack and is reachable on the published port.

Buildpacks are configured through environment variables passed at build time. The BP_JVM_VERSION variable selects the major Java version installed into the image.

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<env>
<BP_JVM_VERSION>21</BP_JVM_VERSION>
</env>
</image>
</configuration>
</plugin>

Other useful build-time variables:

  • BP_JVM_VERSION — the major Java version to install (for example 17, 21).
  • BP_JVM_TYPE — whether to install a JDK or a JRE.
  • BP_SPRING_AOT_ENABLED — enable Spring ahead-of-time processing.
  • BP_JVM_CDS_ENABLED — enable Class Data Sharing for faster startup.

To pass runtime JVM flags into the image, set JAVA_TOOL_OPTIONS. For example, to select the Z Garbage Collector:

<image>
<env>
<JAVA_TOOL_OPTIONS>-XX:+UseZGC</JAVA_TOOL_OPTIONS>
</env>
</image>

The default Spring Boot builder already runs on Liberica JDK. To produce smaller images built on a musl libc base, point the build at the BellSoft Alpaquita builder image. It pairs Liberica JDK with the lightweight Alpaquita Linux base.

<configuration>
<image>
<builder>bellsoft/buildpacks.builder:musl</builder>
</image>
</configuration>

The builder image determines both the build environment and the base of the final runtime image, so switching to the Alpaquita builder changes the JVM and operating system layers of the produced image without any other changes to your project.

If you have configured GraalVM Native Image support, buildpacks can produce a native executable image instead of a JVM image.

Add the GraalVM native build plugin and build with the native profile:

<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
Terminal window
mvn spring-boot:build-image -Pnative

The resulting image contains a native executable with a short startup time and a reduced memory footprint, and you run it with the same docker run command shown above. For a deeper walkthrough of native images, see the Liberica Native Image Kit documentation.

Download Liberica JDK builds for your platform from the Liberica JDK Download Center.