docker-setup.sh
1 #!/bin/bash 2 # Docker Setup for ECHO Project 3 # Sets up PostgreSQL and Redis containers 4 5 set -e 6 7 ECHO_DIR="/Users/pranav/Documents/echo" 8 cd "$ECHO_DIR" 9 10 # Colors 11 GREEN='\033[0;32m' 12 BLUE='\033[0;34m' 13 YELLOW='\033[1;33m' 14 RED='\033[0;31m' 15 NC='\033[0m' 16 17 echo -e "${BLUE}╔════════════════════════════════════════════════════════════════╗${NC}" 18 echo -e "${BLUE}║ ECHO Docker Setup - PostgreSQL & Redis ║${NC}" 19 echo -e "${BLUE}╚════════════════════════════════════════════════════════════════╝${NC}" 20 echo "" 21 22 # Configuration 23 REDIS_PORT=6383 24 DB_PORT=5433 25 DB_NAME=echo_org 26 DB_USER=postgres 27 DB_PASSWORD=postgres 28 29 echo -e "${YELLOW}Step 1: Check Docker${NC}" 30 if ! docker info > /dev/null 2>&1; then 31 echo -e "${RED}❌ Docker is not running${NC}" 32 echo "Please start Docker Desktop or run: sudo systemctl start docker" 33 exit 1 34 fi 35 echo -e "${GREEN}✓ Docker is running${NC}" 36 echo "" 37 38 echo -e "${YELLOW}Step 2: Setup Redis${NC}" 39 if docker ps -a | grep -q "echo-redis"; then 40 echo "Redis container exists. Checking status..." 41 if docker ps | grep -q "echo-redis.*Up"; then 42 echo -e "${GREEN}✓ Redis already running${NC}" 43 else 44 echo "Starting existing Redis container..." 45 docker start echo-redis 46 sleep 2 47 echo -e "${GREEN}✓ Redis started${NC}" 48 fi 49 else 50 echo "Creating new Redis container..." 51 docker run -d \ 52 --name echo-redis \ 53 -p $REDIS_PORT:6379 \ 54 --restart unless-stopped \ 55 redis:7-alpine 56 57 sleep 2 58 echo -e "${GREEN}✓ Redis created and started${NC}" 59 fi 60 61 # Verify Redis 62 if docker exec echo-redis redis-cli ping > /dev/null 2>&1; then 63 echo -e "${GREEN}✓ Redis responding on port $REDIS_PORT${NC}" 64 else 65 echo -e "${RED}❌ Redis not responding${NC}" 66 exit 1 67 fi 68 echo "" 69 70 echo -e "${YELLOW}Step 3: Setup PostgreSQL${NC}" 71 if docker ps -a | grep -q "echo-postgres"; then 72 echo "PostgreSQL container exists. Checking status..." 73 if docker ps | grep -q "echo-postgres.*Up"; then 74 echo -e "${GREEN}✓ PostgreSQL already running${NC}" 75 else 76 echo "Starting existing PostgreSQL container..." 77 docker start echo-postgres 78 sleep 3 79 echo -e "${GREEN}✓ PostgreSQL started${NC}" 80 fi 81 else 82 echo "Creating new PostgreSQL container..." 83 docker run -d \ 84 --name echo-postgres \ 85 -p $DB_PORT:5432 \ 86 -e POSTGRES_PASSWORD=$DB_PASSWORD \ 87 -e POSTGRES_DB=$DB_NAME \ 88 --restart unless-stopped \ 89 postgres:16-alpine 90 91 sleep 5 92 echo -e "${GREEN}✓ PostgreSQL created and started${NC}" 93 fi 94 95 # Verify PostgreSQL 96 if docker exec echo-postgres pg_isready -U postgres > /dev/null 2>&1; then 97 echo -e "${GREEN}✓ PostgreSQL responding on port $DB_PORT${NC}" 98 else 99 echo -e "${RED}❌ PostgreSQL not responding${NC}" 100 exit 1 101 fi 102 echo "" 103 104 echo -e "${YELLOW}Step 4: Run database migrations${NC}" 105 cd shared 106 echo "Running Ecto migrations..." 107 export DB_HOST=localhost 108 export DB_PORT=$DB_PORT 109 export DB_NAME=$DB_NAME 110 export DB_USER=$DB_USER 111 export DB_PASSWORD=$DB_PASSWORD 112 113 # Wait a bit more for DB to be fully ready 114 sleep 2 115 116 # Create database if it doesn't exist 117 mix ecto.create 2>/dev/null || echo "Database already exists" 118 119 # Run migrations 120 mix ecto.migrate 121 122 echo -e "${GREEN}✓ Migrations complete${NC}" 123 echo "" 124 125 cd "$ECHO_DIR" 126 127 echo -e "${BLUE}╔════════════════════════════════════════════════════════════════╗${NC}" 128 echo -e "${BLUE}║ Setup Complete! ║${NC}" 129 echo -e "${BLUE}╚════════════════════════════════════════════════════════════════╝${NC}" 130 echo "" 131 132 echo -e "${GREEN}Docker Containers:${NC}" 133 echo " Redis: echo-redis (port $REDIS_PORT)" 134 echo " PostgreSQL: echo-postgres (port $DB_PORT)" 135 echo "" 136 137 echo -e "${GREEN}Database:${NC}" 138 echo " Name: $DB_NAME" 139 echo " User: $DB_USER" 140 echo " Password: $DB_PASSWORD" 141 echo " Host: localhost" 142 echo " Port: $DB_PORT" 143 echo "" 144 145 echo -e "${YELLOW}Useful Commands:${NC}" 146 echo " Start containers: docker start echo-redis echo-postgres" 147 echo " Stop containers: docker stop echo-redis echo-postgres" 148 echo " View logs (Redis): docker logs -f echo-redis" 149 echo " View logs (PG): docker logs -f echo-postgres" 150 echo " Connect to Redis: docker exec -it echo-redis redis-cli" 151 echo " Connect to DB: docker exec -it echo-postgres psql -U postgres -d $DB_NAME" 152 echo " Remove containers: docker rm -f echo-redis echo-postgres" 153 echo "" 154 155 echo -e "${BLUE}Next Steps:${NC}" 156 echo " 1. Ensure Ollama is running: ollama serve" 157 echo " 2. Run Day 2 training: ./day2_training.sh" 158 echo "" 159 160 # Show container status 161 echo -e "${YELLOW}Container Status:${NC}" 162 docker ps --filter "name=echo-" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"