How to reduce image size of Java App

How to reduce image size of Java App

The simplest Dockerfile


Article content
Do not forget to build your JAR file


let's build our Dockerfile image and compare the sizes of our JAR file and the created Docker image.


Article content

If we look at the SIZE column, we can see that the size of our Docker image is 465MB!


How to know each layer size

To do this, we will use a tool called Dive which is used for exploring a docker image, layer contents, and discovering ways to shrink the size of your Docker/OCI image.

To install Dive, follow the guide in their README:


Article content

Customizing JRE using jlink

Article content

Let’s now build our container image and find out how much smaller it has become.


Article content

Stripping Image Size, Even More, Using jdeps


What if I told you that the size of our container image can be made even smaller? When paired with jlink, you can also use the Java Dependency Analysis Tool (jdeps), which was first introduced in Java 8, to understand the static dependencies of your applications and libraries.

Article content

The only thing left to do is to see how much the container image has shrunk using our latest Dockerfile:


Article content


References

  1. https://dzone.com/articles/ways-to-reduce-jvm-docker-image-size
  2. https://blog.monosoul.dev/2022/04/25/reduce-java-docker-image-size/
  3. https://bell-sw.com/blog/how-to-dockerize-a-spring-boot-app-with-the-smallest-base-image/
  4. https://medium.com/@ievgen.degtiarenko/reduce-size-of-docker-image-with-spring-boot-application-2b3632263350
  5. https://bell-sw.com/announcements/2021/01/13/Secrets-behind-tiny-Docker-containers-for-Java-microservices/


Thanks for sharing Saeid. Do you have a blog where you write articles aside from LinkedIn?

To view or add a comment, sign in

More articles by Saeid Noroozi

  • Java Virtual Thread and Pinning Problem

    Why Virtual Threads? The JVM is a multithreaded environment. As we may know, the JVM gives us an abstraction of OS…

    4 Comments
  • Transaction Isolation Level

    Problems 1. Dirty Reads Dirty Reads 2.

  • Mutation Tests

    Introduction Mutation testing has existed since 1971, it fell out of favor due to high costs. Today, it’s a more…

    2 Comments
  • F5 BIG IP

    Introduction F5's BIG-IP is a collection of hardware and software solutions designed for application security…

    2 Comments

Explore content categories