In a Spring Boot application, the Dockerfile is the recipe that turns your packaged application and a Java runtime into a container image. That image runs the same way on a laptop, in CI and in production, because the JDK or JRE, the JAR and the start command are all fixed inside it.
A good Spring Boot Dockerfile uses a multi-stage build, a slim runtime image, Spring Boot layered JARs for fast rebuilds, and a non-root user.
The Dockerfile provides four things:
Docker caches each instruction as a layer. How you order those layers decides how fast rebuilds are.
The shortest working version copies a prebuilt JAR into a runtime image:
Build the JAR first with ./mvnw package, then build and run the image:
This is fine for learning, but every code change replaces one big layer that contains all dependencies, so each rebuild pushes and pulls the whole JAR again.
Spring Boot executable JARs contain a layers.idx file that splits the JAR into layers: dependencies, Spring Boot loader, snapshot dependencies and application classes. Extracting those layers lets Docker reuse the unchanged ones. The following Dockerfile builds the application inside Docker and extracts the layers:
Important points:
-Djarmode=tools extraction command is the one for Spring Boot 3.3 and later; older 2.x and 3.0 to 3.2 versions used -Djarmode=layertools extract. Check the documentation for your version.application layer changes between builds.| Option | Notes |
|---|---|
eclipse-temurin:21-jre | Common, well maintained OpenJDK build |
eclipse-temurin:21-jre-alpine | Smaller, uses musl libc; test it with native libraries |
| Distroless Java images | No shell or package manager, minimal surface |
Buildpacks (./mvnw spring-boot:build-image) | No Dockerfile at all; Spring Boot builds an optimised image |
Pick a long-term support Java version, currently 21 or 25, and pin the tag so builds are repeatable. The old openjdk image on Docker Hub is deprecated, so avoid it for new projects.
Containers need a few extras to behave well:
.dockerignore file that excludes target/, .git and local config to keep the build context small.SPRING_PROFILES_ACTIVE=prod and SPRING_DATASOURCE_URL, never bake secrets into the image.-XX:MaxRAMPercentage=75.0 in JAVA_TOOL_OPTIONS when needed.HEALTHCHECK or Kubernetes probes.The HEALTHCHECK above requires wget in the image, which some slim images do not include; in that case rely on orchestrator probes instead.
If you do not want to maintain a Dockerfile, Spring Boot can build an OCI image with Cloud Native Buildpacks:
Buildpacks produce layered, memory-tuned images and handle base image updates. A hand-written Dockerfile gives you more control and is easier to customise.
No. You can use spring-boot:build-image or the Gradle bootBuildImage task. A Dockerfile is useful when you want full control of the image.
It keeps build tools out of the final image, which makes the image smaller and safer, and it lets Docker cache the dependency step.
No. Create a non-root user and switch to it with USER.
Use a current LTS release supported by your Spring Boot version, such as Java 21 or 25, and test the application with it before deploying.
The Dockerfile makes your Spring Boot application portable and repeatable. Use a multi-stage build, extract Spring Boot layers for fast rebuilds, run as a non-root user, and configure through environment variables. When you prefer not to maintain a Dockerfile, buildpacks offer a tested alternative.