Graceful shutdown of java process inside docker container

6 views 11:34 pm 0 Comments March 26, 2019

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. [java]Runtime.addShutdownHook( Thread hook)[/java] 

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. 

[java]CMD ["/opt/start.sh", "run"] [/java]. 

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

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

[java]#!/bin/bash …….. exec java -jar boot.jar ………. [/java]

References:

Leave a Reply

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