Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a script when I terminate a docker container

Tags:

docker

I want when I type on my console:

docker ^a docker container^ stop

To execute a script before terminating. is that possible?

like image 386
Dimitrios Desyllas Avatar asked Jan 03 '17 19:01

Dimitrios Desyllas


People also ask

What happens when you stop docker container?

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.

What is the command to exit a container without killing the process?

Docker supports a keyboard combination to gracefully detach from a container. Press Ctrl-P, followed by Ctrl-Q, to detach from your connection.

What is docker kill command?

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.


2 Answers

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
like image 70
zeppelin Avatar answered Oct 23 '22 23:10

zeppelin


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"
like image 1
Torben E Avatar answered Oct 24 '22 00:10

Torben E