# 保存当前运行中的镜像到一个临时文件 docker ps --format "{{.Image}}" | sort | uniq > /tmp/used_images.txt # 遍历本地所有镜像 for image_id in $(docker images -q | uniq); do # 拿到镜像的名字(可能有多个名字) image_names=$(docker image inspect --format='{{range .RepoTags}}{{.}} {{end}}' $image_id) # 逐个名字检查 in_use=false for name in $image_names; do if grep -q "$name" /tmp/used_images.txt; then in_use=true break fi done # 如果镜像没在用,删除它 if [ "$in_use" = false ]; then echo "Deleting unused image: $image_names" docker rmi $image_id fi done # 最后清理临时文件 rm -f /tmp/used_images.txt
浙公网安备 33010602011771号