Question
What factors contribute to the larger size of the Java 11 base Docker image (openjdk:11-jre-slim) compared to Java 8 (openjdk:8-jre-alpine)?
Answer
The size discrepancy between the OpenJDK 11 Docker image and its predecessor OpenJDK 8 can be attributed to various factors, including base image selections, package size differences, and structural changes in the JDK.
...1. Dockerfile Example
FROM openjdk:11-jre-slim
COPY target/my-app.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
# Use multi-stage builds for size optimization
FROM openjdk:11-jre-slim AS build
COPY . /src
RUN javac /src/MyApp.java
...
Causes
- The openjdk:11-jre-slim image is based on debian:sid-slim, which is inherently larger than alpine:3.8 used for Java 8.
- Debian sid is an unstable version, potentially increasing the image size and complexity.
- The size of the openjdk-11-jre-headless package is approximately three times larger than that of openjdk-8-jre.
- The introduction of the 'modules' system in JDK 11 significantly increases the size due to the larger lib directory.
Solutions
- Consider using the openjdk:11-jre-alpine image if minimal size is a priority, though it may come with compatibility trade-offs.
- Look into optimizing Docker images using multi-stage builds to strip unnecessary layers and reduce the final image size.
- Evaluate whether your application can utilize a different base image that better fits your size requirements.
Common Mistakes
Mistake: Using the wrong base image for a Java application without considering size and stability.
Solution: Carefully evaluate the base image based on the application requirements and deployment environment.
Mistake: Not utilizing multi-stage builds to reduce Docker image size.
Solution: Incorporate multi-stage Docker builds to create leaner images by separating build dependencies from runtime.
Helpers
- OpenJDK 11 Docker image size
- openjdk:11-jre-slim vs openjdk:8-jre-alpine
- Java 11 Docker best practices
- Docker image optimization Java