1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
#清理docker
clean_docker_images.sh
#!/bin/sh
# Auth: zisefeizhu Time: 2022-06-17-09:42:11 Place:宝山-上海
# 功能:清理大于最近5个版本的镜像
# 先清除悬空镜像 悬空一定不可用
# 悬空镜像清除成功与否不影响下一步执行
docker rmi $(docker images -f "dangling=true" -q)
# 删除过期镜像,保留最新5个版本
Reserved_Version=5
CleanDate=`date '+%Y-%m-%d_%H:%M:%S'`
# 主逻辑
for imageName in `docker images | awk '{print $1}' |sort -u`
do
version=`docker images | grep ${imageName} | awk '{print $2}'`
versionTotal=`docker images | grep ${imageName} | awk '{print $2}'|wc -l`
if [[ ${versionTotal} -gt ${Reserved_Version} ]];then
tag=(${version// /})
#保留同样类型的5个镜像
for(( j=${Reserved_Version};j<${#tag[@]};j++)) do
docker rmi ${imageName}:${tag[j]}
echo "docker rmi ${imageName}:${tag[j]} 执行时间:${CleanDate}"
done
fi
done
|