Docker允许您将应用程序构建为可移植,自给自足的容器,可以在任何地方运行,从而快速构建,测试和部署应用程序。
Docker默认不会删除未使用的对象,例如容器,图像,卷和网络。在使用Docker时,您可以轻松地积累大量未使用的对象,这些对象会占用大量磁盘空间,并使Docker命令产生的输出混乱。
本教程将说明如何删除未使用Docker容器,镜像,卷和网络,以帮助Docker用户保持系统井井有条,并通过删除未使用的Docker容器,镜像,卷和网络来释放磁盘空间。
删除所有未使用的对象
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 --volumesWARNING! 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容器时,不会自动删除它们,除非使用--rm标志启动容器。
docker --rm -itd run hello-world删除一个或多个容器
要删除一个或多个Docker容器,请使用docker container rm命令,后跟要删除的容器的ID。
可以通过将-a选项传递给docker container ls命令来获得所有活动容器和非活动容器的列表:
docker container ls -a输出应如下所示:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMEScc3f2ff51cab centos "/bin/bash" 2 months ago Created competent_nightingale
cd20b396a061 solita/ubuntu-systemd "/bin/bash -c 'exec …" 2 months ago Exited (137) 2 months ago systemd
fb62432cf3c1 ubuntu "/bin/bash" 3 months ago Exited (130) 3 months ago jolly_mirzakhani
知道要删除的容器的CONTAINER ID后,请将其传递给docker container rm命令。例如,要删除上面输出中列出的前两个容器,请运行:
docker container rm cc3f2ff51cab cd20b396a061如果收到与以下类似的错误,则表明该容器正在运行。您需要先停止容器,然后再将其移除。
Error response from daemon: You cannot remove a running container fc983ebf4771d42a8bd0029df061cb74dc12cb174530b2036987575b83442b47. Stop the container before attempting removal or force remove.删除所有已停止的容器
在执行删除命令之前,您可以使用以下命令获取将要删除已停止的容器列表:
docker container ls -a --filter status=exited --filter status=created 要删除所有已停止的容器,请使用docker container prune命令:
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]
0系统会提示您继续,如果不需要提示,使用-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]
1使用过滤器删除容器
docker container prune命令允许您使用过滤标记--filter根据条件删除容器。
在撰写本文时,当前支持的过滤器为until和label。您可以通过使用多个--filter标志来使用多个过滤器。
例如,要删除所有在12个小时前创建的图像,请运行:
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]
2停止并停止所有容器
您可以使用docker container ls -aq命令获取系统上所有Docker容器的列表。
要停止所有正在运行的容器,请使用docker container stop命令,后跟所有容器ID的列表。
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]
3一旦所有容器都停止,您可以使用docker container rm命令,然后再加上容器ID列表来删除它们。
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]
4移除Docker镜像
要删除一个或多个Docker镜像,请使用docker images ls命令找到要删除的映像的ID。
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]
5输出应如下所示:
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]
6找到要删除的图像后,请将其IMAGE ID传递到docker image rm命令。例如,要删除上面输出中列出的前两个图像,请运行:
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]
7如果收到与以下所示类似的错误,则表示现有容器正在使用该图像。要删除图像,您必须先删除容器。
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]
8删除镜像
Docker提供了docker image prune命令,可用于删除未使用的镜像。
删除未标记且未被任何容器使用的图像。要删除未使用的镜像,请输入:
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]
9系统会提示您继续,请使用-f或--force标志绕过提示。
docker system prune --volumes0当删除未使用的镜像时,如果您标记的未使用的镜像没有被标记,它们也将被删除。
删除所有未使用的镜像
要删除所有现有容器未引用的镜像,而不仅仅是已挂起的容器,请使用带有-a标志的prune命令:
docker system prune --volumes1docker system prune --volumes2使用过滤器删除镜像
使用docker image prune命令,您还可以使用过滤标志--filter根据特定条件删除镜像。在撰写本文时,当前支持的过滤器为until和label。您可以使用多个--filter标志来使用多个过滤器。
例如,要删除12个小时前创建的镜像,您可以运行:
docker system prune --volumes3删除Docker卷
删除一个或多个卷
要删除一个或多个Docker卷,请使用docker volume ls命令查找要删除的卷的ID。
docker system prune --volumes4输出应如下所示:
docker system prune --volumes5找到要删除的卷的VOLUME NAME后,将其传递给docker volume rm命令。例如,要删除上面输出中列出的第一个卷,请运行:
docker system prune --volumes6如果收到与以下所示类似的错误,则表示现有容器正在使用该卷。要删除该卷,您必须先删除该容器。
docker system prune --volumes7删除所有未使用的卷
要删除所有未使用的卷,请使用docker image prune命令:
docker system prune --volumes8系统会提示您继续,请使用-f或--force标志绕过提示。
docker system prune --volumes9删除Docker网络
删除一个或多个网络
要删除一个或多个Docker网络,请使用docker network ls命令查找您要删除的网络的ID。
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
0输出应如下所示:
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
1找到要删除的网络后,请将其NETWORK ID传递给docker network rm命令。例如,要删除名称为my-bridge-network的网络,请运行:
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
2如果收到与以下所示类似的错误,则表示现有容器正在使用该网络。要删除网络,您必须先删除容器。
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
3删除所有未使用的网络
使用docker network prune命令删除所有未使用的网络。
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
4系统会提示您继续,请使用-f或--force标志绕过提示。
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
5使用过滤器删除网络
使用docker network prune命令,您可以使用过滤标记--filter根据条件删除网络。
在撰写本文时,目前个受支持的过滤器为until和label。您可以通过使用多个--filter标志来使用多个过滤器。
例如,要删除在12个小时前创建的所有网络,请运行:
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
6结论
在本指南中,我们向您展示了一些用于删除Docker容器,镜像,卷和网络的常用命令。您还应该查看 Docker官方文档。如有任何疑问,请在下面发表评论。





还没有评论,来说两句吧...