Skip to content

Building Java applications with Cloud Native Buildpacks

BellSoft provides Cloud Native Buildpacks images that contain the latest versions of Liberica JDK Lite and Liberica NIK products specifically built and optimized for Alpaquita Linux. Alpaquita Linux is a full-featured operating system shipped in two libc variants based on two different libc implementations, namely glibc and musl. BellSoft provides images for both glibc and musl libc libraries.

Using Alpaquita Linux (musl) as a target image helps you create lightweight Docker images. For example, the size of a Docker image for a basic Spring Boot application running Java 21, if using standard paketobuildpacks/builder-jammy-tiny is about 250 MB, while using bellsoft/buildpacks.builder:musl saves about 140 MB.

Cloud Native Buildpacks (CNBs) transform your application source code into container images that can run on any cloud.

Liberica JDK Lite is a JDK optimized for cloud instances with a minimal footprint. It is a full-fledged, Java SE-compliant runtime but much smaller than any standard Java distribution. Liberica JDK Lite has a higher compression ratio for modules than a classic JDK, thus reducing static footprint. For more information, see Liberica JDK Lite in the Choosing Liberica JDK flavor document.

Liberica Native Image Kit is a utility capable of converting your JVM-based application into a fully compiled native executable ahead-of-time under the closed-world assumption with an almost instant startup time. It optimizes resource consumption, minimizes the static footprint, and works on various platforms, including lightweight musl-based Alpaquita Linux.

When building images from BellSoft buildpacks, you can choose one of the latest JDK releases of 8, 11, 17 (default), and 21 JDK versions; 22 and 23 NIK versions as well as Alpaquita Linux based on glibc and musl libraries. For the latest product release versions, see the following links:

If the size of the resulting image is important and the final application does not have native components that require glibc, then choose musl. If you need higher performance with a slight increase in size of the final image, it makes sense to consider using the glibc library. In addition, there might be other considerations when choosing C runtime, such as licensing.

Building a Java application with Alpaquita Linux

Section titled “Building a Java application with Alpaquita Linux”

To build a Java application for Alpaquita Linux with the default JDK version, run one of the following commands within the root of your project. Make sure you choose the bellsoft builder and specify the target C library.

Terminal window
pack build demo-app --builder bellsoft/buildpacks.builder:musl --path .

Where bellsoft/buildpacks.builder:<glibc/musl> is the BellSoft builder producing an Alpaquita Linux image with the specified C library. You can also specify BellSoft builder as the default one as follows:

Terminal window
pack config default-builder bellsoft/buildpacks.builder:musl

If you want to install a specific JVM feature version, use the BP_JVM_VERSION environment variable that accepts the following JDK version values: 8, 11, 17, 21 (default is 17). For example:

Terminal window
pack build demo-app --builder bellsoft/buildpacks.builder:glibc --path . -env BP_JVM_VERSION=21

To inspect the resulting image, check the stack, buildpacks participated in the build, etc. Run the following command:

Terminal window
pack inspect demo-app

You can also check the Linux distro inside your Docker image as follows:

Terminal window
docker run --rm -it --entrypoint /bin/cat demo-app /etc/os-release

Then you can check the image size as follows:

Terminal window
docker image ls demo-app

To run the application, use the following Docker command:

Terminal window
docker run --rm -it demo-app

Spring Boot’s Maven and Gradle plugins can build an OCI image directly, with no pack CLI installation. Point the plugin’s builder at the BellSoft Alpaquita builder to produce a musl- or glibc-based image. Combined with Liberica JDK Lite on Alpaquita’s minimal base, this typically cuts container memory use by up to 30 % compared with standard OpenJDK containers.

Configure the spring-boot-maven-plugin in pom.xml:

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<name>demo-app</name>
<builder>bellsoft/buildpacks.builder:musl</builder>
</image>
</configuration>
</plugin>
</plugins>
</build>

Build and run:

Terminal window
mvn spring-boot:build-image
docker run --rm -it demo-app

Use bellsoft/buildpacks.builder:glibc instead when you need glibc. The BP_* build variables described in Configuration options (for example BP_JVM_VERSION) work the same way — pass them through the plugin’s environment/env block.

The JRE inside the resulting image can be optimized in size by using the jlink tool. To enable jlink, set the environment variable BP_JVM_JLINK_ENABLED to true (default is false). In this case, jlink generates a custom JRE with the following default options: --no-man-pages, --no-header-files, --strip-debug, --compress=1. To change the options, use BP_JVM_JLINK_ARGS environment variable. Check the jlink documentation for further information.

For example, the following command creates a new application image on Alpaquita Linux with a custom JRE 21:

Terminal window
pack build demo-app --builder bellsoft/buildpacks.builder:musl --path . -env BP_JVM_VERSION=21 -env BP_JVM_JLINK_ENABLED=true

The resulting image is usually smaller than the one with the default JRE.

To build the native image application, set the BP_NATIVE_IMAGE environment variable to true as in the following example:

Terminal window
pack -v build demo-native-app --builder bellsoft/buildpacks.builder:musl --path . --env BP_NATIVE_IMAGE=true --env BP_JVM_VERSION=21

You can check the size of the resulting image as follows:

Terminal window
docker image ls demo-native-app

To run the application, use the following Docker command:

Terminal window
docker run --rm -it demo-native-app

The following table lists pack builder configuration options for BellSoft Liberica JDK Lite and Native Image Kit Cloud Native Buildpacks ver. 1.1.0.

VariableDefault valueDescription
$BP_JVM_JLINK_ARGS--no-man-pages, --no-header-files, --strip-debug, --compress=1Configures custom jlink arguments (--output must be omitted).
$BP_JVM_JLINK_ENABLEDfalseEnables the jlink tool to generate custom JRE.
$BP_JVM_TYPEJRESpecifies the JVM type - JDK or JRE.
$BP_JVM_VERSION17Specifies a Java version.
VariableDefault valueDescription
$BPL_DEBUG_ENABLEDfalseEnables Java remote debugging support.
$BPL_DEBUG_PORT8000Specifies the remote debugging port.
$BPL_DEBUG_SUSPENDfalseSpecifies whether to suspend execution until a debugger has attached.
$BPL_HEAP_DUMP_PATHN/ASpecifies the path to write heap dumps on error.
$BPL_JAVA_NMT_ENABLEDtrueEnables Java Native Memory Tracking (NMT).
$BPL_JAVA_NMT_LEVELsummaryConfigures the level of NMT, summary or detail.
$BPL_JFR_ARGSN/AConfigures custom Java Flight Recording (JFR) arguments.
$BPL_JFR_ENABLEDfalseEnables Java Flight Recorder (JFR).
$BPL_JMX_ENABLEDfalseEnables Java Management Extensions (JMX).
$BPL_JMX_PORT5000Specifies the JMX port.
$BPL_JVM_HEAD_ROOM0Configures the headroom in memory calculation.
$BPL_JVM_LOADED_CLASS_COUNT35% of classesSets the number of loaded classes in memory calculation.
$BPL_JVM_THREAD_COUNT250Sets the number of threads in memory calculation.
$JAVA_TOOL_OPTIONSN/ASpecifies the JVM launch flags.