docker 如何移除所有码头货柜?

mi7gmzs6  于 2023-01-16  发布在  Docker
关注(0)|答案(5)|浏览(125)

我使用以下命令删除所有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

它拿走了容器。
你能帮助纠正初始命令并解释为什么它不工作吗?
另外,如果您能解释一下如何将container更改为sql_migration这样的状态,我将不胜感激

jhdbpxl9

jhdbpxl91#

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
evrscar2

evrscar22#

我使用以下命令删除所有Docker容器:
docker rm $(docker ps -aq)

dxpyg8gm

dxpyg8gm3#

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

docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
nuypyhwy

nuypyhwy4#

一个内衬用于停止/取出所有Docker容器:
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

s2j5cfk0

s2j5cfk05#

情况1.为了仅移除已加塞容器,请使用sudo docker container prune -f

$ docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
4a7f7eebae0f63178aff7eb0aa39cd3f0627a203ab2df258c1a00b456cf20063
f98f9c2aa1eaf727e4ec9c0283bc7d4aa4762fbdba7f26191f26c97f64090360

Total reclaimed space: 212 B

用例2.为了删除系统上的所有现有Docker容器(在所有状态下-运行/停止/退出容器),使用sudo docker container ls -aq | xargs sudo docker container rm

命令说明

  • sudo container ls -aq-按容器ID筛选器列出系统上的所有容器。
  • xargs-在管道输出上迭代并在每行上执行命令
  • docker container rm <container-id>-码头集装箱拆除

相关问题