6

I have a docker container running CentOS 7.

It is running a script wrapper_script.sh as PID 1. It also has below in the docker compose.

CMD ["/wrapper_script.sh"]

I tried docker stop on the container. It is not executing the cleanup function. It is directly stopping the container.

Tried docker kill --signal="SIGTERM" <containername>. It is also not executing the cleanup function. Also note, this is not killing/stopping the container itself.

Please help on fixing this. Any suggestion is appreciated.

wrapper_script.sh is as below:

#!/usr/bin/env bash
cleanup() {
    echo "Cleaning up..."
    /wrapper-script-stop.sh
}
trap 'cleanup' SIGTERM EXIT
/run-something.sh
while sleep 60; do
  ps aux |grep run-something.sh |grep -q -v grep
  PROCESS_1_STATUS=$?
  if [ $PROCESS_1_STATUS -ne 0 ]; then
    echo "One of the processes has already exited."
    exit 1
  fi
done

1 Answer 1

1

I don't understand the connection between the bash script and the question, but here is some information about stopping a container.

When you issue a docker stop command, Docker will first ask nicely for the process to stop and if it doesn't comply within the default 10 seconds it will forcibly be killed. If your docker stop takes more than 10 seconds to return, this means that your container didn't respond within this time.

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 doesn't exit within this timeout, a SIGKILL signal will be sent.

It is possible that your process is choosing to ignore the SIGTERM signal, but it cannot ignore a SIGKILL. After the SIGKILL is issued, the process will never even see the signal, so cannot do any cleanup action.

You can control the number of seconds that the Docker daemon will wait before sending the SIGKILL. If your process needs more time to finish, you could use a command such as:

docker stop --time=30 mycontainer

If the process is hung, you should find out why, if it absolutely needs to gracefully terminate for this cleanup action to be done.

5
  • Thanks for response. But I understand all this. Question is why is the SIGTERM not captured by the script..wrapper_script.sh is the PID 1 in the container. That is started by CMD ["/wrapper_script.sh"]. Commented Apr 23, 2021 at 16:44
  • In that case, you could try to set a trap for the SIGTERM signal. See link.
    – harrymc
    Commented Apr 23, 2021 at 17:00
  • The script I shared is supposed to trap SIGTERM but it is not working. Need to understand what is the issue Commented Apr 24, 2021 at 22:51
  • I note that your cleanup lacks an exit command, which is included in most examples. This post may be useful.
    – harrymc
    Commented Apr 25, 2021 at 9:50
  • That is not an issue. The CTRL+C is successfully working and trapping EXIT and performing the cleanup. Commented May 3, 2021 at 11:58

You must log in to answer this question.

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