Pre warmed Java Docker image with class data sharing

881 Views Asked by At

I want to create a docker image that runs on a Java Service with OpenJ9's Class Data Sharing feature to improve startup performance. I want to create the Class Cache while building the image using a multi stage docker build. I saw a few mentions of pre warming a docker image like this online

https://github.com/barecode/adopt-openj9-spring-boot/blob/master/Dockerfile.openj9.warmed

however, i'm not able to recreate it here is my Dockerfile

FROM adoptopenjdk/openjdk11-openj9:alpine as base
ADD libs/ /libs
ADD service.jar /service.jar

RUN mkdir /hi
WORKDIR /hi
RUN ls /
RUN java -Xshareclasses:name=mycache -Xshareclasses:cacheDir=/hi -Xshareclasses -jar /usr/share/app/service.jar &
RUN sleep 5
RUN ls -la /hi
FROM adoptopenjdk/openjdk11-openj9:alpine-jre
COPY --from=base libs/ /usr/share/app/libs
COPY --from=base service.jar /usr/share/app/service.jar

RUN /bin/sh -c 'ps aux | grep java | grep service | awk '{print $2}' | xargs kill -1'
#RUN java -Xshareclasses:listAllCaches
ENTRYPOINT ["java","-jar", "-Xshareclasses" , "-Xtune:virtualized", "-XX:+UseContainerSupport", "/usr/share/app/service.jar"]

my problem is that when I'm running

RUN java -Xshareclasses:name=mycache -Xshareclasses:cacheDir=/hi -Xshareclasses -jar /usr/share/app/service.jar &

and then expecting the cache file to be saved on /hi, the file isn't there.

any help will be appreciated. Thanks.

1

There are 1 best solutions below

0
On

OpenJ9 only reads the last -Xshareclasses option provided. This makes it easy to replace previous options in the commandline when developing / debugging as in some environments, it's hard to modify the existing commandline args.

Change the command to:

RUN java -Xshareclasses:name=mycache,cacheDir=/hi -jar /usr/share/app/service.jar &

and the cache will be created in the /hi directory.

For example:

# java -Xshareclasses:name=mycache,cacheDir=/hi -version
openjdk version "11.0.4" 2019-07-16
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.4+11)
Eclipse OpenJ9 VM AdoptOpenJDK (build openj9-0.15.1, JRE 11 Linux amd64-64-Bit Compressed References 20190717_286 (JIT enabled, AOT enabled)
OpenJ9   - 0f66c6431
OMR      - ec782f26
JCL      - fa49279450 based on jdk-11.0.4+11)

# ls /hi
C290M11F1A64P_mycache_G37