Docker kill process inside container

随声附和 提交于 2019-12-11 00:19:55

问题


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

Inside container I run command 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?


回答1:


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.



来源:https://stackoverflow.com/questions/46910412/docker-kill-process-inside-container

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!