Sub topic 10
Building images without a Dockerfile using buildpacks
This chapter assumes you can build a Spring Boot JAR with Maven or Gradle, and that you know what a container image is and how to run one with Docker. You should also have Docker (or a compatible daemon) running on your machine. The previous chapter covered writing a Dockerfile by hand. Here you will get an equivalent image from the build plugin alone.
What a buildpack is
A buildpack is a program that inspects your source or build output and decides how to turn it into a runnable image. Cloud Native Buildpacks (CNB) is the open specification behind this idea. The Spring Boot plugins use a CNB builder called Paketo to do the work.
You do not write image instructions. The builder detects a Java application, picks a JRE, sets memory options, and splits the JAR into layers. Your job is only to name the image and, when needed, adjust a few settings.
The trade-off is control. A Dockerfile shows every step. A buildpack hides the steps behind a tested default. For many teams that default is better than a hand-written file, because security patches to the base image and the JRE arrive by rebuilding with a newer builder.
Running the build
The Spring Boot plugins add a goal for this. You run it instead of docker build.
# Maven
./mvnw spring-boot:build-image -Dspring-boot.build-image.imageName=myorg/myapp:1.0.0
# Gradle
./gradlew bootBuildImage --imageName=myorg/myapp:1.0.0The goal does these things in order:
- It packages the executable JAR, as in the earlier chapters.
- It pulls the builder image and the run image if they are not cached.
- It starts the builder in a container and passes it your JAR.
- The builder detects the application type, adds a JRE and other layers, and exports an image.
- The image is stored in your local Docker daemon.
The first run is slow because it downloads several hundred megabytes. Later runs reuse cached layers and are much faster.
Run the result like any other image:
docker run --rm -p 8080:8080 \
-e SPRING_PROFILES_ACTIVE=prod \
-e DB_URL=jdbc:postgresql://db:5432/app \
-e DB_PASSWORD=change-me \
myorg/myapp:1.0.0The configuration approach from the externalized configuration chapter still applies. The image holds the JAR. The environment supplies the profile, the database URL, and the password.
How the build flows
What is inside the image
The Paketo Java buildpacks produce an image with several parts:
- A base operating system layer from the run image, which is kept small and updated by the builder maintainers.
- A JRE layer. The version follows your project's Java version, which the plugin reads from the build configuration.
- Your application, stored in layers so that dependencies and application classes are separate. Dependencies change rarely, so unchanged layers are reused when you push or pull.
- A launcher that starts the JVM with settings computed when the container starts.
The last point matters. The Paketo memory calculator reads the memory limit of the container at start-up and sets heap, metaspace, and stack sizes to fit. If you give the container 1 GB, the JVM options are different from those for 512 MB, and you did not edit the image. The next chapter covers JVM sizing in more detail. For now, note that the buildpack handles the first pass for you.
You can inspect the layers and metadata with the pack CLI or with Docker:
docker image inspect myorg/myapp:1.0.0 --format '{{ index .Config.Labels "io.buildpacks.build.metadata" }}'
docker history myorg/myapp:1.0.0The label lists the buildpacks that took part and the versions they installed. This is useful when you need to answer "which JRE is in production?" without starting the container.
Configuring the build
Defaults are enough to start. Real projects usually set a few options in the build file. Here is a Maven configuration.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<name>myorg/myapp:${project.version}</name>
<env>
<BP_JVM_VERSION>21</BP_JVM_VERSION>
</env>
</image>
</configuration>
</plugin>The Gradle equivalent uses the bootBuildImage task.
tasks.named("bootBuildImage") {
imageName = "myorg/myapp:${version}"
environment = ["BP_JVM_VERSION": "21"]
}Variables that start with BP_ are build-time settings read by the buildpacks. Variables that start with BPE_ are copied into the image as run-time environment variables. This difference is a common source of confusion, so the table lists the two kinds.
| Prefix | When it applies | Example | Effect |
|---|---|---|---|
| `BP_` | While the image is built | `BP_JVM_VERSION=21` | Chooses the JRE version installed in the image |
| `BPE_` | When the container runs | `BPE_DELIM_JAVA_TOOL_OPTIONS= -Xss512k` | Adds an environment variable to the image |
| none | Passed with `docker run -e` | `SPRING_PROFILES_ACTIVE=prod` | Normal run-time configuration |
Other options you will meet often:
builderandrunImageselect a different builder, for example a more minimal one.publishpushes the image directly to a registry instead of the local daemon.cleanCachediscards cached layers, which is useful when a build behaves strangely.tagsadds extra tags, such aslatest, in the same build.
Publishing to a registry
In a CI environment you often have no use for a local copy. With publish enabled, the plugin pushes straight to the registry, and you supply credentials in the plugin configuration.
./mvnw spring-boot:build-image \
-Dspring-boot.build-image.imageName=registry.example.com/myorg/myapp:1.0.0 \
-Dspring-boot.build-image.publish=true \
-Dspring-boot.build-image.publishRegistry.url=registry.example.com \
-Dspring-boot.build-image.publishRegistry.username="$REGISTRY_USER" \
-Dspring-boot.build-image.publishRegistry.password="$REGISTRY_PASSWORD"Read the credentials from CI secret variables, as shown. Never write them into the POM or the build script, since both are committed to version control.
Buildpacks compared with a Dockerfile
A short decision guide:
- Choose buildpacks when you want a sensible image with little maintenance, and your needs are the common ones: a JRE, layered JAR, and memory settings.
- Choose a Dockerfile when you need extra operating system packages, a specific base image mandated by your organization, or a nonstandard start command.
- You can mix them. Some teams use buildpacks for ordinary services and a Dockerfile for the few that need special handling.
Common errors and gotchas
The daemon is not reachable. The plugin talks to Docker through its socket. If the build fails with a connection error, start Docker, or set DOCKER_HOST to the correct address. With Podman or a remote daemon, you may also need the docker.host plugin options.
The build fails offline or behind a proxy. The plugin must download the builder and run images. In a restricted network, mirror them to an internal registry and set the builder and runImage options to those locations. Proxy settings for Docker do not always reach the builder container, so check the plugin documentation for the proxy options.
Wrong Java version. If the image has a different JRE from the one you built with, set BP_JVM_VERSION explicitly. A mismatch shows up as UnsupportedClassVersionError at start-up, because the classes were compiled for a newer Java than the image provides.
Image architecture mismatch. A build on an Apple silicon laptop produces an arm64 image by default, which will not run on an amd64 cluster. Build in CI on the target architecture, or set the builder platform for the target.
Stale results. Cached layers can hide a changed setting. Use the clean cache option once to confirm that the cache is the cause.
Reproducibility. Paketo creates images with a fixed creation date, which makes builds comparable, so docker images may show the image as created long ago. This is intended and is not a bug.
Checking the result
After a build, verify three things before you rely on the image:
- The container starts and responds on the health endpoint, for example
curl localhost:8080/actuator/health. - It runs as a non-root user. Paketo images do this by default, which you can confirm with
docker run --rm --entrypoint id myorg/myapp:1.0.0. - Stopping the container prints graceful shutdown messages, which shows that the SIGTERM signal reaches the JVM.
Exercise
A teammate builds an image with spring-boot:build-image on an Apple silicon laptop, pushes it, and the pod on the amd64 cluster fails to start with an "exec format error". What is the cause, and how would you fix it in the pipeline?
Show answer
The image was built for arm64, the laptop's architecture, and the cluster nodes need amd64. Run the image build in CI on an amd64 runner, or configure the builder platform for the target architecture, and then push that image.
Exercise
You need the image to use Java 21, and you want a JVM option added at run time. Which kind of variable do you use for each, and where do you set them?
Show answer
Use BP_JVM_VERSION=21 for the Java version, because it is read while the image is built. Use a BPE_ variable, such as BPE_DELIM_JAVA_TOOL_OPTIONS, for the run-time JVM option, because it is stored in the image as an environment variable. Both go in the image.env section of the Maven plugin or the environment map of bootBuildImage in Gradle.