A Dockerfile builder for Spring Boot that produces a clean multi-stage Dockerfile for Java microservices. You choose Maven or Gradle, a JDK, a port, and whether to wire in an Actuator health check, and it emits a build that compiles your jar in one image and runs it in a slim JRE image as a non-root user.
How it works
The build stage uses a full image that contains both the JDK and your build tool. For
Maven it runs mvn dependency:go-offline after copying only pom.xml, so dependency
resolution is cached as its own layer, then runs mvn package -DskipTests. For Gradle it
resolves dependencies, then runs bootJar. Either way the resulting fat jar is copied to a
predictable path, app.jar.
The runtime stage starts from eclipse-temurin:<jdk>-jre-alpine — a slim image with
only the Java runtime. It creates a dedicated spring user, copies the jar with the right
ownership, switches to that user with USER, and starts the app with MaxRAMPercentage so
the JVM sizes its heap against the container limit. When enabled, the HEALTHCHECK polls
/actuator/health and looks for the UP status.
Tips and notes
- Tests are skipped in the image build (
-DskipTests/-x test) on purpose — run your test suite in CI before building the image, not inside the container build. - For even faster startup and smaller images you can extract the Spring Boot layered
jar with
java -Djarmode=layertools -jar app.jar extract; this builder keeps the simpler single-jar form, which is correct for the vast majority of services. - Always pin an LTS JDK (21, 17, or 11) so your runtime matches what you tested against.
- Make sure
spring-boot-starter-actuatoris on the classpath andmanagement.endpoint.health.probes.enabled=trueis set before relying on the health check.