✦ For everyone, free.

Practical knowledge for real and everyday life

Home

3.3.2.1 Java Runtime Images

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

Java runtime images provide a Java Virtual Machine pre-installed on top of an operating system base, used as the foundation for running compiled Java applications inside a container without separately installing and configuring a JVM in every project.

Choosing Between JDK and JRE Images

A development or build stage typically needs the full Java Development Kit, including the compiler, while the final runtime stage only needs the smaller Java Runtime Environment needed to execute already-compiled code.

FROM eclipse-temurin:21-jdk AS build
WORKDIR /src
COPY . .
RUN javac Main.java

FROM eclipse-temurin:21-jre
COPY --from=build /src/Main.class /app/Main.class
WORKDIR /app
CMD ["java", "Main"]

The final image only includes the JRE, not the full JDK, since compilation already happened in the earlier build stage.

Packaging a JAR File

For applications packaged as an executable JAR, the runtime image simply needs to run that JAR using the included java command.

FROM eclipse-temurin:21-jre
COPY target/myapp.jar /app/myapp.jar
CMD ["java", "-jar", "/app/myapp.jar"]
JVM Memory Configuration Inside Containers

Modern JVMs are container-aware, automatically detecting cgroup memory limits and sizing the heap accordingly, but explicitly setting JVM memory flags is still common to ensure predictable behavior under the container's configured memory limit.

docker run -e JAVA_OPTS="-Xmx512m" myapp:1.0
Choosing a Base Distribution for the JVM Image

Java runtime images are available built on various underlying operating systems, including Alpine-based variants for smaller image size, though some Java applications depend on glibc-specific behavior that can make a glibc-based base a safer default choice.

FROM eclipse-temurin:21-jre-alpine
Why Java Runtime Images Matter

Because installing and correctly configuring a JVM from scratch involves several non-trivial steps, prebuilt Java runtime images significantly simplify containerizing Java applications, letting a Dockerfile focus on packaging the application itself rather than on JVM installation details.