-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdkr.sh
61 lines (55 loc) · 1.39 KB
/
dkr.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
#!/bin/bash
# Ensure Docker is installed
if ! command -v docker &> /dev/null
then
echo "Docker could not be found. Please install it first."
exit 1
fi
# Display help
display_help() {
echo "Usage: dkr [action] [--force]"
echo "Actions:"
echo " stop Stops all Docker containers"
echo " remove Removes all Docker containers"
echo "Options:"
echo " --force Forces the stop or remove process"
}
# Check if help flag is set or no arguments provided
if [ "$1" == "--help" ] || [ "$1" == "-h" ] || [ -z "$1" ]
then
display_help
exit 0
fi
ACTION=$1
FORCE=""
# Check if force flag is set
if [ "$2" == "--force" ]
then
FORCE="-f"
fi
# Perform the requested action
case $ACTION in
"stop")
CONTAINERS=$(docker ps -aq)
if [ -z "$CONTAINERS" ]; then
echo "No Docker containers are currently running."
else
echo "Stopping all Docker containers..."
docker stop $FORCE $CONTAINERS
fi
;;
"remove")
CONTAINERS=$(docker ps -aq)
if [ -z "$CONTAINERS" ]; then
echo "No Docker containers found."
else
echo "Stopping and removing all Docker containers..."
docker stop $CONTAINERS
docker rm $FORCE $CONTAINERS
fi
;;
*)
echo "Invalid action. Please provide: stop, remove"
display_help
;;
esac