如何批量安全删除镜像?

公司业务在生产环境100多台服务器上用了docker,可是最近发现,每个服务器上的各种镜像好多好乱,有没有什么办法可以批量安全地删除?显示全部

公司业务在生产环境100多台服务器上用了docker,可是最近发现,每个服务器上的各种镜像好多好乱,有没有什么办法可以批量安全地删除?

收起
参与5

查看其它 1 个回答胶东牛国磊的回答

胶东牛国磊胶东牛国磊系统架构师百度游戏
!/bin/shCleanup docker files: untagged containers and images.#
Use docker-cleanup -n for a dry run to see what would be deleted.untagged_containers() {
# Print containers using untagged images: $1 is used with awk\'s print: 0=line, 1=column 1.
# NOTE: \"[0-9a-f]{12}\" does not work with GNU Awk 3.1.7 (RHEL6).
#       Ref: https://github.com/blueyed/dotfiles/commit/a14f0b4b#commitcomment-6736470
docker ps -a | tail -n +2 | awk \'$2 ~ \"^[0-9a-f]+$\" {print $\'$1\'}\'
}

untagged_images() {
# Print untagged images: $1 is used with awk\'s print: 0=line, 3=column 3.
# NOTE: intermediate images (via -a) seem to only cause
# \"Error: Conflict, foobarid wasn\'t deleted\" messages.
# Might be useful sometimes when Docker messed things up?!
# docker images -a | awk \'$1 == \"\" {print $\'$1\'}\'
docker images | tail -n +2 | awk \'$1 == \"\" {print $\'$1\'}\'
}
Dry-run.if [ \"$1\" = \"-n\" ]; then
echo \"=== Containers with uncommitted images: ===\"
untagged_containers 0
echo

echo \"=== Uncommitted images: ===\"
untagged_images 0

exit
fi
if [ -n \"$1\" ]; then
echo \"Cleanup docker files: remove untagged containers and images.\"
echo \"Usage: ${0##*/} [-n]\"
echo \"   -n: dry run: display what would get removed.\"
exit 1
fi
Remove containers with untagged images.echo \"Removing containers:\" >&2
untagged_containers 1 | xargs --no-run-if-empty docker rm --volumes=true
Remove untagged imagesecho \"Removing images:\" >&2
untagged_images 3 | xargs --no-run-if-empty docker rmi

这是是我用了好久的代码了,可以把你说的乱七八糟没用的image都删掉

文化教育其它 · 2015-10-27
浏览2179

回答者

胶东牛国磊
系统架构师百度游戏
擅长领域: 云计算容器Docker

胶东牛国磊 最近回答过的问题

回答状态

  • 发布时间:2015-10-27
  • 关注会员:3 人
  • 回答浏览:2179
  • X社区推广