#!/bin/bash # Comprehensive Health Check Script for Portfolio Application # This script checks both internal container health and external accessibility set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Logging functions log() { echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1" } error() { echo -e "${RED}[ERROR]${NC} $1" >&2 } success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } log "🔍 Running comprehensive health checks..." # Check if Docker is running if ! docker info > /dev/null 2>&1; then error "Docker is not running" exit 1 fi # Check container status log "📊 Container status:" docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Command}}\t{{.Status}}\t{{.Ports}}" | grep portfolio || { error "No portfolio containers found" exit 1 } # Check if containers are running if ! docker ps | grep -q portfolio-app; then error "Portfolio app container is not running" exit 1 fi if ! docker ps | grep -q portfolio-postgres; then error "PostgreSQL container is not running" exit 1 fi if ! docker ps | grep -q portfolio-redis; then error "Redis container is not running" exit 1 fi # Check application health from inside the container log "🏥 Checking application container..." HEALTH_RESPONSE=$(docker exec portfolio-app curl -s http://localhost:3000/api/health 2>/dev/null || echo "FAILED") if [[ "$HEALTH_RESPONSE" == "FAILED" ]]; then error "Application health check failed - container not responding" exit 1 fi # Parse health response if echo "$HEALTH_RESPONSE" | grep -q '"status":"healthy"'; then success "Application health check passed!" echo "$HEALTH_RESPONSE" else error "Application health check failed - unhealthy status" echo "$HEALTH_RESPONSE" exit 1 fi # Check external accessibility log "🌐 Checking external accessibility..." # Try multiple methods to check if the app is accessible EXTERNAL_ACCESSIBLE=false # Method 1: Check if port 3000 is bound and accessible if netstat -tlnp 2>/dev/null | grep -q ":3000 "; then log "Port 3000 is bound" EXTERNAL_ACCESSIBLE=true fi # Method 2: Try to connect to the application if curl -f http://localhost:3000/api/health > /dev/null 2>&1; then log "Application is accessible via localhost:3000" EXTERNAL_ACCESSIBLE=true fi # Method 3: Check Docker port mapping if docker port portfolio-app 3000 > /dev/null 2>&1; then log "Docker port mapping is active" EXTERNAL_ACCESSIBLE=true fi if [ "$EXTERNAL_ACCESSIBLE" = true ]; then success "✅ Main page is accessible!" else warning "⚠️ Main page accessibility check inconclusive" log "This might be normal if running behind a reverse proxy" fi # Check database connectivity log "🗄️ Checking database connectivity..." if docker exec portfolio-postgres pg_isready -U portfolio_user -d portfolio_db > /dev/null 2>&1; then success "Database is healthy" else error "Database health check failed" exit 1 fi # Check Redis connectivity log "🔴 Checking Redis connectivity..." if docker exec portfolio-redis redis-cli ping > /dev/null 2>&1; then success "Redis is healthy" else error "Redis health check failed" exit 1 fi # Final status success "🎉 All health checks passed!" log "Application is running and healthy" log "Container status:" docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | grep portfolio exit 0