Sub topic 7
Building a native container image
This chapter shows how to package a Spring Boot native executable as an OCI container image. It covers two routes: Cloud Native Buildpacks, driven by the build plugin, and a hand-written multi-stage Dockerfile.
It assumes you can build a Spring Boot project with Maven or Gradle and run a container with Docker. It also assumes the project is already set up for native builds, as in the earlier chapters. That means Spring Boot 3, the native profile in Maven or the org.graalvm.buildtools.native plugin in Gradle, and Java 17 or later.
Why a native executable needs its own packaging
native:compile produces an executable for the machine that ran it. On macOS or Windows that executable cannot run inside a Linux container. A container image therefore needs the native executable to be compiled on Linux, for the CPU architecture you will deploy to.
There are two ways to get that:
- Let Cloud Native Buildpacks compile the application inside a builder container and wrap the result in an image.
- Write a multi-stage Dockerfile that compiles in a GraalVM stage and copies the executable into a small runtime stage.
Both ways compile inside a Linux container. Neither needs GraalVM installed on your machine.
Route 1: Cloud Native Buildpacks
Cloud Native Buildpacks (CNB) turn source or build output into an image without a Dockerfile. The Spring Boot build plugins call a CNB builder through the Docker API. The builder contains the GraalVM compiler and a base operating system layer.
With Maven:
./mvnw -Pnative spring-boot:build-imageWith Gradle:
./gradlew bootBuildImageWhen the native build plugin is present, the Spring Boot plugin turns on the Paketo native-image buildpack by setting BP_NATIVE_IMAGE=true for you. The build then runs the same steps as a local native build, but inside the builder container:
- Spring AOT processing generates the bean definitions and hints.
native-imagecompiles the application into an executable.- The buildpack puts the executable in a minimal image.
You need only a running container runtime. You do not need a local GraalVM.
Run the result like any other image. By default the image is named after the project, in the form artifactId:version:
docker run --rm -p 8080:8080 demo:0.0.1-SNAPSHOTThe startup log should report a start time measured in tens of milliseconds. A JVM start of the same application usually takes seconds.
Configuring the image
Set the image name, the builder, and environment variables for the buildpack in the plugin configuration. In Maven:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<name>registry.example.com/team/demo:${project.version}</name>
<env>
<BP_NATIVE_IMAGE_BUILD_ARGUMENTS>-Os</BP_NATIVE_IMAGE_BUILD_ARGUMENTS>
</env>
</image>
</configuration>
</plugin>The Gradle Kotlin DSL equivalent:
tasks.named<org.springframework.boot.gradle.tasks.bundling.BootBuildImage>("bootBuildImage") {
imageName.set("registry.example.com/team/demo:${project.version}")
environment.put("BP_NATIVE_IMAGE_BUILD_ARGUMENTS", "-Os")
}BP_NATIVE_IMAGE_BUILD_ARGUMENTS appends extra arguments to the native-image command. Here -Os optimizes for size. Other buildpack variables you may need:
| Variable | Purpose |
|---|---|
| `BP_NATIVE_IMAGE` | Switches the buildpack between a JVM image and a native image. The Spring Boot plugin sets it for you. |
| `BP_NATIVE_IMAGE_BUILD_ARGUMENTS` | Extra arguments for `native-image`. |
| `BP_JVM_VERSION` | Selects the JDK version used by the builder. |
You can also publish directly with -Dspring-boot.build-image.publish=true on Maven or --publishImage on Gradle. That needs registry credentials in the plugin configuration.
Builders and base images
The builder determines the base operating system and which libraries the final image contains. Spring Boot picks a default Paketo builder, and which one depends on your Spring Boot version. Check the documentation for your version before relying on it. Tiny builders produce small images with almost nothing inside. A tiny image has no shell and no package manager, so you cannot docker exec into it to debug.
You can pick a builder explicitly with <builder> in Maven or builder.set(...) in Gradle. Choose a larger builder if the application needs shared libraries that the tiny base does not include.
Resource needs and platform
A native build is heavy. Plan for the following:
- Memory. The
native-imagestep often needs several gigabytes. Docker Desktop's default memory limit can be too low, and the build fails with an out-of-memory or "killed" message. Raise the limit, commonly to 8 GB or more. - Time. Builds take minutes, not seconds. Use JVM builds for the inner development loop.
- Architecture. An image built on an Apple Silicon laptop is
arm64. Deploying it to anamd64cluster fails at start. Build on the architecture you deploy to, usually in CI.
Route 2: A multi-stage Dockerfile
Use a Dockerfile when you want control of the base image, for example to match a company standard. The pattern is two stages. The first stage compiles the executable. The second stage copies only the executable.
FROM ghcr.io/graalvm/native-image-community:21 AS build
WORKDIR /workspace
COPY .mvn/ .mvn/
COPY mvnw pom.xml ./
COPY src/ src/
RUN ./mvnw -Pnative -DskipTests native:compile
FROM debian:bookworm-slim
WORKDIR /app
COPY --from=build /workspace/target/demo /app/demo
EXPOSE 8080
ENTRYPOINT ["/app/demo"]Build and run it:
docker build -t demo-native .
docker run --rm -p 8080:8080 demo-nativeSome details matter here:
- The file name
/workspace/target/demoassumes the project'sartifactIdisdemo. Adjust it to your project. - The runtime base image must provide the shared libraries the executable links against. By default
native-imageproduces a dynamically linked executable that needs the C library (glibc). Compile against a glibc no newer than the runtime image has. A runtime image with an older glibc than the build stage will fail with aGLIBC_x.y not founderror. - A fully static or "mostly static" executable can run on very small bases such as
scratch. That needs extranative-imageoptions, and a static glibc build has its own limits, such as DNS resolution. Treat it as an optimization for later.
Choosing between the routes
| Question | Buildpacks | Dockerfile |
|---|---|---|
| Setup effort | One command | You write and maintain the file |
| Base image control | Limited to available builders | Full |
| Image contents | Minimal, with a non-root user by default | Whatever you choose |
| Reproducibility | Builder versions are managed for you | You pin versions yourself |
| Layer caching | Handled by the buildpack | You must order `COPY` steps well |
Buildpacks are a good default. Move to a Dockerfile when you need a specific base image, extra system packages, or custom build steps.
Common problems
- The build is killed with no clear error. The container ran out of memory. Increase the Docker memory limit.
- The image starts and then fails with an
exec format error. The image architecture does not match the host. Rebuild on the right platform. GLIBC_x.y not foundat start. The runtime base is older than the build stage. Use a newer runtime base.- The application works as a JVM but fails in the container. A reflection, resource, or proxy access was not registered. The native image will report errors such as
ClassNotFoundExceptionor a missing resource at runtime. Register runtime hints. The troubleshooting chapter covers this in detail. - You cannot open a shell in the container. Tiny images have no shell. Use logs, actuator endpoints, or a debug image.
Exercise
A teammate builds a native executable with ./mvnw -Pnative native:compile on a Windows laptop and copies target/demo.exe into a debian:bookworm-slim image. The container fails to start. Why, and what are two ways to fix it?
Show answer
native-image produces an executable for the operating system and CPU of the machine that ran it, so the Windows executable cannot run on Linux. Fix 1: run ./mvnw -Pnative spring-boot:build-image, so Cloud Native Buildpacks compile inside a Linux builder container. Fix 2: use a multi-stage Dockerfile whose first stage runs native:compile in a Linux GraalVM image, and copy the resulting Linux executable into the runtime stage. In both cases, build for the CPU architecture you will deploy to.