我的本地ubuntu中有大约50K的图像:
$ docker info
Containers: 3
Images: 49708
Storage Driver: aufs
Root Dir: /var/lib/docker/aufs
Backing Filesystem: extfs
Dirs: 49714
docker rmi -f $(docker images | grep "something" | awk "{print \$3}")
占用100%CPU并且速度太慢。有没有快速删除大量图像的方法?
答案 0 :(得分:6)
所以回答我自己的问题,docker contrib目录中有一个脚本: https://github.com/docker/docker/blob/620339f166984540f15aadef2348646eee9a5b42/contrib/nuke-graph-directory.sh
使用sudo运行它删除了我的所有图像,只需重启docker守护进程就可以了。你好了。
答案 1 :(得分:2)
似乎您需要清除/var/lib/docker/aufs
。我没有这样做,这可能是不安全的。
但首先尝试使用ndk编写的这些别名:
# Kill all running containers.
alias dockerkillall='docker kill $(docker ps -q)'
# Delete all stopped containers.
alias dockercleanc='printf "\n>>> Deleting stopped containers\n\n" && docker rm $(docker ps -a -q)'
# Delete all untagged images.
alias dockercleani='printf "\n>>> Deleting untagged images\n\n" && docker rmi $(docker images -q -f dangling=true)'
# Delete all stopped containers and untagged images.
alias dockerclean='dockercleanc || true && dockercleani'
# Delete all
alias dockercleanup='dockerkillall || true && dockercleanc || true && dockercleani'
UPD:自该答案发表以来,已经引入了几种新的手动清理方法。我将允许自己引用GitHub用户adamhadani的代码(需要docker-py
):
#!/usr/bin/env python
"""
Check all existing Docker containers for their mapped paths, and then purge any
zombie directories in docker's volumes directory which don't correspond to an
existing container.
"""
import logging
import os
import sys
from shutil import rmtree
import docker
DOCKER_VOLUMES_DIR = "/var/lib/docker/vfs/dir"
def get_immediate_subdirectories(a_dir):
return [os.path.join(a_dir, name) for name in os.listdir(a_dir)
if os.path.isdir(os.path.join(a_dir, name))]
def main():
logging.basicConfig(level=logging.INFO)
client = docker.Client()
valid_dirs = []
for container in client.containers(all=True):
volumes = client.inspect_container(container['Id'])['Volumes']
if not volumes:
continue
for _, real_path in volumes.iteritems():
if real_path.startswith(DOCKER_VOLUMES_DIR):
valid_dirs.append(real_path)
all_dirs = get_immediate_subdirectories(DOCKER_VOLUMES_DIR)
invalid_dirs = set(all_dirs).difference(valid_dirs)
logging.info("Purging %s dangling Docker volumes out of %s total volumes found.",
len(invalid_dirs), len(all_dirs))
for invalid_dir in invalid_dirs:
logging.info("Purging directory: %s", invalid_dir)
rmtree(invalid_dir)
logging.info("All done.")
if __name__ == "__main__":
sys.exit(main())