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 --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容器时,不会自动删除它们,除非使用--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 --volumes
0
当删除未使用的镜像时,如果您标记的未使用的镜像没有被标记,它们也将被删除。
删除所有未使用的镜像
要删除所有现有容器未引用的镜像,而不仅仅是已挂起的容器,请使用带有-a
标志的prune
命令:
docker system prune --volumes
1
docker system prune --volumes
2
使用过滤器删除镜像
使用docker image prune
命令,您还可以使用过滤标志--filter
根据特定条件删除镜像。在撰写本文时,当前支持的过滤器为until
和label
。您可以使用多个--filter
标志来使用多个过滤器。
例如,要删除12个小时前创建的镜像,您可以运行:
docker system prune --volumes
3
删除Docker卷
删除一个或多个卷
要删除一个或多个Docker卷,请使用docker volume ls
命令查找要删除的卷的ID。
docker system prune --volumes
4
输出应如下所示:
docker system prune --volumes
5
找到要删除的卷的VOLUME NAME
后,将其传递给docker volume rm
命令。例如,要删除上面输出中列出的第一个卷,请运行:
docker system prune --volumes
6
如果收到与以下所示类似的错误,则表示现有容器正在使用该卷。要删除该卷,您必须先删除该容器。
docker system prune --volumes
7
删除所有未使用的卷
要删除所有未使用的卷,请使用docker image prune
命令:
docker system prune --volumes
8
系统会提示您继续,请使用-f
或--force
标志绕过提示。
docker system prune --volumes
9
删除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官方文档。如有任何疑问,请在下面发表评论。
还没有评论,来说两句吧...