How to extract thread and heap dump from running JRE in container

jattach is a utility to send commands to remote JVM.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# attach tty to the container
docker exec -it container sh

# for alpine image, install like this
apk add --no-cache jattach --repository http://dl-cdn.alpinelinux.org/alpine/edge/community/

# otherwise, you can download the binary
wget -L -O /usr/local/bin/jattach \
  https://github.com/apangin/jattach/releases/download/v1.5/jattach && \
  chmod +x /usr/local/bin/jattach

# generate heap dump
jattach 1 dumpheap container.hprof

# generate thread dump
jattach 1 threaddump > container.tdump

# go back to host
exit

# copy thread & heap dump to host
docker cp container:/container.hprof .
docker cp container:/container.tdump .

# use jconsole or visualvm to analyse the dumps