Docker for Java Developers: Production Guide
A common mistake in Java containerization is copying a fat JAR into a single-layer image. This results in 200MB+ images and slow deployment cycles. Here is how to build production-grade Java containers.
1. The Multi-Stage Build Pattern
Always separate your build environment from your runtime environment.
# Stage 1: Build
FROM maven:3.8-openjdk-17 AS build
COPY src /app/src
COPY pom.xml /app
RUN mvn -f /app/pom.xml clean package
# Stage 2: Runtime
FROM eclipse-temurin:17-jre-alpine
COPY --from=build /app/target/app.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
2. JRE Choice: Alpine vs. Slim
- Alpine: Tiny footprint, but uses
muslinstead ofglibc. - Slim: Larger but more compatible with native libraries.
3. JVM-Awareness
Modern JDKs (10+) are aware of container resource limits (cgroups). Always ensure UseContainerSupport is enabled (it's the default in modern versions) to avoid the JVM claiming the host's total RAM and getting OOM-killed by Docker.
Summary
Optimized Docker images lead to faster builds, cheaper storage, and more predictable production performance.
Next: Beyond CAP: Why PACELC is the Real Rule Related: Advanced Java GC Tuning
