rm_vol.sh 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/bin/bash
  2. # Arrays containing container names and their associated volumes
  3. CONTAINER_NAMES=("compact-box" ) # Replace with your container names
  4. VOLUMES=("dutest_vol1" "dutest_vol2" "dutest_vol3") # Replace with your volume names
  5. # Log file for errors
  6. LOG_FILE="remove_containers_and_volumes_strict.log"
  7. > "$LOG_FILE" # Clear the log file
  8. # Function to log errors and exit
  9. log_error_and_exit() {
  10. echo "$(date '+%Y-%m-%d %H:%M:%S') - ERROR: $1" >> "$LOG_FILE"
  11. echo "Script failed. Check $LOG_FILE for details." >&2
  12. exit 1
  13. }
  14. # Iterate through the containers and volumes
  15. for i in "${!CONTAINER_NAMES[@]}"; do
  16. CONTAINER_NAME="${CONTAINER_NAMES[$i]}"
  17. VOLUME="${VOLUMES[$i]}"
  18. echo "Processing container: $CONTAINER_NAME"
  19. # Stop the container
  20. if ! docker stop "$CONTAINER_NAME" > /dev/null 2>&1; then
  21. log_error_and_exit "Failed to stop container: $CONTAINER_NAME"
  22. fi
  23. echo "Stopped container: $CONTAINER_NAME"
  24. # Remove the container
  25. if ! docker rm "$CONTAINER_NAME" > /dev/null 2>&1; then
  26. log_error_and_exit "Failed to remove container: $CONTAINER_NAME"
  27. fi
  28. echo "Removed container: $CONTAINER_NAME"
  29. # Remove the associated volume
  30. if ! docker volume rm "$VOLUME" > /dev/null 2>&1; then
  31. log_error_and_exit "Failed to remove volume: $VOLUME"
  32. fi
  33. echo "Removed volume: $VOLUME"
  34. done
  35. echo "Script execution completed successfully."