How to capture application monitoring information from the docker container

2.7k Views Asked by At

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?

2

There are 2 best solutions below

9
On BEST ANSWER

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

Examples:

  • jattach <pid> threaddump (works like jstack)
  • jattach <pid> inspectheap (works like jmap -histo)
  • jattach <pid> jcmd GC.class_stats

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.

5
On

The command you should use is:

docker exec -i <container_id_or_name> <your_monitoring_command>

The command will be executed inside the container, but it will report its output (by -i) to the caller console.