#!/bin/bash # Arrays containing container names and their associated volumes CONTAINER_NAMES=("compact-box" ) # Replace with your container names VOLUMES=("dutest_vol1" "dutest_vol2" "dutest_vol3") # Replace with your volume names # Log file for errors LOG_FILE="remove_containers_and_volumes_strict.log" > "$LOG_FILE" # Clear the log file # Function to log errors and exit log_error_and_exit() { echo "$(date '+%Y-%m-%d %H:%M:%S') - ERROR: $1" >> "$LOG_FILE" echo "Script failed. Check $LOG_FILE for details." >&2 exit 1 } # Iterate through the containers and volumes for i in "${!CONTAINER_NAMES[@]}"; do CONTAINER_NAME="${CONTAINER_NAMES[$i]}" VOLUME="${VOLUMES[$i]}" echo "Processing container: $CONTAINER_NAME" # Stop the container if ! docker stop "$CONTAINER_NAME" > /dev/null 2>&1; then log_error_and_exit "Failed to stop container: $CONTAINER_NAME" fi echo "Stopped container: $CONTAINER_NAME" # Remove the container if ! docker rm "$CONTAINER_NAME" > /dev/null 2>&1; then log_error_and_exit "Failed to remove container: $CONTAINER_NAME" fi echo "Removed container: $CONTAINER_NAME" # Remove the associated volume if ! docker volume rm "$VOLUME" > /dev/null 2>&1; then log_error_and_exit "Failed to remove volume: $VOLUME" fi echo "Removed volume: $VOLUME" done echo "Script execution completed successfully."