按图像名称停止 Docker 容器 - Ubuntu [英] Stopping Docker containers by image name - Ubuntu

查看:39
本文介绍了按图像名称停止 Docker 容器 - Ubuntu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Ubuntu 14.04 (Trusty Tahr) 上,我正在寻找一种停止正在运行的容器的方法,我拥有的唯一信息是 Docker 运行命令中使用的映像名称.

On Ubuntu 14.04 (Trusty Tahr) I'm looking for a way to stop a running container and the only information I have is the image name that was used in the Docker run command.

是否有命令可以找到所有匹配该映像名称的正在运行的容器并停止它们?

Is there a command to find all the matching running containers that match that image name and stop them?

推荐答案

如果你知道 image:tag 确切的容器版本

issue 8959 之后,一个好的开始是:

If you know the image:tag exact container version

Following issue 8959, a good start would be:

docker ps -a -q --filter="name=<containerName>"

由于 name 指的是 container 而不是图像名称,您需要使用更新的 Docker 1.9 过滤器祖先,在 koekie 中提到的答案.

Since name refers to the container and not the image name, you would need to use the more recent Docker 1.9 filter ancestor, mentioned in koekiebox's answer.

docker ps -a -q  --filter ancestor=<image-name>


正如下面 kiril 所评论的,移除这些容器:


As commented below by kiril, to remove those containers:

stop 也返回容器.

所以链接 stoprm 就可以了:

So chaining stop and rm will do the job:

docker rm $(docker stop $(docker ps -a -q --filter ancestor=<image-name> --format="{{.ID}}"))


如果你只知道图片名称(而不是image:tag)

正如 Alex Jansen评论:

祖先选项不支持通配符匹配.>

The ancestor option does not support wildcard matching.

Alex 提出了一个解决方案,但我设法运行了一个解决方案,当你有多个 从同一个镜像运行的容器是(例如在你的 ~/.bashrc 中):

Alex proposes a solution, but the one I managed to run, when you have multiple containers running from the same image is (in your ~/.bashrc for instance):

dsi() { docker stop $(docker ps -a | awk -v i="^$1.*" '{if($2~i){print$1}}'); }

然后我只调用我的 bash 会话(在获取 ~/.bashrc 之后):

Then I just call in my bash session (after sourcing ~/.bashrc):

dsi alpine

任何从 alpine.*:xxx 运行的容器都会停止.

And any container running from alpine.*:xxx would stop.

含义:名称为alpine开头的任何图像.
如果您希望 ^$1.* 更精确,您可能需要调整 awk -v i=^$1.*".

Meaning: any image whose name is starting with alpine.
You might need to tweak the awk -v i="^$1.*" if you want ^$1.* to be more precise.

当然是从那里:

drmi() { docker rm $(dsi $1  | tr '
' ' '); }

drmi alpine 会停止并移除任何 alpine:xxx 容器.

And a drmi alpine would stop and remove any alpine:xxx container.

这篇关于按图像名称停止 Docker 容器 - Ubuntu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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