Reputation: 41
I am trying to provide a minimal JRE for a slim Docker image. With jlink 11.0.4, which comes with open-jdk-11 on Ubuntu, the image built with this custom JRE is actually larger than what it would be with a default JRE. I think this is due to a very large size of cutom_jre/lib/server/libjvm.so
I am following Kubernetes for Java Developers from Arun Gupta on Linkedin Learning. At some point, I have noticed a strange behaviour on my Ubuntu instance. Along with this course, I needed to provide a custom JRE to a minimal Spring Boot application, in order to use it for a slim Docker image.
jlink \
--output myjre \
--add-modules $(jdeps --print-module-deps target/greeting.jar),\
java.xml,jdk.unsupported,java.sql,java.naming,java.desktop,\
java.management,java.security.jgss,java.instrument \
This approach worked fine in the course and the size of my_jre
was fit to build a minimal Docker image for the application. The problem is that in my setup I have used openjdk-11, rather than openjdk-9.
With my setup, the resulting size of my_jre
would be very large(~380mb). After some investigation, I found out that this was mainly and possibly only due to the large size of my_jre/lib/server/libjvm.so
.
I have not had any prior experience with jlink
before this attempt. After some trial and error, and browsing through similar issues on github, I have found a workaround which comes outside of the jlink functionality.
This workaround comes from the binutils package and the strip utility.
Running the strip
utility with the --strip-unneeded
flag would slim down the libjvm.so size and make possible the achievement of the primary goal: providing a minimal JRE(~113mb) for a slim Docker image.
strip -p --strip-unneeded myjre/lib/server/libjvm.so
I would appreciate a deeper explanation about the background of this flair from someone who has the knowledge. Also what did change from jdk9 to jdk11 in terms of this behavior? Is there any standard process to achieve this goal without any external utilities other than the ones jdk9+ provides, such as jlink?
Upvotes: 4
Views: 2561
Reputation: 301
There are other options available to reduce the size in jlink.
--strip-debug --no-header-files --no-man-pages
For help see "jlink --help"
I also tried to create custom JRE. I got libjvm.so nearly 415 MB. with the above options mentioned I got the JRE size to 39.7 MB.
Upvotes: 2