File size: 2,030 Bytes
700dd75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash

# Script to kill all running Docker containers
# Usage: ./kill_all_containers.sh [--force]

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Function to print colored output
print_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# Check if Docker is running
if ! sudo docker info >/dev/null 2>&1; then
    print_error "Docker is not running or not accessible. Please start Docker first."
    exit 1
fi

# Get list of running containers
RUNNING_CONTAINERS=$(sudo docker ps -q)

if [ -z "$RUNNING_CONTAINERS" ]; then
    print_info "No running containers found."
    exit 0
fi

# Count running containers
CONTAINER_COUNT=$(echo "$RUNNING_CONTAINERS" | wc -l | tr -d ' ')

print_info "Found $CONTAINER_COUNT running container(s):"
sudo docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Names}}\t{{.Status}}"

# Check for --force flag
FORCE_KILL=false
if [ "$1" = "--force" ]; then
    FORCE_KILL=true
    print_warning "Force mode enabled. Containers will be killed without confirmation."
fi

# Ask for confirmation unless --force is used
if [ "$FORCE_KILL" = false ]; then
    echo
    read -p "Are you sure you want to kill all running containers? (y/N): " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        print_info "Operation cancelled."
        exit 0
    fi
fi

# Kill all running containers
print_info "Killing all running containers..."
if sudo docker kill $RUNNING_CONTAINERS; then
    print_info "Successfully killed all running containers."
else
    print_error "Failed to kill some containers. You may need to run with sudo or check Docker permissions."
    exit 1
fi

# Optional: Remove stopped containers (commented out by default)
# Uncomment the following lines if you also want to remove the stopped containers
# print_info "Removing stopped containers..."
# sudo docker container prune -f

print_info "Done!"