diff --git a/.tools/remove-all-images.sh b/.tools/remove-all-images.sh index b183422..7c31a20 100755 --- a/.tools/remove-all-images.sh +++ b/.tools/remove-all-images.sh @@ -1,13 +1,18 @@ #!/bin/bash -# Purpose: Clean up Docker resources for Mutillidae application +# Purpose: Clean up all Docker resources for Mutillidae application # Usage: ./remove-all-images.sh [options] # Function to print messages with a timestamp print_message() { - echo "" echo "$(date +"%Y-%m-%d %H:%M:%S") - $1" } +# Function to handle errors +handle_error() { + print_message "Error: $1" + exit 1 +} + # Function to display help message show_help() { echo "Usage: $0 [options]" @@ -16,16 +21,10 @@ show_help() { echo " -h, --help Display this help message." echo "" echo "Description:" - echo "This script is used to clean up Docker resources for the Mutillidae application." - echo "It stops and removes all containers, removes all images, and prunes all volumes and networks." + echo "This script stops and removes all Docker containers and images for the Mutillidae application, then prunes unused volumes and networks." exit 0 } -# Function to handle errors -handle_error() { - print_message "Error: $1" -} - # Parse options while [[ "$#" -gt 0 ]]; do case $1 in @@ -38,13 +37,12 @@ done # Check if Docker is installed and running if ! command -v docker &> /dev/null; then handle_error "Docker is not installed or not in PATH. Please install Docker." - exit 1 fi -# Clean up Docker resources -print_message "Cleaning up Docker resources for Mutillidae" +# Clean up all Docker resources +print_message "Cleaning up all Docker resources for Mutillidae" -# Stop and remove all containers, with checks to ensure there are containers to stop +# Stop and remove all containers CONTAINERS=$(docker ps -a -q) if [[ -n "$CONTAINERS" ]]; then print_message "Stopping and removing all containers" @@ -53,7 +51,7 @@ else print_message "No containers to remove" fi -# Remove all images, with a check to ensure there are images to remove +# Remove all images IMAGES=$(docker images -a -q) if [[ -n "$IMAGES" ]]; then print_message "Removing all images" @@ -62,16 +60,10 @@ else print_message "No images to remove" fi -# Prune containers, images, volumes, and networks with error handling -print_message "Pruning containers, images, volumes, and networks" -docker container prune -f || true -docker image prune --all -f || true -docker volume prune -f || true -docker network prune -f || true - -# System-wide prune to ensure complete cleanup -print_message "Performing system-wide prune" -docker system prune --all --volumes -f || handle_error "Failed to prune system" +# Prune unused resources +print_message "Pruning unused volumes and networks" +docker volume prune -f || handle_error "Failed to prune volumes" +docker network prune -f || handle_error "Failed to prune networks" # Success message -print_message "Docker resources for Mutillidae cleaned up successfully" +print_message "All Docker resources cleaned up successfully" diff --git a/.tools/stop-containers.sh b/.tools/stop-containers.sh index ad2887d..cc4af99 100755 --- a/.tools/stop-containers.sh +++ b/.tools/stop-containers.sh @@ -1,10 +1,9 @@ #!/bin/bash -# Purpose: Stop Docker containers defined in docker-compose.yml -# Usage: ./stop-containers.sh -f /path/to/docker-compose.yml [options] +# Purpose: Stop and remove Docker containers defined in docker-compose.yml +# Usage: .tools/stop-containers.sh -f /path/to/docker-compose.yml [options] # Function to print messages with a timestamp print_message() { - echo "" echo "$(date +"%Y-%m-%d %H:%M:%S") - $1" } @@ -17,7 +16,7 @@ show_help() { echo " -h, --help Display this help message." echo "" echo "Description:" - echo "This script is used to stop Docker containers defined in docker-compose.yml." + echo "This script stops and removes Docker containers defined in docker-compose.yml using 'docker compose down' or 'docker-compose down'." exit 0 } @@ -52,14 +51,28 @@ if [[ -z "$compose_file" ]]; then handle_error "The -f/--file option is mandatory. Use -h for help." fi +# Check if the compose file exists +if [[ ! -f "$compose_file" ]]; then + handle_error "The specified docker-compose.yml file does not exist: $compose_file" +fi + # Check if Docker is installed and running if ! command -v docker &> /dev/null; then handle_error "Docker is not installed or not in PATH. Please install Docker." fi -# Stop Docker containers +# Determine whether to use 'docker-compose' or 'docker compose' +if command -v docker-compose &> /dev/null; then + DOCKER_COMMAND="docker-compose" +elif docker compose version &> /dev/null; then + DOCKER_COMMAND="docker compose" +else + handle_error "Neither 'docker-compose' nor 'docker compose' is available. Please install one." +fi + +# Perform 'down' operation to stop and remove containers print_message "Stopping and removing containers using $compose_file" -docker compose -f "$compose_file" down || handle_error "Failed to stop containers" +$DOCKER_COMMAND -f "$compose_file" down || handle_error "Failed to stop and remove containers" # Success message -print_message "Docker containers stopped successfully" +print_message "Docker containers stopped and removed successfully"