<p>PID 1 is a special PID which will ignore certain signals unless handlers are explicitly created. Docker has some documentation on this:</p>

<blockquote>
  <p>Note: A process running as PID 1 inside a container is treated specially by Linux: it ignores any signal with the default action. So, the process will not terminate on SIGINT or SIGTERM unless it is coded to do so.</p>
</blockquote>

<p>From here: <a href="https://docs.docker.com/engine/reference/run/#foreground" rel="noreferrer">https://docs.docker.com/engine/reference/run/#foreground</a></p>

<p>To kill PID 1 you will have to explicitly declare the handler for the SIGTERM signal or, in current versions of Docker, pass the <code>--init</code> flag in the <code>docker run</code> command to instrument <a href="https://github.com/krallin/tini" rel="noreferrer">tini</a>.</p>

CC BY-SA 4.0
2018-12-06T16:30:47.603Z
Thank you very much @edaemon, especially for having mentioned the `--init` flag I wasn't aware of!
Sylvain Leroux
CC BY-SA 4.0
2018-12-05T20:49:59.007Z
2018-12-05T20:58:45.393Z
2018-12-05T20:58:45.393Z
edaemon
<p>Also have this problem, solving by use follwing docker-entrypoint.sh</p>
<pre><code>#!/bin/sh
set -e
trap &quot;trap - SIGTERM &amp;&amp; kill -- -$$&quot; SIGINT SIGTERM EXIT SIGHUP

some_app &amp;
wait
</code></pre>
<p>You can 'kill 1' somewhere to exit this whole container.</p>

CC BY-SA 4.0
2021-07-03T02:56:54.020Z
What does it help to run it the background and then `wait` on it?  Why not just run it?
chicks
CC BY-SA 4.0
2023-03-28T09:17:55.023Z
amazing! this is the only solution that works
Alex
CC BY-SA 4.0
2021-06-30T15:44:27.393Z
2021-06-30T15:44:27.393Z
lenew
<p>For demonstration purposes, I would like to kill the process with PID 1 from inside a container. But apparently, <code>kill 1</code> doesn't work:</p>

<pre><code>$ sudo docker run -it centos
[root@3752d3a44f10 /]# ps
  PID TTY          TIME CMD
    1 pts/0    00:00:00 bash
   15 pts/0    00:00:00 ps
[root@3752d3a44f10 /]# kill 1
[root@3752d3a44f10 /]# kill -9 1
[root@3752d3a44f10 /]# kill -SEGV 1
[root@3752d3a44f10 /]# &lt;-- shell is still running
</code></pre>

<p>Is there a way to explicitly kill the process with PID 1 from inside a container? How?</p>

<docker>
How to explicitly kill the process with PID 1 from inside a container?
Sylvain Leroux
# How to explicitly kill the process with PID 1 from inside a container?

(Asked by: username_0 on 2018-12-04)

For demonstration purposes, I would like to kill the process with PID 1 from inside a container. But apparently, kill 1 doesn't work:
$ sudo docker run -it centos
[root@3752d3a44f10 /]# ps
  PID TTY          TIME CMD
    1 pts/0    00:00:00 bash
   15 pts/0    00:00:00 ps
[root@3752d3a44f10 /]# kill 1
[root@3752d3a44f10 /]# kill -9 1
[root@3752d3a44f10 /]# kill -SEGV 1
[root@3752d3a44f10 /]# <-- shell is still running

Is there a way to explicitly kill the process with PID 1 from inside a container? How?

**Tags:** docker


---

## 2 answers

### Answer by username_1 on 2018-12-05. Score: 15 | Accepted answer

PID 1 is a special PID which will ignore certain signals unless handlers are explicitly created. Docker has some documentation on this:

> Note: A process running as PID 1 inside a container is treated specially by Linux: it ignores any signal with the default action. So, the process will not terminate on SIGINT or SIGTERM unless it is coded to do so.

From here: <https://docs.docker.com/engine/reference/run/#foreground>

To kill PID 1 you will have to explicitly declare the handler for the SIGTERM signal or, in current versions of Docker, pass the `--init` flag in the `docker run` command to instrument [tini](https://github.com/krallin/tini).

> **Comments:**
> **username_0**: Thank you very much @username_1, especially for having mentioned the `--init` flag I wasn't aware of!  

---

### Answer by username_2 on 2021-06-30. Score: 2

Also have this problem, solving by use follwing docker-entrypoint.sh
#!/bin/sh
set -e
trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT SIGHUP

some_app &
wait

You can 'kill 1' somewhere to exit this whole container.

> **Comments:**
> **username_3**: What does it help to run it the background and then `wait` on it?  Why not just run it?  
> **username_4**: amazing! this is the only solution that works  

---

