Skip to content

Graceful shutdown of java process inside docker container

  • by

Recently faced an issue where the docker stop was not calling a shutdown hook was not working correctly. The same application when run as a spring boot applicaiton or jar without docker invokes the shutdownhook. 

Runtime.addShutdownHook( Thread hook)

During debugging found that the boot app inside the container is not geting SIGTERM when docker stop command is being issued. The boot proces gets killed abruptly after few seconds.

During Start:

Furthur looked at our docker file and found that the java webapp/boot app is being started like this. 

CMD ["/opt/", "run"] 

Docker start —–> Shell Script ——–> Java Process(web application)
 The actual java process is a child of shell script process.
#!/bin/bash ........ 
java -jarboot.jar ..........

During Stop:

Basically during shutdown the container will pass the TERM to the shell script and unfortunately as the shell script does not forward the same to java process.   

Docker stop—->a SIGTERM to the main proces Shell script stop -/(broken/ NO SIGTERM forward)—–>Java Process(web app)

To fix this we modified our script as below

#!/bin/bash ........ 
exec java -jar boot.jar ..........


Leave a Reply

Your email address will not be published. Required fields are marked *