Sub topic 7
Building a native container image
This chapter assumes you can build Spring Boot projects with Maven or Gradle, and that you have a container runtime such as Docker installed. It also assumes you have seen native:compile and know that a native executable is built from your code ahead of time.
Why build the image with buildpacks
native:compile needs a local GraalVM installation. It also produces an executable for your own operating system, which is usually not the Linux system inside your container. Spring Boot's image build avoids both problems. It uses Cloud Native Buildpacks, which run the whole native build inside a container. You need only a container runtime on your machine.
The result is an OCI image (the standard container image format) that holds the native executable and a minimal operating system layer. It contains no JVM.
Building with Maven
With a project that inherits from spring-boot-starter-parent, activate the native profile and run the spring-boot:build-image goal. Run the second command for Gradle:
./mvnw -Pnative spring-boot:build-imageWith the org.graalvm.buildtools.native plugin applied, Gradle uses its own task:
./gradlew bootBuildImageThe Spring Boot plugin detects the GraalVM plugin and switches the buildpack into native mode. It does this by setting the BP_NATIVE_IMAGE environment variable for you. You can pass your own buildpack settings, such as extra arguments for native-image, through the plugin's environment configuration.
What happens during the build
The plugin first runs Spring AOT processing on your machine. It then starts a builder container, which holds a GraalVM distribution. That container runs native-image and packages the executable into the final image. The image appears in your local Docker daemon, named after the artifact ID and version, for example demo:0.0.1-SNAPSHOT.
Running the image
docker run --rm -p 8080:8080 demo:0.0.1-SNAPSHOTLook at the startup log line. A native Spring Boot application typically reports a start time of tens of milliseconds, against seconds on a JVM. The exact number depends on your application.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Cannot connect to the Docker daemon | The container runtime is not running, or the socket is not reachable | Start Docker, or configure the plugin's Docker host settings |
| Build is killed or fails with exit code 137 | The container ran out of memory | Raise the memory limit in Docker Desktop's settings |
| Image runs on your laptop but not on the server | CPU architecture mismatch, for example an `arm64` image built on Apple Silicon and run on `amd64` | Build on the target architecture, for example in CI |
| Build succeeds but the app fails at startup | Missing reflection or resource metadata | See the chapters on runtime hints and troubleshooting |
Image size and contents
A native image is usually smaller than a JVM-based image, because it carries no JDK. It still contains the base operating system layers the builder chooses, so its size depends on that builder. The image is not portable between architectures, and you cannot attach a debugger or a profiler the way you would on a JVM.
Exercise
You run ./mvnw -Pnative spring-boot:build-image on an Apple Silicon laptop, and the resulting image fails to start on an x86-64 Linux server with an exec format error. What caused this, and how do you fix it?
Show answer
The buildpack compiled a native executable for the CPU architecture of the build machine, which is arm64. A native executable is machine code for one architecture, so it cannot run on x86-64. Build the image on an x86-64 machine, for example a CI runner, or use a builder setup that targets the server's architecture.