gpt4 book ai didi

docker - 如何删除所有Docker容器?

转载 作者:行者123 更新时间:2023-12-02 18:10:59 26 4
gpt4 key购买 nike

我使用以下命令删除所有docker容器:

docker ps -q | xargs docker stop
docker ps -aq --no-trunc -f status=exited | xargs docker rm

但是无论如何,我在之后看到了容器:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
70cb7107d00d 24820714bfc6 "wait-for-it.sh mysqâ¦" 21 minutes ago Created sql_migration

然后我执行了命令
 docker rm sql_migration

然后,它取下了容器。

您能否帮助纠正初始命令并解释为什么它不起作用。

如果您能解释如何将容器更改为 sql_migration之类的状态,我也将不胜感激

最佳答案

1.删​​除可以删除的内容...
要删除所有exitedcreated容器,但不删除Up(正在运行)的容器,请执行以下操作:

docker container prune -f
-f--force可防止提示您回答以下问题:
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N]

示例:(最后请注意,未删除一个容器)
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
053ce57276b7 alpine "/bin/sh" 3 seconds ago Created eager_meninsky
55f20431a536 alpine "/bin/sh" 15 seconds ago Up 14 seconds hardcore_clarke
44cbe2dc81b0 alpine "/bin/sh" 38 seconds ago Exited (0) 37 seconds ago test
647747afb9a4 alpine "/bin/sh" 18 hours ago Exited (137) 18 hours ago admiring_visvesvaraya


$ docker container prune -f
Deleted Containers:
053ce57276b7d7008272e95991cf950268b9b32676b1389ec6e8ab6e6b755dc9
44cbe2dc81b0522e0fd0e53f28a4d3871b818b9b17681dd011c8680ab37b51e7
647747afb9a431a2c5040e6aba5119b199b94061c444ff0194aaa809dbf849b8

Total reclaimed space: 0B


$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
55f20431a536 alpine "/bin/sh" 45 seconds ago Up 44 seconds hardcore_clarke

2.全部停止并删除...
docker container stop $(docker container ls -aq)
docker container prune -f

示例:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da7ffd8efb62 alpine "/bin/sh" 5 seconds ago Created quirky_poitras
31cb71a8899c alpine "/bin/sh" 20 seconds ago Exited (0) 13 seconds ago elastic_shaw
becfdc81228c alpine "/bin/sh" 25 seconds ago Up 24 seconds thirsty_murdock


$ docker container stop $(docker container ls -aq)
da7ffd8efb62
31cb71a8899c
becfdc81228c


$ docker container prune -f
Deleted Containers:
da7ffd8efb623677882b1534008e66a1530baa94e0473be537ef5c415c928ba3
31cb71a8899c47472d0ccb5710e34ff08b4ef142599d4e857e3e69740a2e59b5
becfdc81228cdf41519102ea780956eed71a86103e849dff3d9f7cca0a54651f

Total reclaimed space: 5B


$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

关于docker - 如何删除所有Docker容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52073000/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com