Reputation: 216
Let's say I want to execute a cleanup script whenever container termination is triggered. How do I go about this using docker-compose?
This could be handy to automatically back up the files, databases, etc for the dev container.
Upvotes: 7
Views: 4172
Reputation: 8933
docker
containers are meant to be ephemeral:
By "ephemeral", we mean that the container can be stopped and destroyed, then rebuilt and replaced with an absolute minimum set up and configuration.
Building upon this concept docker
itself does not offer anything to hook into the shutdown process. docker-compose
is built on top of docker
and also does not add such functionality.
Maybe you can rethink your problem the docker
way to better fit the intended use of docker
. Without further context it is hard to say what could be a good solution but maybe one of the following approaches helps you out:
docker stop
sends a SIGTERM
signal to the main process in the container. You could use a custom entrypoint or supervisor process that would trigger the appropriate actions on a SIGTERM
. This approach requires custom containers. With the stop_signal
attribute you can also configure a custom signa to be sent in your docker-compose.yml
if you just want to persist data files from the containers just configuring the right volumes
might be enough
you could use docker events
to listen and act upon any types of events emitted by the docker daemon
Upvotes: 4