bash 如果 docker 容器正在运行,则停止并删除它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34228864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
stop and delete docker container if its running
提问by Robbo_UK
I am looking to pragmatically stop and delete a docker container if it is running. This is for a build script.
我希望务实地停止并删除正在运行的 docker 容器。这是一个构建脚本。
Take the following example. How would I stop and delete the docker container "rabbitmq" as seen under the NAMES column in a bash script.
以下面的例子为例。我将如何停止和删除 docker 容器“rabbitmq”,如 bash 脚本的 NAMES 列下所示。
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9909a5e2856f rabbitmq-image "/docker-entrypoint.s" 11 minutes ago Up 11 minutes 0.0.0.0:5672->5672/tcp, rabbitmq
8990dd1fe503 redis-image "/entrypoint.sh redis" 6 weeks ago Up 4 days 0.0.0.0:32770->6379/tcp redis
etc
The following command will delete the container and does what im looking to do
以下命令将删除容器并执行我希望执行的操作
docker stop rabbitmq && docker rm -f rabbitmq
However its combing it into a script that I would like to know?I think it would look something like this.
然而它把它组合成一个我想知道的脚本?我认为它看起来像这样。
#!/bin/bash
if [ /*docker ps check some value */ ]; then
docker stop rabbitmq && docker rm -f rabbitmq
fi
回答by Johannes Barop
As you have probably noticed, docker stop
as well as docker rm
exit with a status code indicating failure if the container is not existent or not running. This results in your build failing.
您可能已经注意到,如果容器不存在或未运行docker stop
,则docker rm
退出并显示一个指示失败的状态代码。这会导致您的构建失败。
If you can cope with the error messages in your build log you can do this little trick to prevent the shell command of failing:
如果您可以处理构建日志中的错误消息,您可以使用这个小技巧来防止 shell 命令失败:
docker stop rabbitmq || true && docker rm rabbitmq || true
In the case that one of the docker command fails, true
is called which always exits with a status code indicating success.
在 docker 命令之一失败的情况下,true
调用它总是以指示成功的状态代码退出。
回答by ncoghlan
I have a similar problem, but didn't like the accepted answer as it suppresses allerrors from the commands, rather than just the "not found" error.
我有一个类似的问题,但不喜欢接受的答案,因为它抑制了命令中的所有错误,而不仅仅是“未找到”错误。
However, docker ps -q --filter "name=rabbitmq"
only produces output if a container of that name actually exists, so inspired by Test if a command outputs an empty stringI came up with:
但是,docker ps -q --filter "name=rabbitmq"
仅当该名称的容器实际存在时才产生输出,因此受到Test if a commandoutput a empty string我想出的启发:
docker ps -q --filter "name=rabbitmq" | grep -q . && docker stop rabbitmq && docker rm -fv rabbitmq
The following command is also useful for testing filter definitions:
以下命令也可用于测试过滤器定义:
docker ps -q --filter "name=rabbitmq" | grep -q . && echo Found || echo Not Found
My actual use case was in defining a pair of Ansible tasks that deleted all currently existing containers (whether running or not) from a list of names generated in an earlier task:
我的实际用例是定义一对 Ansible 任务,这些任务从先前任务中生成的名称列表中删除所有当前存在的容器(无论是否正在运行):
- name: Check for containers that actually exist
shell: 'docker ps -aq --filter "name={{ item }}"'
with_items:
- '{{ previous_command.stdout_lines }}'
register: found_containers
- name: Remove the containers found by the above command
shell: 'docker stop {{ item.item }} && docker rm -fv {{ item.item }}'
with_items: '{{ found_containers.results }}'
when: item.stdout
回答by Robbo_UK
Been using docker for a while longer now. This is my preferred way to stop and remove a docker container. The piping of true is there to ensure the it always outputs a success. Without it any bash scripts would exit and error if the container name did not exist.
现在使用 docker 已经有一段时间了。这是我停止和删除 docker 容器的首选方法。true 的管道是为了确保它总是输出成功。没有它,如果容器名称不存在,任何 bash 脚本都会退出并出错。
docker rm -f container_name || true
回答by anubhava
You can use:
您可以使用:
app="rabbitmq"
if docker ps | awk -v app="$app" 'NR > 1 && $NF == app{ret=1; exit} END{exit !ret}'; then
docker stop "$app" && docker rm -f "$app"
fi
awk
command gets a command line varapp
from BASH's variable$app
NR>1
skips first header row fromdocker ps
command.$(NF) == app
Compare last columnNAMES
is equal to app variable or not
awk
命令app
从 BASH 的变量中获取命令行变量$app
NR>1
跳过docker ps
命令中的第一个标题行。$(NF) == app
比较最后一列NAMES
是否等于 app 变量
回答by iver56
# Stop and remove containers with names like "rabbitmq" and "rabbitmq123" if they exist
CONTAINER_NAME="rabbitmq"
OLD="$(docker ps --all --quiet --filter=name="$CONTAINER_NAME")"
if [ -n "$OLD" ]; then
docker stop $OLD && docker rm $OLD
fi
回答by DS.
I suggest this incantation in bash:
我建议在 bash 中使用这个咒语:
( docker stop $CONTAINER > /dev/null && echo Stopped container $CONTAINER && \
docker rm $CONTAINER ) 2>/dev/null || true
It always exits with 0, doesn't complain if the container is not running, and prints Stopped container $CONTAINER
if it actually got stopped.
它总是以 0 退出,如果容器没有运行就不会抱怨,Stopped container $CONTAINER
如果它真的停止了就打印。
回答by Christoph Schranz
A general form based on some answers here:
基于此处一些答案的一般形式:
docker rm -f container_name > /dev/null 2>&1 && echo 'removed container' || echo 'nothing to remove'
docker rm -f container_name > /dev/null 2>&1 && echo 'removed container' || echo 'nothing to remove'
回答by Alex Montoya
Copy this code in your script.sh if you want stop
and remove
all
如果你想在这个script.sh代码复制stop
和remove
所有
#!/bin/sh
ids=$(docker ps -a -q)
for id in $ids
do
echo "$id"
docker stop $id && docker rm $id
done
回答by Svend
If you do not delete your stopped containers, another simple way to address this is to rely on docker ps -a
, which will always return that container id. Then executing docker stop
on that stopped container will idempotently simply do nothing:
如果您不删除已停止的容器,另一种解决此问题的简单方法是依赖docker ps -a
,它将始终返回该容器 ID。然后docker stop
在那个停止的容器上执行将幂等地简单地什么都不做:
docker stop $(docker ps -a --filter name= rabbitmq -q )
docker stop $(docker ps -a --filter name= rabbitmq -q )
回答by Gustavo Mu?oz Valenzuela
to stop all containers first you have to stop all containers with
要先停止所有容器,您必须使用以下命令停止所有容器
docker kill $(docker ps -q)
and to delete all containers
并删除所有容器
docker rm $(docker ps -a -q)
and if you want delete all images this is the command
如果你想删除所有图像,这是命令
docker rmi $(docker images -q)