#!/bin/bash # Docker Compose Reset Script # This script performs a complete reset of the Docker Compose environment: # - Stops and removes containers, networks, and volumes # - Explicitly removes Supabase volumes (supabase-db, supabase-storage) # - Prunes unused Docker resources (containers, images, networks, volumes) # - Rebuilds and starts all services in detached mode # - Waits for Supabase migrations to complete set -e # Exit on error echo "=== Docker Compose Reset ===" echo "" # Get the project root directory (parent of scripts directory) SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" # Change to project root directory cd "$PROJECT_ROOT" echo "Working directory: $PROJECT_ROOT" echo "" echo "1. Stopping and removing containers, networks, and volumes..." docker compose down -v echo "" echo "2. Removing Supabase volumes explicitly..." # Find and remove Supabase volumes (they may be named with project prefix) # Docker Compose typically names volumes as: _ # Try to find volumes containing 'supabase-db' or 'supabase-storage' SUPABASE_VOLUMES=$(docker volume ls --format "{{.Name}}" | grep -E "(supabase-db|supabase-storage)" || true) if [ -n "$SUPABASE_VOLUMES" ]; then echo "$SUPABASE_VOLUMES" | while read -r volume; do echo " - Removing Supabase volume: $volume" docker volume rm "$volume" 2>/dev/null || echo " (Volume may have been removed already or is in use)" done else echo " - No Supabase volumes found (they may have been removed already)" fi echo "" echo "3. Pruning unused Docker resources..." echo " - Pruning unused containers..." docker container prune -f echo " - Pruning unused images..." docker image prune -af echo " - Pruning unused networks..." docker network prune -f echo " - Pruning unused volumes..." docker volume prune -f echo "" echo "4. Rebuilding and starting all services in detached mode..." docker compose up --build -d echo "" echo "5. Waiting for Supabase database to be ready..." # Wait for database to be healthy MAX_WAIT=60 WAIT_COUNT=0 while [ $WAIT_COUNT -lt $MAX_WAIT ]; do if docker compose ps supabase-db | grep -q "healthy"; then echo " ✓ Supabase database is healthy" break fi echo " Waiting for database... ($WAIT_COUNT/$MAX_WAIT seconds)" sleep 2 WAIT_COUNT=$((WAIT_COUNT + 2)) done if [ $WAIT_COUNT -ge $MAX_WAIT ]; then echo " ⚠ Warning: Database may not be fully ready" fi echo "" echo "6. Waiting for Supabase migrations to complete..." # Wait for migration container to complete (it has restart: "no", so it should exit when done) MAX_WAIT=120 WAIT_COUNT=0 MIGRATE_CONTAINER="usda-vision-supabase-migrate" while [ $WAIT_COUNT -lt $MAX_WAIT ]; do # Check if container exists and its status if docker ps -a --format "{{.Names}}\t{{.Status}}" | grep -q "^${MIGRATE_CONTAINER}"; then CONTAINER_STATUS=$(docker ps -a --format "{{.Names}}\t{{.Status}}" | grep "^${MIGRATE_CONTAINER}" | awk '{print $2}') if echo "$CONTAINER_STATUS" | grep -q "Exited"; then EXIT_CODE=$(docker inspect "$MIGRATE_CONTAINER" --format='{{.State.ExitCode}}' 2>/dev/null || echo "1") if [ "$EXIT_CODE" = "0" ]; then echo " ✓ Supabase migrations completed successfully" break else echo " ⚠ Warning: Migrations may have failed (exit code: $EXIT_CODE)" echo " Check logs with: docker compose logs supabase-migrate" break fi fi fi echo " Waiting for migrations... ($WAIT_COUNT/$MAX_WAIT seconds)" sleep 2 WAIT_COUNT=$((WAIT_COUNT + 2)) done if [ $WAIT_COUNT -ge $MAX_WAIT ]; then echo " ⚠ Warning: Migration timeout - check logs with: docker compose logs supabase-migrate" echo " Note: Migrations may still be running or the container may not have started yet" fi echo "" echo "7. Checking service status..." docker compose ps echo "" echo "=== Docker Compose Reset Complete ===" echo "" echo "All services have been reset and are running in detached mode." echo "" echo "Useful commands:" echo " - View all logs: docker compose logs -f" echo " - View Supabase logs: docker compose logs -f supabase-db supabase-rest supabase-auth" echo " - View migration logs: docker compose logs supabase-migrate" echo " - Check service status: docker compose ps" echo " - Access Supabase Studio: http://localhost:54323" echo ""