Docker 终止容器内的进程

Docker kill process inside container

我用 docker exec -it container-name bash

执行到 Docker 容器

容器内 I 运行 命令 ps aux | grep processName

我收到一个 PID,然后我 运行:

kill processId 但收到:

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

我是不是遗漏了什么?我知道 Docker 显示来自主机内的 top 命令和容器内的 ps aux 的不同进程 ID (),但我 运行在容器内?

该响应是因为您试图终止的进程在终止时不存在。例如,如果你启动 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

然后,如果您尝试终止 PID 15 的进程,您将收到错误消息,因为 PID 15 在尝试终止它时已完成。 ps 命令在显示进程信息后终止。所以:

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

在 docker 容器中,除了根进程 (id 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        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

如你所见,你无法杀死它。无论如何,如果你想证明你可以杀死你可以做的进程:

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

希望对您有所帮助ps。