The Dockerfile tells Docker how to build the image that runs your Spring Boot application: which Java runtime to start from, which files to copy in, and which command launches the app. A good Dockerfile gives you the same image on a laptop, in CI and in production, and keeps rebuilds and image pulls fast.
For Spring Boot that means a multi-stage build, a JRE base image, layered jar extraction and a non-root user. This article shows each step with a complete file.
A Dockerfile is a text file of instructions. docker build runs them in order and stores each result as a read-only layer. The final image contains the Java runtime and your application and nothing else, so it runs identically wherever Docker runs.
The practical benefits are:
Build the jar first with ./mvnw package, then build and run the image.
Do not use the old openjdk Docker Hub image. It is deprecated and no longer receives updates. Use a maintained distribution such as eclipse-temurin, Amazon Corretto, Microsoft Build of OpenJDK or Bellsoft Liberica. Prefer a JRE tag over a JDK tag at runtime to keep the image small, because the app does not need a compiler.
A Spring Boot fat jar changes whenever your code changes, even though the dependencies inside it rarely do. Spring Boot can split the jar into layers (dependencies, loader, snapshot dependencies, application) so Docker caches the heavy dependency layer and only the small application layer is rebuilt and pushed.
On Spring Boot 3.3 and later, extract the layers with the tools jar mode. Older versions use -Djarmode=layertools with extract and without the --layers flag.
After a small code change, only the last COPY layer differs, so deployments transfer a few kilobytes instead of the whole jar.
A multi-stage build lets Docker compile the project, so CI needs only Docker.
Copying pom.xml before src means the dependency download layer is cached until the POM changes. The JDK stays in the build stage, and only the JRE ends up in the final image.
USER, as shown above..dockerignore file with target/, .git/, *.iml and local config, so the build context stays small and secrets are not copied.SPRING_PROFILES_ACTIVE and SPRING_DATASOURCE_URL, never bake secrets into the image.-XX:MaxRAMPercentage=75 through JAVA_TOOL_OPTIONS rather than a fixed -Xmx.Spring Boot can build an image without one. The Maven goal spring-boot:build-image and the Gradle task bootBuildImage use Cloud Native Buildpacks to produce an optimised, layered image with a sensible JVM memory configuration.
Choose a Dockerfile when you need full control over the base image and build steps, and buildpacks when you want a maintained default with less to write.
Not strictly. Buildpacks through spring-boot:build-image create images without one. A Dockerfile is the better choice when you need custom base images or build steps.
A current Temurin, Corretto or similar JRE image for your LTS Java version, for example Java 21 or 25. Avoid the deprecated openjdk image.
It keeps the compiler, Maven cache and source out of the final image, which makes it smaller and reduces the attack surface.
Usually a JDK base image, an unlayered fat jar or a missing .dockerignore. Switch to a JRE image, use layers and exclude build output from the context.
The Dockerfile is the recipe for a reproducible Spring Boot image. Start from a maintained JRE image, build with multi-stage Dockerfiles, split the jar into layers for caching, run as a non-root user and pass configuration through the environment. Use buildpacks when you prefer not to maintain a Dockerfile.