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

jattach is a utility to send commands to remote JVM.

# 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