Run a script when docker is stopped

I am trying to create a docker container using dockerfile where script -entry.sh should be run when the containers are running and script -exit.sh will be executed when the container stops.

ENTRYPOINT helped complete the first part of the problem when script -entry.sh is started at startup.

How can I make sure script -exit.sh is executed on docker exit / stop?

+6
source share
2 answers

docker stop sends a SIGTERM signal to the main process running inside the Docker container (script entry). So you need a way to catch the signal and then run the script output.

See this link for an explanation of signal capture and an example (closer to the end of the page)

+8
source

Create a script and save it as a bash file that contains the following:

 $CONTAINER_NAME="someNameHere" docker exec -it $CONTAINER_NAME bash -c "sh script-exit.sh" docker stop $CONTAINER_NAME 

Run this file instead of running docker stop , and this should do the trick. You can also set up an alias.

As for automating it inside Docker itself, I've never seen this before. Good luck figuring this out if this is the road you want to take.

0
source

All Articles