-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_containers.sh
125 lines (112 loc) · 2.82 KB
/
setup_containers.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
function teardown(){
docker compose -f "BuildTools/docker-compose.yml" down --volumes --remove-orphans
if [[ $? -eq 1 ]]
then
echo "Failed to perform docker compose teardown"
echo "Is Docker Desktop running?"
exit
fi
}
function standup(){
docker compose -f "BuildTools/docker-compose.yml" up -d --build --remove-orphans
if [[ $? -eq 1 ]]
then
echo "Failed to perform docker compose up"
echo "Is Docker Desktop running?"
exit
fi
cleanup_dangling_images
}
function does_image_exist(){
docker images ls --filter="reference=$1" | grep -v 'REPOSITORY' | wc -l
}
function cleanup_compose_images(){
echo "Cleanup up Compose Images"
for compose_image in $(grep 'image:' BuildTools/docker-compose.yml | awk '{ print $2 }')
do
if [[ $(does_image_exist "${compose_image}") -gt 0 ]]
then
echo "Removing ${compose_image}"
docker rmi "${compose_image}"
fi
done
}
function cleanup_dockerfile_images(){
echo "Cleaning up dockerfile images"
for image_tag in $(grep -ri '^FROM' ./**/docker* | awk '{ split($0, a, "FROM "); split(a[2], it, " AS "); print it[1]}')
do
if [[ $( does_image_exist "${image_tag}" ) -ne 0 ]]
then
docker rmi "${image_tag}"
fi
done
}
function cleanup_dangling_images(){
echo "Cleaning up dangling images"
if [[ $(docker images -f "dangling=true" -q | wc -l ) -gt 0 ]]
then
docker rmi $(docker images -f "dangling=true" -q)
fi
}
function cleanup_volumes(){
echo "Cleaning up volumes"
docker volume rm dbdata redisdata -f
docker volume prune -f
}
function cleanup_builder(){
echo "Cleaning up builder"
docker builder prune -af --verbose
docker buildx prune -af --verbose
}
function cleanup(){
cleanup_compose_images
cleanup_dockerfile_images
cleanup_dangling_images
cleanup_volumes
cleanup_builder
}
function get_logs(){
echo "Exporting Container Logs"
mkdir -p logs
docker compose -f "BuildTools/docker-compose.yml" logs > logs/container_logs.log
}
function help(){
echo "Please pass a parameter to this script"
echo "teardown, cleanup, standup or getlogs"
echo "You can also pass each of the parameters together"
echo "They must be in the order of teardown cleanup standup getlogs"
exit
}
if [ -z "${1}" ]
then
help
fi
for i in {1..4}
do
if [[ $i -eq 1 ]] && [[ -z "${!i}" ]]
then
help
fi
if [[ -z "${!i}" ]]
then
break
fi
case "${!i}" in
teardown)
teardown
;;
cleanup)
cleanup
;;
standup)
standup
;;
getlogs)
get_logs
;;
*)
help
;;
esac
done