I want when I type on my console:
docker ^a docker container^ stop
To execute a script before terminating. is that possible?
When you use docker stop or docker kill to signal a container, that signal is sent only to the container process running as PID 1. Since /bin/sh doesn't forward signals to any child processes, the SIGTERM we sent never reached our script.
Docker supports a keyboard combination to gracefully detach from a container. Press Ctrl-P, followed by Ctrl-Q, to detach from your connection.
Description. The docker kill subcommand kills one or more containers. The main process inside the container is sent SIGKILL signal (default), or the signal that is specified with the --signal option. You can reference a container by its ID, ID-prefix, or name.
The docker stop command attempts to stop a running container first by sending a SIGTERM signal to the root process (PID 1) in the container. If the process hasn't exited within the timeout period a SIGKILL signal will be sent.
In practice, that means that you have to define an ENTRYPOINT script, which will intercept (trap) the SIGTERM signal and execute any shutdown logic as appropriate.
A sample entrypoint script can 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 $!
(shell signal handling, with respect to wait, is explained in a bit more details here)
Note, that with the entrypoint above, the cleanup logic will only be executed if container is stopped explicitly, if you wish it to also run when the underlying process/command stops by itself (or fails), you can restructure it as follows.
...
#Trap SIGTERM
trap 'true' SIGTERM
#Execute command
"${@}" &
#Wait
wait $!
#Cleanup
cleanup
I worked way too long on getting this to work. Here is a solution that worked for me (based on the other response given):
docker-compose.yml
version: "3.8"
services:
your_service:
image: alpine
entrypoint: ["/bin/sh", "./shutdown_handler.sh"]
shutdown_handler.sh
#!/bin/bash
echo "Waiting for signal..."
echo "pid=$$"
# Sleeping in the background seems to do the job
sleep infinity &
wait $!
# ADD YOUR CODE HERE
echo "Received arbitrary signal"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With