如何杀死容器内的进程

如何杀死容器内的进程

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

问题描述

我有一个来自docker网站官方指南的简单示例.

I have simple example from official guide at docker website.

我运行以下命令:

sudo docker run -d ubuntu:latest /bin/sh -c "while true; do echo hello world; sleep 1; done"
a66asdasdhqie123...

然后从创建的容器中获取一些输出:

Then take some output from created container:

sudo docker logs a66
hello
hello
hello
...

然后我查找容器的运行过程:

Then I lookup the running processes of a container:

sudo docker top a66
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                25055               15152               0                   20:07               ?                   00:00:00            /bin/sh -c while true; do echo hello world; sleep 1; done
root                25295               25055               0                   20:10               ?                   00:00:00            sleep 1

接下来,我尝试杀死容器的第一个过程:

Next I try to kill the first process of container:

sudo docker exec a66 kill -9 25055

但是,在我将其更改之后,没有任何改变.进程仍然有效,并且每秒输出"hello".我怎么了?

However after I make it nothing changes. Process still works and output "hello" every second. What do I wrong?

推荐答案

当重现您的情况时,我看到docker top <container>docker exec -it <container> ps -aux之间的PID不同.当您执行docker exec时,该命令在container =>内部执行,应使用容器的pid.否则,在没有docker的情况下,您可以直接从主机执行杀死操作,sudo kill -9 25055.

When I reproduce your situation I see different PIDs between docker top <container> and docker exec -it <container> ps -aux. When you do docker exec the command is executed inside the container => should use container's pid. Otherwise you could do the kill without docker straight from the host, in your case: sudo kill -9 25055.

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

08-07 09:27