For a reason, I want to kill the main python process ( PID 1 ) in docker container. But non of the terminating signals such as SIGTERM
, SIGKILL
work. I mean, running kill -SIGKILL 1
has no effect.
How can I kill the pid 1 from the inside of container? I do not want to run docker stop
or similar solutions.
Since it's impossible to set a handler for SIGKILL, a SIGKILL would never be delivered to PID 1. The process who sends the signal, however, would get 0 return code, indicating that everything went fine. On Ubuntu 20.04, it causes the screen to freeze instantly.
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.
docker rm -f The final option for stopping a running container is to use the --force or -f flag in conjunction with the docker rm command. Typically, docker rm is used to remove an already stopped container, but the use of the -f flag will cause it to first issue a SIGKILL.
According to the Docker issue tracker and how the general documentation of pid 1s state, you need to specifically add a handler to the signals and kill the process from them.
signal.signal(signal.SIGINT, exit_gracefully)
signal.signal(signal.SIGTERM, exit_gracefully)
exit_gracefully
needs to be defined and eventually call sys.exit(0)
.
This behaviour is wanted and enforced by the kernel as it will not call the sigaction
default, which is is termination, on the other pids.
We need to do something similar in Node.js.
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