A dockerized trap that waits for a SIGTERM (docker stop / docker kill) and executes some code then.
918
This image implements a docker trap which waits for a SIGTERM (docker stop / docker kill) and executes some code after receiving.
My use case is to run some code on RancherOS system termination. RancherOS sends a SIGTERM to all system docker services. The trap within this image waits for it and executes a script which have to be mounted into the running container.
Make sure to mount the script you want to execute to /run_trap_code.sh. For example:
docker run -v /your/script/on/host:/run_trap_code.sh ...
cat >/var/lib/rancher/conf/trap.yml<<'EOF'
trap:
image: mminks/trap:latest
restart: always
volumes:
- /usr/local/bin/your_local_script.sh:/run_trap_code.sh
EOF
/usr/local/bin/your_local_script.sh is the script that is going to be executes.
If you want to send a message to a Slack channel in case of termination, add some environment variables:
...
environment:
- SLACK_URL=https://hooks.slack.com/services/123456789
- SLACK_CHANNEL=your-cool-channel
- SLACK_USERNAME=your-cool-user
Specify the channel name without #.
sudo ros service enable /var/lib/rancher/conf/trap.yml
sudo ros service up trap
Content type
Image
Digest
Size
78.2 MB
Last updated
over 6 years ago
docker pull mminks/trap