Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a script when docker is stopped

I am trying to create docker container using dockerfile where script-entry.sh is to be executed when the containers starts and script-exit.sh to be executed when the container stops.

ENTRYPOINT helped to accomplish the first part of the problem where script-entry.sh runs on startup.

How will i make sure the script-exit.sh is executed on docker exit/stop ?

like image 209
B_B Avatar asked Mar 05 '16 00:03

B_B


People also ask

How do I run a stopped docker container?

For containers that are stopped, you can also start the container using the Docker start command and then run the Docker exec command.

Can the docker exec command be run on stopped containers?

When we try to run /bin/sh on a stopped container using docker exec , Docker will throw a No such container error. We have to transform the stopped Docker container into a new Docker image before we can inspect the internals of the container. We can transform a container into a Docker image using the commit command.

What happens when docker container stops?

When this happens, the program will stop, and the container will exit. The container has been stopped using docker stop : You can manually stop a container using the docker stop command. The Docker daemon has restarted, and it terminated and restarted the container: Docker can restart containers if you need it to.


1 Answers

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

See This link for explanation on signal trapping and an example (near the end of the page)

like image 85
Xiongbing Jin Avatar answered Sep 19 '22 11:09

Xiongbing Jin