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 Docker (or a compatible daemon) runs on your machine. You should also know what a container image is, from the previous chapter on Dockerfiles.
What buildpacks do
A Cloud Native Buildpack is a tool that inspects your project and turns it into an OCI image. An OCI image is a container image that follows the Open Container Initiative format, so Docker, Kubernetes, and other runtimes can run it. You do not write a Dockerfile. The Spring Boot plugins call a builder image that detects a Java application, chooses a JRE, and arranges the JAR into layers.
The Dockerfile from the last chapter fixed three things by hand: the base image, the non-root user, and the file layout. A buildpack makes those choices for you, and it applies them the same way to every project.
Building an image
Both plugins include a goal for this. You do not need to add any dependency.
# 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 first run is slow. The plugin pulls the builder image and the run image from a registry, and these are large. Later builds reuse them from the local Docker daemon, so they finish 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 still comes from outside the image, as in the earlier chapter on externalized configuration.
How the build flows
The plugin packages the JAR, then passes it to the builder. The builder detects a Spring Boot JAR, adds a JRE, and writes layers. The image lands in your local Docker daemon. Pushing it to a registry is a separate step.
Configuring the build
You can adjust the build in the plugin configuration or on the command line. Common options are the image name, the Java version, and environment variables for the build process.
<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>BP_JVM_VERSION is a build-time variable read by the Paketo Java buildpack, which is the default builder's Java support. Variables that start with BP_ affect the build. Variables that start with BPL_ affect the running container.
Dockerfile or buildpack
| Concern | Dockerfile | Buildpack |
|---|---|---|
| Control over the image | Full | Limited to buildpack options |
| Maintenance | You update base images and scripts | The builder supplies patched layers |
| Consistency across teams | Depends on each file | Same builder, same result |
| Build speed on first run | Fast | Slow, due to downloads |
| Extra tooling | Docker only | Docker and a builder image |
Buildpacks suit teams with many services that want the same secure defaults. A Dockerfile suits cases that need unusual system packages or a custom entrypoint.
Common errors
- Cannot connect to the Docker daemon. The plugin needs a running daemon. Start Docker, or set the
DOCKER_HOSTenvironment variable to point at your daemon. - Pull failures in CI. The builder image comes from a public registry, so a locked-down network blocks the build. Mirror the builder to an internal registry and set the builder name in the plugin configuration.
- Image runs the wrong Java version. Set
BP_JVM_VERSIONexplicitly. Otherwise the buildpack uses its own default, which may not match your compiler target.
Exercise
You build an image with bootBuildImage and want to publish it from CI. The build log shows a very slow first run, then fast runs after that. Why, and what would you change so every CI run is fast?
Show answer
The first run downloads the builder and run images. Later runs reuse them from the local Docker daemon cache. A fresh CI runner has an empty cache, so each run is slow. Cache the Docker images between runs, or mirror the builder to a registry close to the runner.
Exercise
Which prefix marks a variable that changes how the image is built, and which prefix marks one that changes the running container?
Show answer
BP_ affects the build. BPL_ affects the running container.