-
Notifications
You must be signed in to change notification settings - Fork 11
/
docker-cleanup
executable file
·46 lines (40 loc) · 1.29 KB
/
docker-cleanup
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash
help() {
echo 'usage: docker-cleanup'
echo ' docker-cleanup --help'
echo
echo ' Will remove:'
echo ' - stopped containers'
echo ' - dangling volumes'
echo ' - unused networks'
echo ' - images without repository and without tag (<none> <none>)'
echo ' - older versions of images with identical repository and tag'
echo ' - dangling images'
echo
exit 1
}
[ "$1" == "--help" ] && help
set -e # stop on error
set -u # stop on undefined variable
set -o pipefail # stop part of pipeline failing
docker system prune --volumes
previous_repository=""
previous_tag=""
previous_id=""
# images are sorted older to younger
#
docker images --format "{{.Repository}} {{.Tag}} {{.CreatedAt}} {{.ID}}" \
| sort \
| while read repository tag created_at id; do
if [ "$tag" == "<none>" ] && [ "$repository" == "<none>" ]; then
echo docker rmi $id
elif [ "$tag" == "$previous_tag" ] && [ "$repository" == "$previous_repository" ]; then
echo docker rmi $previous_id
fi
previous_repository=$repository
previous_tag=$tag
previous_id=$id
done
# in case iterating through images above left some
# dangling ones, then delete then now
docker image prune --filter=dangling=true