# 定义要保留的容器ID的数组 declare -a retain_containers=("f8a56340f88a""12d1b26b31c3""9cbc3133cff4""f0a8c04f785b""d6e2f51be0a8""9ef9e894c7c7")
# 定义要保留的镜像的数组 declare -a retain_images=("sha256:5e8d4559f59d""sha256")
echo info: delete containers echo echo
# 停止并删除除了指定容器以外的所有正在运行的容器 for container_id in $(docker ps -aq); do if [[ " ${retain_containers[@]} " =~ " ${container_id} " ]]; then echo"detected: ${container_id} is retained"
# 获取保留容器对应的镜像ID declare -a retain_image_ids for container_id in"${retain_containers[@]}"; do image_id=$(docker inspect --format='{{.Image}}'"$container_id") if [[ -n "$image_id" ]]; then retain_image_ids+=("$image_id") fi done
declare -a retain_image_ids=("${retain_image_ids[@]}""${retain_images[@]}")
# 删除images for image_id in $(docker images -aq); do is_true=1 for retain_image_id in"${retain_image_ids[@]}"; do if [ "${retain_image_id:0:19}" == "sha256:${image_id}" ]; then echo"detected: ${image_id} is retained" is_true=0 break fi done if [ $is_true -eq 1 ]; then echo"Deleted: sha256:${image_id}" docker rmi $image_id fi