-
Notifications
You must be signed in to change notification settings - Fork 1
/
docker-img-usage.sh
69 lines (57 loc) · 2.53 KB
/
docker-img-usage.sh
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash
docker_find_files () {
#echo -e "\e[96m Largest Files (+200M)\e[0m"
docker_find_files_results=$(docker exec $1 sh -c '/usr/bin/find / -xdev $2 -path ./proc -prune -o -path ./sys -prune -o -type f -size +200000k -exec du -Shx {} + | sort -rh | head | sed "s/^/ /"')
}
docker_find_directories () {
#echo -e "\e[96m Largest Directories (+200M)\e[0m"
docker_find_directories_results=$(docker exec $1 sh -c 'du -hxa --threshold=200M / $2 | sort -rh | head | sed "s/^/ /"')
}
docker_find_subdirectories () {
#echo -e "\e[96m Largest Directories (+200M) (excluding subdirectories)\e[0m"
docker_find_subdirectories_results=$(docker exec $1 sh -c 'du -Shx --threshold=200M / $2 | sort -rh | head | sed "s/^/ /"')
}
docker_get_mounts () {
mounts=$(docker inspect --format='{{range .Mounts}}{{json .Destination}}{{end}}' $1)
#echo $mounts
}
docker_set_exclude_path () {
for i in `echo $mounts | tr \" '\n' | grep -v "^$"`; do path="$path -path '.$i' -prune -o "; done
}
docker_set_exclude_dir () {
for i in `echo $mounts | tr \" '\n' | grep -v "^$"`; do exclude="$exclude --exclude='$i' "; done
#echo $exclude
}
results () {
if [[ ! -z $docker_find_files_results || ! -z $docker_find_directories_results || ! -z $docker_find_subdirectories_results ]]; then
echo -e "\e[32;4m$1\e[0m"
fi
if [[ ! -z $docker_find_files_results ]]; then
echo -e "\e[96m Largest Files (+200M)\e[0m"
echo $docker_find_files_results
fi
if [[ ! -z $docker_find_directories_results ]]; then
echo -e "\e[96m Largest Directories (+200M)\e[0m"
echo $docker_find_directories_results
fi
if [[ ! -z $docker_find_subdirectories_results ]]; then
echo -e "\e[96m Largest Directories (+200M) (excluding subdirectories)\e[0m"
echo $docker_find_subdirectories_results
fi
if [[ ! -z $docker_find_files_results || ! -z $docker_find_directories_results || ! -z $docker_find_subdirectories_results ]]; then
echo ""
fi
}
# Uncomment if you need, but leaving it commented out since it adds significant delay to the script.
# docker system df -v
for container in $(docker ps --format '{{.Names}}' | sort -f)
do
#echo -e "\e[32;4m$container\e[0m"
docker_get_mounts "$container"
docker_set_exclude_path
docker_set_exclude_dir
docker_find_files $container $path
#docker_find_directories $container $exclude #This needs works. Currently not useful
docker_find_subdirectories $container $exclude
results $container $docker_find_files_results $docker_find_directories_results $docker_find_subdirectories_results
done