✦ For everyone, free.

Practical knowledge for real and everyday life

Home

12.2.1 Java Docker Workflow

A focused guide to Java Docker Workflow, connecting core concepts with practical Docker and container operations.

The Java Docker workflow follows a multi-stage build pattern that separates the JDK-dependent compilation step from a leaner, JRE-only (or minimal JDK) runtime stage, reflecting the fact that compiling and running Java code have meaningfully different tooling requirements.

The Basic Multi-Stage Structure

A build stage uses a full JDK image to compile and package the application, while the final stage uses a much smaller runtime-only image.

FROM maven:3.9-eclipse-temurin-21 AS build
WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn package -DskipTests

FROM eclipse-temurin:21-jre-alpine
COPY --from=build /app/target/myapp.jar /app/myapp.jar
CMD ["java", "-jar", "/app/myapp.jar"]

The final image contains only the JRE and the built JAR, without the much larger JDK and build tooling needed only during compilation.

Why This Separation Significantly Reduces Final Image Size

A JDK image, including full compilation and development tooling, is substantially larger than a JRE-only image — the multi-stage approach ensures none of that unnecessary build-time tooling carries into the final, deployed image.

docker images
maven:3.9-eclipse-temurin-21    650MB
eclipse-temurin:21-jre-alpine   220MB
Leveraging Dependency Layer Caching for Faster Builds

Copying dependency declarations before the full source code allows Docker to cache the dependency resolution step, avoiding unnecessary re-downloads when only application code changes.

COPY pom.xml .
RUN mvn dependency:go-offline
COPY src ./src
RUN mvn package
Why This Workflow Matters

This multi-stage pattern, separating Java's heavier build-time tooling from its lighter runtime requirements, is the standard, well-established approach for producing efficient, appropriately sized Java application images, and understanding it is foundational to containerizing Java applications effectively.

Content in this section