Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot kill pid 1 inside docker container with SIGKILL

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.

like image 621
Pooya Avatar asked Dec 03 '16 12:12

Pooya


People also ask

What happens if you kill PID 1?

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.

How do I kill a process in docker container?

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.

How do I force kill a docker container?

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.


1 Answers

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.

like image 155
eljefedelrodeodeljefe Avatar answered Oct 06 '22 01:10

eljefedelrodeodeljefe