删除docker没用的容器,网络,卷,为你的主机“瘦身”
2017-10-25 16:00
639 查看
Docker takes a conservative(保守的) approach(方法) to cleaning up unused objects(对象) (often referred to(被叫做) as “garbage collection”(垃圾回收)), such as images, containers, volumes, and networks: these objects are generally(通常) not removed unless you explicitly(明确) ask(要求) Docker to do so. This can cause Docker to use extra(额外) disk space. For each type of object, Docker provides a prune command. In addition(此外), you can use docker system prune to clean up multiple types of objects at once. This topic shows how to use these prune commands.
Prune images
The docker image prune command allows you to clean up unused images. By default, docker image prune only cleans up dangling images. A dangling image is one that is not tagged and is not referenced(引用) by any container. To remove dangling images:
To remove all images which are not used by existing containers, use the -a flag:
$ docker image prune -a
WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y
By default, you are prompted(提示) to continue. To bypass(绕过) the prompt, use the -f or --force flag. 强制删除
You can limit which images are pruned(删除) using filtering(过滤) expressions(表达式) with the --filter flag. For example, to only consider images created more than 24 hours ago:
$ docker image prune -a --filter "until=24h"
Other filtering expressions are available. See the docker image prune reference for more examples.
Prune containers
When you stop a container, it is not automatically removed unless you started it with the --rm flag. To see all containers on the Docker host, including stopped containers, use docker ps -a. You may be surprised how many containers exist, especially on a development system! A stopped container’s writable layers still take up(占据) disk space. To clean this up, you can use the docker container prune command.
$ docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
By default, you are prompted to continue. To bypass the prompt, use the -f or --force flag.
By default, all stopped containers are removed. You can limit the scope using the --filter flag. For instance, the following command only removes stopped containers older than 24 hours:
$ docker container prune --filter "until=24h"
Other filtering expressions are available. See the docker container prune reference for more examples.
Prune volumes
Volumes can be used by one or more containers, and take up space on the Docker host. Volumes are never removed automatically, because to do so could(能) destroy(破坏) data.
$ docker volume prune
WARNING! This will remove all volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
By default, all unused volumes are removed. You can limit the scope using the --filter flag. For instance, the following command only removes volumes which are not labelled with the keep label(下面的命令只删除未标记为保留标签的卷):
$ docker volume prune --filter "label!=keep"
Other filtering expressions are available. See the docker volume prune reference for more examples.
Prune networks
Docker networks don’t take up much disk space, but they do create iptables rules, bridge network devices, and routing table entries. To clean these things up, you can use docker network prune to clean up networks which aren’t used by any containers().
$ docker network prune
WArNING! This will remove all networks not used by at least one container.
Are you sure you want to continue? [y/N] y
By default, you are prompted to continue. To bypass the prompt, use the -f or --force flag.
By default, all unused networks are removed. You can limit the scope using the --filter flag. For instance, the following command only removes networks older than 24 hours:
Prune everything
The docker system prune command is a shortcut that prunes images, containers, and networks. In Docker 17.06.0 and earlier, volumes are also pruned. In Docker 17.06.1 and higher, you must specify the --volumes flag for docker system prune to prune volumes.
$ docker system 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] y
If you are on Docker 17.06.1 or higher and want to also prune volumes, add the --volumes flag:
$ 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
Prune images
The docker image prune command allows you to clean up unused images. By default, docker image prune only cleans up dangling images. A dangling image is one that is not tagged and is not referenced(引用) by any container. To remove dangling images:
To remove all images which are not used by existing containers, use the -a flag:
$ docker image prune -a
WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y
By default, you are prompted(提示) to continue. To bypass(绕过) the prompt, use the -f or --force flag. 强制删除
You can limit which images are pruned(删除) using filtering(过滤) expressions(表达式) with the --filter flag. For example, to only consider images created more than 24 hours ago:
$ docker image prune -a --filter "until=24h"
Other filtering expressions are available. See the docker image prune reference for more examples.
Prune containers
When you stop a container, it is not automatically removed unless you started it with the --rm flag. To see all containers on the Docker host, including stopped containers, use docker ps -a. You may be surprised how many containers exist, especially on a development system! A stopped container’s writable layers still take up(占据) disk space. To clean this up, you can use the docker container prune command.
$ docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
By default, you are prompted to continue. To bypass the prompt, use the -f or --force flag.
By default, all stopped containers are removed. You can limit the scope using the --filter flag. For instance, the following command only removes stopped containers older than 24 hours:
$ docker container prune --filter "until=24h"
Other filtering expressions are available. See the docker container prune reference for more examples.
Prune volumes
Volumes can be used by one or more containers, and take up space on the Docker host. Volumes are never removed automatically, because to do so could(能) destroy(破坏) data.
$ docker volume prune
WARNING! This will remove all volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
By default, all unused volumes are removed. You can limit the scope using the --filter flag. For instance, the following command only removes volumes which are not labelled with the keep label(下面的命令只删除未标记为保留标签的卷):
$ docker volume prune --filter "label!=keep"
Other filtering expressions are available. See the docker volume prune reference for more examples.
Prune networks
Docker networks don’t take up much disk space, but they do create iptables rules, bridge network devices, and routing table entries. To clean these things up, you can use docker network prune to clean up networks which aren’t used by any containers().
$ docker network prune
WArNING! This will remove all networks not used by at least one container.
Are you sure you want to continue? [y/N] y
By default, you are prompted to continue. To bypass the prompt, use the -f or --force flag.
By default, all unused networks are removed. You can limit the scope using the --filter flag. For instance, the following command only removes networks older than 24 hours:
Prune everything
The docker system prune command is a shortcut that prunes images, containers, and networks. In Docker 17.06.0 and earlier, volumes are also pruned. In Docker 17.06.1 and higher, you must specify the --volumes flag for docker system prune to prune volumes.
$ docker system 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] y
If you are on Docker 17.06.1 or higher and want to also prune volumes, add the --volumes flag:
$ 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应用-5(使用overlay 网络进行容器间跨物理主机通信)
- 跨主机网络概述 - 每天5分钟玩转 Docker 容器技术(48)
- 理解Docker跨多主机容器网络
- docker1.9网络新特性,overlay网络实现主机间容器互联
- Docker网络基础---Docker跨主机容器访问通信
- 多主机网络下 Docker Swarm 模式的容器管理
- 主机网络切换后,docker toolbox里的容器网络不通的解决方法
- Docker网络管理及容器跨主机通信(四)
- Docker实现跨主机容器实例网络通信(2)——利用OpenVSwitch构建多主机Docker网络
- Docker 网络之pipework 工具(3)单主机Docker容器VLAN划分
- docker 容器无法删除--网络连接无法释放
- Docker 官方宣布收购 SocketPlane 多主机容器网络解决方案。
- 多主机网络下 Docker Swarm 模式的容器管理
- 多主机网络下 Docker Swarm 模式的容器管理
- docker 实战---多台物理主机的联网,容器桥接到物理网络拓扑图(四)
- docker 实战---多台物理主机的联网,容器桥接到物理网络拓扑图(四)
- 理解Docker跨多主机容器网络
- Docker实现跨主机容器实例网络通信(1)——利用LinuxBridge构建多主机Docker网络
- 立足Docker运行MySQL:多主机网络下Docker Swarm模式的容器管理
- Docker容器访问宿主机网络的方法