简体   繁体   English

Docker杀死容器内的进程

[英]Docker kill process inside container

I exec into Docker container with docker exec -it container-name bash 我使用docker exec -it container-name bash进入Docker容器

Inside container I run command ps aux | grep processName 在容器内部,我运行命令ps aux | grep processName ps aux | grep processName

I receive a PID and after that I run: 我收到一个PID,然后运行:

kill processId but receive: kill processId但收到:

-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? 我知道Docker从主机内部的top命令和容器内部的ps aux显示不同的进程ID( 如何在容器内部杀死进程?Docker top命令 ),但是我是在容器内部运行的?

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): 例如,如果启动ps aux ,则可以在容器内获得类似这样的输出(当然这取决于容器):

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. 然后,如果您尝试使用PID 15终止进程,则会收到错误,因为PID 15在尝试终止它的时刻已完成。 The ps command terminates after showing you the processes info. 向您显示进程信息后,ps命令终止。 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). 在docker容器中,除了根进程(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. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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