DevOps

Spring Boot Dockerfile Generator

Scaffold an optimized Dockerfile for Spring Boot applications using Maven multi-stage builds with Eclipse Temurin JDK/JRE images.

Options

# Stage 1: Builder
FROM maven:3.9-eclipse-temurin-17 AS builder
WORKDIR /app
COPY pom.xml .
RUN mvn dependency:go-offline
COPY src ./src
RUN mvn clean package -DskipTests

# Stage 2: Runner
FROM eclipse-temurin:17-alpine
WORKDIR /app
COPY --from=builder /app/target/*.jar app.jar
ENV NODE_ENV=production
EXPOSE 3000
RUN addgroup -S appgroup && adduser -S appuser -G appgroup\nUSER appuser\nENTRYPOINT ["java", "-jar", "app.jar"]

Spring Boot Dockerfile Generator: Complete Guide

Multi-Stage Docker Builds for Spring Boot

Spring Boot applications compile into fat JARs that contain an embedded Tomcat server. A multi-stage Dockerfile separates the Maven build (which needs the full JDK + Maven toolchain at ~800MB) from the runtime (which only needs the JRE at ~200MB), drastically reducing the final image size.

# Stage 1: Builder
FROM maven:3.9-eclipse-temurin-17 AS builder
WORKDIR /app
COPY pom.xml .
RUN mvn dependency:go-offline
COPY src ./src
RUN mvn clean package -DskipTests

# Stage 2: Runner
FROM eclipse-temurin:17-jre-alpine
WORKDIR /app
COPY --from=builder /app/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]

How to Use

  1. 1

    Runtime Pre-Selected

    This page has pre-configured the generator for Spring Boot. Adjust port, base image, and environment variables as needed.

  2. 2

    Toggle Multi-Stage Build

    Enable multi-stage builds to separate the build environment from the runtime, drastically reducing image size.

  3. 3

    Enable Non-Root Security

    Check the non-root user option to run your container as a non-privileged user for production security.

  4. 4

    Copy & Use

    Switch between Dockerfile, docker-compose.yml, and .dockerignore tabs to copy each file.

Frequently Asked Questions

How do I create a multi-stage Dockerfile for Spring Boot?+

Use a Maven image (maven:3.9-eclipse-temurin-17) as the builder stage to compile and package the JAR. Then copy the resulting JAR into a lightweight JRE image (eclipse-temurin:17-jre-alpine) for the runner stage.

What is the best Java base image for Docker?+

Eclipse Temurin (formerly AdoptOpenJDK) is the recommended production-grade OpenJDK distribution. Use 'eclipse-temurin:17-jre-alpine' for the smallest runtime image, or 'eclipse-temurin:17-jre' for Debian-based compatibility.