Coder Social home page Coder Social logo

fedeoliv / java-custom-jre Goto Github PK

View Code? Open in Web Editor NEW
8.0 2.0 2.0 238 KB

Docker image size optimization for Java applications with jlink and multi-stage builds.

License: MIT License

Java 83.51% Dockerfile 12.68% Shell 3.81%
java maven spring-boot jlink docker-images

java-custom-jre's Introduction

Docker image size optimization for Java applications

Background

If you have tried to create a Docker image for a Java application, you know it tends to be large because:

  • Official Java base Docker images are relatively large. For example, the openjdk:12-alpine has 339MB.
  • There are many classes that might not be used by your application, but they are bundled anyway by the JRE (for example, application servers that run in headless mode still bundle graphical packages such as javax.swing). Even for a small application we have to maintain a complete JRE, which is simply a waste of memory.

In this repository we use the following approaches to have a smaller Docker image:

  1. Java modularity. Java 9 introduced a new level of packages abstraction known as modules, making possible to eliminate unnecessary modules that are part of the JDK as they are (most of them) decoupled.
  2. Create a custom JRE using jlink and jdeps. We want a JRE that contains only the platform modules that are required by the application.
  3. Multi-stage builds. Creating a custom JRE allows us to leverage smaller final images like alpine - a minimal Docker image based on Alpine Linux with a complete package index and only 5 MB in size!

Prerequisites

For development purposes:

  • Install Java JDK 9 or later
  • Install Maven 3.x or later

For building and running containers:

About the samples

This repository contains Maven and Spring Boot samples, structured in the following way:

Folder Description
maven The maven-app-v1 folder contains a Maven project using the default JRE, while the maven-app-v2 is similar but using a custom JRE.
spring-boot The spring-boot-default folder contains a Spring Boot project using the default JRE, while the spring-boot-custom is similar but using a custom JRE.

Comparing results

You can build all Docker images for Maven and Spring Boot applications to compare image sizes.

Maven

Follow the instructions described here to compare results of maven-app-v1 and maven-app-v2 projects.

The image size was reduced from 339MB to 53.9MB, representing ~84.1% reduction in the image size.

Spring Boot

Follow the instructions described here to compare results of spring-boot-default and spring-boot-custom projects.

The image size was reduced from 358MB to 113MB, representing ~68.4% reduction in the image size.

java-custom-jre's People

Contributors

dependabot[bot] avatar fedeoliv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

bharatnpti jdluna

java-custom-jre's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.