Docker杀死容器内的进程 [英] Docker kill process inside container

查看:2816
本文介绍了Docker杀死容器内的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用docker exec -it container-name bash

在内部容器中,运行命令ps aux | grep processName

Inside container I run command ps aux | grep processName

我收到PID,然后运行:

I receive a PID and after that I run:

kill processId但收到:

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

我错过了什么吗?我知道Docker从主机内部的top命令和容器内部的ps aux命令显示了不同的进程ID(

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?

推荐答案

该响应是因为您要终止的进程在终止时不存在.例如,如果启动ps aux,则可以在容器内获得这样的输出(当然,这取决于容器):

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

然后,如果您尝试使用PID 15终止进程,则会收到错误,因为PID 15在尝试终止它的时刻已完成.向您显示进程信息后,ps命令终止.所以:

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

在docker容器中,除了根进程(id 1)外,您可以通过与正常方式相同的方式杀死进程.您无法杀死它:

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

希望有帮助.

这篇关于Docker杀死容器内的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆