dockerjvmopenjdk-11jstackjstat

How to capture application monitoring information from the docker container


I have more than one java application running in separate docker containers. I am trying to collect monitoring data such as GC log, thread dump, heap dump from the java process running inside a container using tools like jstat, jstack, jmap. Is it possible to capture this information from the host(outside containers)?

I am new to the containerized world. I understand that PID namespace of the host and container is different. When I execute jstack <PID> > thread_dump.txt from the host, it shows error message: Unable to open socket file /proc/root/tmp/.java_pid: target process doesn't respond within 10500ms or HotSpot VM not loaded

Where PID is process id from the host PID namespace.

When I execute jstack inside container ( docker exec -it <container_id_or_name>) then it is able to capture thread dump.

Where PID is process id from the container PID namespace.

Any hints on how to solve this?


Solution

  • jattach tool serves exactly for this purpose. The project is free and open source.

    Examples:

    where <pid> is Java process ID in the host namespace.

    As a bonus, jattach works even if container runs JRE with no JDK tools (jstack, jcmd, etc.) installed.