简体   繁体   中英

Docker kill process inside container

I exec into Docker container with docker exec -it container-name bash

Inside container I run command ps aux | grep processName ps aux | grep processName

I receive a PID and after that I run:

kill processId but receive:

-bash: kill: (21456) - No such process

Am I missing something or? I know that Docker shows different process IDs from top command inside the host and ps aux inside the container ( How to kill process inside container? Docker top command ), but I am running this from inside container?

That response is because the process you are trying to kill is not existing at the moment of killing it. For example, if you launch ps aux you can get an output like this inside a container (it depends of the container of course):

oot@69fbbc0ff80d:/# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  18400  3424 pts/0    Ss   13:55   0:00 bash
root        15  0.0  0.0  36840  2904 pts/0    R+   13:57   0:00 ps aux

Then if you try to kill process with PID 15 you'll get the error because PID 15 is finished at the moment of trying to kill it. The ps command terminates after showing you the processes info. So:

root@69fbbc0ff80d:/# kill 15
bash: kill: (15) - No such process

In a docker container you can kill process in the same way as normal excepting the root process (id 1). You can't kill it:

root@69fbbc0ff80d:/# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  18400  3424 pts/0    Ss   13:55   0:00 bash
root        16  0.0  0.0  36840  2952 pts/0    R+   13:59   0:00 ps aux
root@69fbbc0ff80d:/# kill 1
root@69fbbc0ff80d:/# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  18400  3424 pts/0    Ss   13:55   0:00 bash
root        17  0.0  0.0  36840  2916 pts/0    R+   13:59   0:00 ps aux

As you can see you can't kill it. Anyway if you want to proof that you can kill processes you can do:

root@69fbbc0ff80d:/# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  18400  3424 pts/0    Ss   13:55   0:00 bash
root        18  0.0  0.0  36840  3064 pts/0    R+   14:01   0:00 ps aux
root@69fbbc0ff80d:/# sleep 1000 &
[1] 19
root@69fbbc0ff80d:/# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  18400  3424 pts/0    Ss   13:55   0:00 bash
root        19  0.0  0.0   4372   724 pts/0    S    14:01   0:00 sleep 1000
root        20  0.0  0.0  36840  3016 pts/0    R+   14:01   0:00 ps aux
root@69fbbc0ff80d:/# kill 19
root@69fbbc0ff80d:/# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  18400  3424 pts/0    Ss   13:55   0:00 bash
root        21  0.0  0.0  36840  2824 pts/0    R+   14:01   0:00 ps aux
[1]+  Terminated              sleep 1000

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM