-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_docker_killer
executable file
·49 lines (34 loc) · 1.07 KB
/
_docker_killer
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
#!/bin/bash
if [[ $1 == '-c' ]]
then
containers_count=$2
containers_str=$(docker ps -a -n $containers_count | egrep -o --regexp="^\S+" | grep -v CONTAINER)
images_str=$(docker ps -a -n $containers_count | grep -Po '(?<=^\w{12}\s{3}).+?(?=\s+)')
# другой вариант найти первое слово: '^(?:\S+)'
set -f
containers=(${containers_str/\n})
for i in "${!containers[@]}"
do
echo "\nУдаляю контейнер ${containers[i]}"
docker stop "${containers[i]}"
docker rm "${containers[i]}"
done
set -f
images=($images_str)
for i in "${!images[@]}"
do
echo "\nУдаляю image ${images[i]}"
docker rmi -f "${images[i]}"
done
elif [[ $1 == '-i' ]]
then
images_count=$2
images_str=$(docker images | grep -v REPOSITORY | head -n $images_count | grep -Po '(?<=\s{21})\w+(?=\s{3}\S)')
set -f
images=($images_str)
for i in "${!images[@]}"
do
echo "\nУдаляю image ${images[i]}"
docker rmi -f "${images[i]}"
done
fi