bashdockersignalsbash-trap

Run a cleanup script when stopping a Docker container


My Dockerfile looks like this:

FROM ubuntu:latest
# ... there is more
ENTRYPOINT ["/root/startup.sh"]
CMD ["choose_random"]

So startup.sh is called with a default paramater if none is provided by the user.

My startup.sh is this one:

#!/bin/bash

# init database etc.
python3 /root/boot.py $1
# snmpd
/usr/sbin/snmpd -f -Lo &
# logger
tail -f /var/log/mylog.log

Now I want to simply call cleanup.py whenever a SIGTERM is received. I saw a few examples but my knowledge about the bash syntax is too little to actually understand and transfer onto my situation. I understand that I need a SIGTERM trap, but how do I actually call the cleanup script a wait until it's finished?

Thanks in advance! Markus


Solution

  • Have you tried this method?

    If it works, you can just change what's inside the function to call your cleanup script.