-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.sh
154 lines (119 loc) · 3.78 KB
/
script.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env bash
# Set shell options:
# -e, exit immediately if a command exits with a non-zero status
# -o pipefail, means that if any element of the pipeline fails, then the pipeline as a whole will fail.
# -u, treat unset variables as an error when substituting.
set -e
set -u
set -o pipefail
DONT_RESTART_DOCKER_ENGINE=0
DONT_ASK_CONFIRMATION=0
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
--no-restart)
DONT_RESTART_DOCKER_ENGINE=1
shift
;;
-y)
DONT_ASK_CONFIRMATION=1
shift
;;
*)
echo "Unknown parameter passed: $1";
exit 1;
;;
esac
done
# Asks user for confirmation interactively
ask_user_for_confirmation () {
cat << EOF
==============================================
This script reclaims disk space by removing stale and unused Docker data:
> removes stopped containers
> removes orphan (dangling) images layers
> removes unused volumes
> removes Docker build cache
> shrinks the "Docker.raw" file on MacOS
> restarts the Docker engine
> prints Docker disk usage
==============================================
EOF
if [ $DONT_ASK_CONFIRMATION -eq 1 ]; then
return
fi
read -p "Would you like to proceed (y/n)? " confirmation
# Stop if answer is anything but "Y" or "y"
if [ "$confirmation" == "${confirmation#[Yy]}" ]; then
exit 1;
fi
}
# On MacOS, restarting Docker Desktop for Mac might take a long time
poll_for_docker_readiness() {
# TODO: add new line at the end of polling
printf 'Waiting for docker engine to start:\n'
local i=0
while ! docker system info > /dev/null 2>&1;
do
printf '.%.0s' {1..$i}
i=$((i + 1))
sleep 1
tput el
done
printf '\n'
}
# Checks if a particular program is installed
is_program_installed () {
command -v "$1" &>/dev/null
}
# Restarts the Docker engine
restart_docker_engine () {
if [ $DONT_RESTART_DOCKER_ENGINE -eq 1 ]; then
return
fi
echo "👉 Restarting Docker engine"
# On MacOS, restart through "launchd"
if [ "$(uname)" == "Darwin" ] && is_program_installed "launchctl"; then
local docker_service=$(launchctl list | grep "com.docker.docker" | awk '$0 != "-" { print $3 }')
if [ -n "$docker_service" ]; then
launchctl stop "$docker_service" || true;
fi
launchctl start com.docker.helper
sleep 1
poll_for_docker_readiness
# On Linux, restart through "systemd"
elif [ "$(uname)" == "Linux" ] && is_program_installed "systemctl"; then
sudo systemctl stop docker.service || true
sudo systemctl start docker.service
# Other platforms are not supported
else
printf "Platform type $(uname) is not supported\n" >&2
fi
}
echo "👉 Docker disk usage"
docker system df
ask_user_for_confirmation
echo "👉 Remove all stopped containers"
docker ps --filter "status=exited" -q | xargs -r docker rm --force
echo "👉 Remove all orphan image layers"
docker images -f "dangling=true" -q | xargs -r docker rmi -f
echo "👉 Remove all unused volumes"
docker volume ls -qf dangling=true | xargs -r docker volume rm
echo "👉 Remove Docker builder cache"
DOCKER_BUILDKIT=1 docker builder prune --all --force
echo "👉 Remove docker builder cache"
echo "👉 Remove networks not used by at least one container"
docker network prune --force
# echo "👉 Remove unused volumes"
# -a, --all, Remove all unused build cache, not just dangling ones
# docker system prune --force --volumes
# Run "docker/docker-reclaim-space" only on Intel chips
# because image is not build for ARM achitecture (Apple M1 chips)
if [ "$(uname)" == "Darwin" ] && [ "$(uname -m)" == "x86_64" ]; then
echo "👉 Shrink the Docker.raw file"
docker run --privileged --pid=host docker/desktop-reclaim-space
fi
echo "👉 Docker disk usage (after cleanup)"
docker system df
restart_docker_engine
echo "🤘 Done"