Gracefully Stopping Docker Containers

Finally solved the problem.

Tcsh shell doesn't receive most of the signals like SIGTERM which is the signal sent by docker when stopping the container.

So I changed the script to use bash shell and whenever I want to run a tcsh command I just do it like this:

/bin/tcsh ./my-command

So, my docker-entrypoint.sh is like this:

#!/bin/bash

# SIGTERM-handler this funciton will be executed when the container receives the SIGTERM signal (when stopping)
term_handler(){
   echo "***Stopping"
   /bin/tcsh ./my-cleanup-command
   exit 0
}

# Setup signal handlers
trap 'term_handler' SIGTERM

echo "***Starting"
/bin/tcsh ./my-command

# Running something in foreground, otherwise the container will stop
while true
do
   #sleep 1000 - Doesn't work with sleep. Not sure why.
   tail -f /dev/null & wait ${!}
done