Sign inSign up

mminks/trap

By mminks

Updated over 6 years ago

A dockerized trap that waits for a SIGTERM (docker stop / docker kill) and executes some code then.

Image
0

918

mminks/trap repository overview

Docker Trap

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 ...

In RancherOS you'll need this to install the trap:
1. Create a docker-compose.yml
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 #.

2. Enable the trap

sudo ros service enable /var/lib/rancher/conf/trap.yml

3. Start the trap

sudo ros service up trap

Tag summary

Content type

Image

Digest

Size

78.2 MB

Last updated

over 6 years ago

docker pull mminks/trap