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.