I’ve been attempting various ways to handle docker stop’s SIGTERM to do some cleanup and haven’t found a satisfactory way yet.
Ideally I would be able to have a neat place to put an exit to run something before the container exits.
I’ve tried using bash traps but the trap only seems to run if the script is in a particular state (not execing another process which is what all my containers do, not sleeping etc.) read works, ie the trap catches SIGTERM and I can do cleanup but read is not what my containers are doing obviously. Even if there was a way I would still have to forward the signal on to the process that the container is running to get it to start terminating before SIGKILL smashes up the joint.
I’ve seen others on google use a nanny process (perl/c) to handle SIGTERM, do the cleanup, launch the real process and forward SIGTERM to that process. Surely there is a better way ?