How to execute a script when I complete a docker container

I want when I type in my console:

docker ^a docker container^ stop 

To execute the script before terminating. perhaps?

+5
source share
1 answer

The docker stop command tries to stop a running container from the beginning by sending a SIGTERM signal to the root process (PID 1) in the container. If the process has not timed out, a SIGKILL signal will be sent.

In practice, this means that you need to define an ENTRYPOINT script that will intercept (trap) the SIGTERM signal and, if necessary, execute any shutdown logic.

An example script entry point might look something like this:

 #!/bin/bash #Define cleanup procedure cleanup() { echo "Container stopped, performing cleanup..." } #Trap SIGTERM trap 'cleanup' SIGTERM #Execute a command "${@}" & #Wait wait $! 

(processing of the shell signal in relation to the wait is explained in more detail here )

Please note that with the above entry point, the cleaning logic will only be executed if the container is stopped explicitly, if you want it to also be executed when the main process / command stops on its own (or does not work), you can restructure him as follows.

 ... #Trap SIGTERM trap 'true' SIGTERM #Execute command "${@}" & #Wait wait $! #Cleanup cleanup 
+8
source

Source: https://habr.com/ru/post/1262237/


All Articles