sh 删除已停止的Docker容器,未标记且不是给定存储库的最新Docker镜像

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 删除已停止的Docker容器,未标记且不是给定存储库的最新Docker镜像相关的知识,希望对你有一定的参考价值。

#!/bin/bash -u
#
# Clean Docker containers and images.
#
# This script assumes that the docker images are tagged with a version (like a git sha1) and 'latest'.
#
# WARNING: only 'latest' Docker images for the given repo are kept after a cleanup!
#
# Example of how to tag a Docker image with a version and 'latest':
#   $ docker build --rm -t $REPO/$NAME:$VERSION .
#   $ docker tag -f $REPO/$NAME:$VERSION $(ORG)/$NAME:latest
#

# The repo to use to filter images to delete
REPO=$1

# Utils functions
countContainers()       { docker ps -aq | wc -l; }
listStoppedContainers() { docker ps -a  | grep Exit | awk '{print $1}'; }
countmages()            { docker images | egrep -c "($REPO|none)"; }
listNotLatestImages()   { docker images | grep $REPO | grep -v latest | awk '{printf "%s:%s\n", $1, $2}'; }
listUntaggedImages()    { docker images -q -f dangling=true; }
log() { echo "[$(date +"%Y-%m-%dT%H:%M:%SZ")][docker-cleanup] $1"; }

totalContainers=$(countContainers)
totalImages=$(countmages)
log "Total containers: $totalContainers"
log "Total images: $totalImages"

#  Delete all stopped containers
containerIds=$(listStoppedContainers)
if [[ "$containerIds" != "" ]]; then
    log "Remove stopped containers..."
    docker rm $containerIds
fi

# Delete not latest images
oldImages=$(listNotLatestImages)
if [[ "$oldImages" != "" ]]; then
    log "Remove not latest images..."
    docker rmi -f $oldImages
fi

# Delete all untagged images
untaggedImages=$(listUntaggedImages)
if [[ "$untaggedImages" != "" ]]; then
    log "Remove untagged images..."
    docker rmi $untaggedImages
fi

newTotalContainers=$(countContainers)
newTotalImages=$(countmages)

log "---"
log "Containers deleted: $(expr $totalContainers - $newTotalContainers)"
log "Images deleted: $(expr $totalImages - $newTotalImages)"
log "---"
log "Total containers: $newTotalContainers"
log "Total images: $newTotalImages"

以上是关于sh 删除已停止的Docker容器,未标记且不是给定存储库的最新Docker镜像的主要内容,如果未能解决你的问题,请参考以下文章

Docker删除镜像

sh 停止/删除所有Docker容器

sh 停止并删除所有docker容器

sh Docker删除未标记的图像

Docker笔记:收集Docker常用的一些命令

sh 删除已停止的容器