Services DevOps DevSecOps Cloud Consulting Infrastructure Automation Managed Services AIOps MLOps DataOps Microservices 🔐 Private AINEW Solutions DevOps Transformation CI/CD Automation Platform Engineering Security Automation Zero Trust Security Compliance Automation Cloud Migration Kubernetes Migration Cloud Cost Optimisation AI-Powered Operations Data Platform Modernisation SRE & Observability Legacy Modernisation Managed IT Services 🔐 Private AI DeploymentNEW Products ✨ ZippyOPS AINEW 🛡️ ArmorPlane 🔒 DevSecOpsAsService 🖥️ LabAsService 🤝 Collab 🧪 SandboxAsService 🎬 DemoAsService Bootcamp 🔄 DevOps Bootcamp ☁️ Cloud Engineering 🔒 DevSecOps 🛡️ Cloud Security ⚙️ Infrastructure Automation 📡 SRE & Observability 🤖 AIOps & MLOps 🧠 AI Engineering 🎓 ZOLS — Free Learning Company About Us Projects Careers Get in Touch

JVM Docker Optimization: Reduce Image Size Fast

JVM Docker Optimization: Techniques to Reduce Image Size

JVM Docker optimization is essential for faster deployments and lower resource usage. By applying proven strategies like multi-stage builds, jlink, and jdeps, teams can shrink container images significantly and improve runtime efficiency.

Why JVM Docker Optimization Matters

Large Docker images are common because Java 11 and newer versions do not provide a pre-bundled JRE. Consequently, a basic Dockerfile often produces oversized images. For example, consider a simple Spring Petclinic project:

FROM eclipse-temurin:17
VOLUME /tmp
COPY target/spring-petclinic-3.1.0-SNAPSHOT.jar app.jar

After building the Docker image, its size can reach 465MB, even though the JAR is only 55MB. Most of this space comes from unnecessary layers, including the full JDK, OS utilities, and extra files. Tools like Dive help analyze layer contents and identify space-saving opportunities.

JVM Docker optimization example showing multi-stage build, jlink, and jdeps for smaller image size

Key Factors That Increase JVM Docker Image Size

  • Installing the full JDK instead of a minimal runtime
  • Adding utilities like tzdata, curl, and locales
  • Using large base OS images
  • Copying applications without dependency analysis

Understanding these causes is crucial for effective JVM Docker optimization.

Multi-Stage Builds with jlink for JVM Docker Optimization

Using the jlink tool is a powerful way to reduce Docker image sizes. It creates a custom Java runtime with only the modules your application needs.

Example Dockerfile for multi-stage builds:

FROM eclipse-temurin:17 as jre-build

RUN $JAVA_HOME/bin/jlink \
    --add-modules ALL-MODULE-PATH \
    --strip-debug \
    --no-man-pages \
    --no-header-files \
    --compress=2 \
    --output /javaruntime

FROM debian:buster-slim
ENV JAVA_HOME=/opt/java/openjdk
ENV PATH "${JAVA_HOME}/bin:${PATH}"
COPY --from=jre-build /javaruntime $JAVA_HOME
RUN mkdir /opt/app
COPY target/spring-petclinic-3.1.0-SNAPSHOT.jar /opt/app/app.jar
CMD ["java", "-jar", "/opt/app/app.jar"]

With this approach, the Docker image size dropped from 465MB to 217MB, almost 50% smaller. Multi-stage builds combined with jlink are essential for efficient JVM Docker optimization.

Further Image Size Reduction Using jdeps

The Java Dependency Analysis Tool (jdeps) helps refine JVM Docker optimization by including only required modules.

Example Dockerfile using jdeps:

FROM eclipse-temurin:17 as jre-build
COPY target/spring-petclinic-3.1.0-SNAPSHOT.jar /app/app.jar
WORKDIR /app
RUN jar xf app.jar
RUN jdeps --ignore-missing-deps --print-module-deps --multi-release 17 --recursive --class-path 'BOOT-INF/lib/*' app.jar > modules.txt
RUN $JAVA_HOME/bin/jlink --add-modules $(cat modules.txt) --strip-debug --no-man-pages --no-header-files --compress=2 --output /javaruntime

FROM debian:buster-slim
ENV JAVA_HOME=/opt/java/openjdk
ENV PATH "${JAVA_HOME}/bin:${PATH}"
COPY --from=jre-build /javaruntime $JAVA_HOME
RUN mkdir /opt/server
COPY --from=jre-build /app/app.jar /opt/server/
CMD ["java", "-jar", "/opt/server/app.jar"]

By including only the modules required, the image size dropped further to 184MB, illustrating the impact of careful dependency analysis for JVM Docker optimization.

Best Practices for JVM Docker Optimization

  1. Use slim base images like debian:buster-slim
  2. Implement multi-stage Docker builds
  3. Create custom JRE with jlink
  4. Analyze dependencies using jdeps
  5. Remove unnecessary utilities and debug files
  6. Regularly inspect images with tools like Dive

These steps ensure your containers are optimized for speed, efficiency, and maintainability.

Enhance JVM Docker Optimization with ZippyOPS

For comprehensive JVM Docker optimization, ZippyOPS provides consulting, implementation, and managed services across DevOps, DevSecOps, DataOps, Cloud, Automated Ops, AIOps, MLOps, Microservices, Infrastructure, and Security.

By combining technical best practices with expert guidance, ZippyOPS helps organizations streamline operations and maintain secure, high-performing environments. Learn more about ZippyOPS services, solutions, and products. Watch demos and tutorials on their YouTube channel.

Conclusion

In summary, JVM Docker optimization significantly reduces image size, improves deployment speed, and lowers resource usage. Techniques such as multi-stage builds, jlink, jdeps, and slim base images create leaner, more efficient Docker containers. Partnering with ZippyOPS ensures expert guidance in implementing these best practices.

For professional assistance or a consultation, email [email protected].

External Reference:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top