2. 移除所有停止的容器

01 在删除任何容器之前,我们可以使用以下命令获取将要删除的所有非运行(停止)容器的列表:

$docker container ls -a --filter status=exited --filter status=created

02 现在,要删除所有停止的容器,请使用 docker container prune命令:

$docker container prune

系统会提示我们继续,使用 -f或者 --force标志绕过提示。

WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y

3. 停止并移除所有容器

01 首先,我们可以使用以下命令获取系统上所有 Docker 容器的列表:

$docker container ls -aq

02 现在让我们使用以下命令停止所有正在运行的容器:

$docker container stop $(docker container ls -aq)

02 停止所有容器后,我们可以使用 rm命令后跟容器 ID 列表来删除它们。

$docker container rm $(docker container ls -aq)

1. 删除所有未使用的对象

docker system prune命令将删除所有停止的容器、所有悬空图像和所有未使用的网络:

$docker system prune

系统会提示我们继续,使用 -f或者 --force标志绕过提示。

WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all dangling images
        - all build cache
Are you sure you want to continue? [y/N]

如果我们还想删除所有未使用的卷,请传递 --volumes标志:

$docker system prune --volumes
WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all volumes not used by at least one container
        - all dangling images
        - all build cache
Are you sure you want to continue? [y/N] y
如何停止和删除所有 Docker 容器?

Docker 是一个开源容器化平台,可让我们快速构建、测试和部署应用程序作为可在任何地方运行的便携式容器。
在这个快速教程中,我们将展示如何删除 Docker 中所有停止的容器、所有悬空的图像和所有未使用的网络。
此外,如何通过运行几个命令来停止和删除所有容器。

日期:2020-06-02 22:18:45 来源:oir作者:oir