Is it possible to take thread dump of a java process in the container from the host?

hobgoblin picture hobgoblin · Jun 15, 2017 · Viewed 8.2k times · Source

My java process is running inside the container. Of course I can see that process on host machine and see its pid as well. If jdk is not installed on the cluster but on the host, can I run jstack from the host against the java process in the docker container, using this pid. By the way, I tried it and ran into following error

Attaching to process ID 66367, please wait...
Error attaching to process: Doesn't appear to be a HotSpot VM (could not find symbol "gHotSpotVMTypes" in remote process)
sun.jvm.hotspot.debugger.DebuggerException: Doesn't appear to be a HotSpot VM (could not find symbol "gHotSpotVMTypes" in remote process)
at sun.jvm.hotspot.HotSpotAgent.setupVM(HotSpotAgent.java:411)
at sun.jvm.hotspot.HotSpotAgent.go(HotSpotAgent.java:305)
at sun.jvm.hotspot.HotSpotAgent.attach(HotSpotAgent.java:140)
at sun.jvm.hotspot.tools.Tool.start(Tool.java:185)
at sun.jvm.hotspot.tools.Tool.execute(Tool.java:118)
at sun.jvm.hotspot.tools.JStack.main(JStack.java:92)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.tools.jstack.JStack.runJStackTool(JStack.java:140)
at sun.tools.jstack.JStack.main(JStack.java:106)

Answer

user801247 picture user801247 · Mar 9, 2018

The jstack version you are using needs to come from the exactly same JVM version your software is running.

Running jstack within the application container will work.

You can also run jstack in remote debugging after activating the remote debugging server / jmx on your software.

Also, if your container doesn't have jstack, you can probably run a container built with the same jvm but ships jstack, in the same pid namespace of your jvm container by running:

docker run --pid=container:your_app your_jstack_images jstack $in_your_app_container_jvms_pid