[Original] Uncle Experience Sharing (71) Using jvm tools in docker containers

Posted by degsy on Fri, 14 Jun 2019 19:43:39 +0200

jvm tools are often used in java applications for some operations. If a java application is deployed in a docker container, how do you use jvm tools?

First, look at the docker image used.

  • For example, common openjdk mirrors are divided into jdk and jre. Only the jdk version has jvm tools, so you can use the jdk version of openjdk directly.
  • For example, common tomcat mirrors do not have jdk and JRE selections. By default, they are all jre, so there is no jvm tool. The jdk directory in tomcat mirrors is as follows:
# ls /usr/lib/jvm/java-1.8-openjdk
bin  jre  lib
# ls /usr/lib/jvm/java-1.8-openjdk/bin
appletviewer  orbd          rmid          tnameserv
java          pack200       rmiregistry   unpack200
keytool       policytool    servertool
# ls /usr/lib/jvm/java-1.8-openjdk/lib
amd64

Then you can mount the jdk file into the container and execute it. First, you need to find the corresponding jdk file. The first step is to find the operating system version of the current container.

 

Common versions:

Alpine

cat /etc/os-release
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.9.3
PRETTY_NAME="Alpine Linux v3.9"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://bugs.alpinelinux.org/"

Debian

# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
NAME="Debian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

Centos

# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

 

Then find the corresponding jdk image based on the operating system version, such as openjdk

openjdk:<version> Be based on debian
openjdk:<version>-alpine Be based on alpine

The tomcat image is based on alpine, so download openjdk:8-jdk-alpine, 8 is the specific JDK version, and after downloading, copy the JDK directory out

/usr/lib/jvm/java-1.8-openjdk/

There are several ways to use the jvm tool:

  • Copy the file directly into the container;
  • Mount the necessary files into the container;
  • Mount the jdk directory into the container and directly replace the original jdk directory.

The first two methods require only a few files, one is $JAVA_HOME/lib/tools.jar, the other is the specific jvm tools in $JAVA_HOME/bin/such as jmap, jstack, jstat, etc., which can be used by copying or mounting files to a specified location in the container.

The last is the simplest, and all jvm tools can be used after the jdk directory is mounted.

# docker exec -it $container_id jstat -gcutil $pid
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT
  0.00 100.00  73.20  60.74  95.79  93.92    417    3.163     8    1.213    4.376

 

Reference resources:

https://hub.docker.com/_/openjdk

https://hub.docker.com/_/tomcat

Topics: PHP JDK jvm CentOS Java