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

docker stop ${ContainerName}

Is there any way of achieving that?

1 Answer 1


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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .