I want to run some script inside the container when I execute the

docker stop ${ContainerName}

Is there any way of achieving that?


That's a great question man.

First of all, docker stop sends a kind SIGTERM to the root process inside your container. And by default, your process has ten seconds before Docker send a no-mercy SIGKILL, killing it. [1]

Second, a robust application shouldn't corrupt data if the process was unexpectedly killed. For example, Nginx has well documented way to handle system signals. [2]

But in case your're making your onw application, and that might be even a Shell script, you'll need to implement your own signal handling. Just a few lines of code and you're done. Here's a nice tutorial. [3]

Have a nice day.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.