Thread and Heap Dumps in Docker Containers

Maciej
2 min readNov 18, 2020

--

Introduction

Sometimes we need to debug our docker applications with the use of Thread/Heap dump, below is a simple and quick way to do it from docker container

Environment

Thread dump

Following steps will allow take thread dumps from a docker container, and then copy saved file into to host

  • Run example application:
root@vagrant:/home/vagrant# docker run -d bitnami/java-example:0.0.1
  • Run the below command and go into the container.
root@vagrant:/home/vagrant# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bf342a25fc61 bitnami/java-example:0.0.1 "java -jar jenkins.w…" 2 seconds ago Up 1 second 8080/tcp competent_dubinsky
root@vagrant:/home/vagrant# docker exec -it bf342a25fc61 bash
  • Find PID of java process (First column is user that run the process, second column PID)
root@bf342a25fc61:/app# ps -fea|grep -i java
root 1 0 2 19:02 ? 00:00:06 java -jar jenkins.war
root 43 26 0 19:06 pts/0 00:00:00 grep -i java
  • Run the below command to get the thread dump. (Change PID to number of process)
root@bf342a25fc61:/app# jstack PID > thread.tdump
  • Exit from the docker container and download thethread.tdump from the docker container to host
root@bf342a25fc61:/app# exit
root@vagrant:/home/vagrant# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bf342a25fc61 bitnami/java-example:0.0.1 "java -jar jenkins.w…" 8 minutes ago Up 7 minutes 8080/tcp competent_dubinsky
root@vagrant:/home/vagrant# docker cp bf342a25fc61:/app/thread.tdump .
root@vagrant:/home/vagrant# ls
file_with_password.txt new_file.txt thread.tdump

Heap Dump

Following steps will allow take heap dumps from a docker container, and then copy saved file into to host

  • Run example application:
docker run -d bitnami/java-example:0.0.1
  • Run the below command and go into the container.
root@vagrant:/home/vagrant# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
68e2cd8a3415 bitnami/java-example:0.0.1 "java -jar jenkins.w…" 3 seconds ago Up 1 second 8080/tcp nice_lehmann
root@vagrant:/home/vagrant# docker exec -it 68e2cd8a3415 bash
  • Find PID of java process (First column is user that run the process, second column PID)
root@68e2cd8a3415:/app# ps -fea|grep -i java
root 1 0 10 19:14 ? 00:00:05 java -jar jenkins.war
root 32 26 0 19:14 pts/0 00:00:00 grep -i java
  • Run the below command to get the heap dump. (Change PID to number of process)
root@68e2cd8a3415:/app# jmap -dump:live,format=b,file=heap.hprof 1
Dumping heap to /app/heap.hprof ...
Heap dump file created
root@68e2cd8a3415:/app#
  • Exit from the docker container and download heap.hprof from the docker container to host.
root@68e2cd8a3415:/app# exit
exit
root@vagrant:/home/vagrant# docker cp 68e2cd8a3415:/app/heap.hprof .
root@vagrant:/home/vagrant# ls
heap.hprof thread.tdump

--

--

Maciej

DevOps Consultant. I’m strongly focused on automation, security, and reliability.