How to run JCMD without JDK on a docker container with openJDK installed

2.6k Views Asked by At

I need to run JCMD on prod to monitor my application, but unfortunately I can't have a proper JDK in place so I need to run it with a JRE. I can add some dependencies, but not the whole jdk.

I'm using openJDK 8.

Does anyone know how to do it?

There was one question already posted here (How to run jcmd without the JDK?), but the answer works for windows servers, not for linux OS in a docker container.

1

There are 1 best solutions below

0
On

In my personal experience jcmd from newer version of Java works with the older versions. You can exploit the multi-stage build feature and have jlink elegantly package for you the (strictly) necessary files, please refer to this Dockerfile:

FROM adoptopenjdk:11-jdk-hotspot

RUN ${JAVA_HOME}/bin/jlink --module-path jmods --add-modules jdk.jcmd --output /jcmd 

FROM adoptopenjdk:8-jre-hotspot
        
COPY --from=0 /jcmd /jcmd

In case you prefer, I give you "an" answer for Java 8, using adoptopenjdk and multistage build

FROM adoptopenjdk:8-jdk-hotspot

RUN mkdir -p /jcmd && \
    mkdir -p /jcmd/bin && \
    mkdir -p /jcmd/lib && \
    cp ${JAVA_HOME}/bin/jcmd /jcmd/bin/jcmd && \
    cp -r ${JAVA_HOME}/lib/* /jcmd/lib


FROM adoptopenjdk:8-jre-hotspot
        
COPY --from=0 /jcmd ${JAVA_HOME}

in this case jcmd gets installed along the jre installation, in the same bin folder as java.

I tested both of them in both docker-desktop 20.10.7 and Mirantis (Docker Enterprise) 3.3.11.