-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanNetwork.sh
executable file
·52 lines (43 loc) · 1.13 KB
/
cleanNetwork.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
#!/bin/bash
#
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
#
# usage: ./cleanNetwork.sh [docker image key word]
# example: ./cleanNetwork.sh sample
#
function printHelp {
echo ""
echo "usage: ./cleanNetwork.sh [docker image key word]"
echo "example: ./cleanNetwork.sh sample"
echo ""
exit
}
if [ $# -ne 1 ]; then
echo "invalid number of arguments: $#"
printHelp
fi
keyWord=$1
#bring down network
echo "..... clean network ..... docker images key word: $keyWord"
# docker-compose down
#remove dead docker containers
echo "..... remove containers ....."
#docker rm -f $(docker ps -aq)
containerIDs=$(docker ps -aq)
if [ -z "$containerIDs" -o "$containerIDs" = " " ]; then
echo "..... No containers to be deleted ....."
else
docker rm -f $containerIDs
fi
#remove dead docker images
echo "..... remove docker images ....."
dockerImageIDs=$(docker images | grep "$keyWord" | awk '{print $3}')
if [ -z "$dockerImageIDs" -o "$dockerImageIDs" = " " ]; then
echo "..... No images to be deleted ....."
else
docker rmi -f $dockerImageIDs
fi
echo "clean Network completed."