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.
Furthur looked at our docker file and found that the java webapp/boot app is being started like this.
CMD ["/opt/start.sh", "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 ..........
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.
To fix this we modified our script as below
#!/bin/bash ........ exec java -jar boot.jar ..........