#!/bin/bash # Fix Connection Issues Script # This script diagnoses and fixes common connection issues 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 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 "🔧 Diagnosing and fixing connection issues..." # Check if containers are running if ! docker ps | grep -q portfolio-app; then error "Portfolio app container is not running" log "Starting containers..." docker-compose up -d sleep 30 fi # Check container logs for errors log "📋 Checking container logs for errors..." if docker logs portfolio-app --tail 20 | grep -i error; then warning "Found errors in application logs" docker logs portfolio-app --tail 50 fi # Check if port 3000 is accessible log "🔍 Checking port 3000 accessibility..." # Method 1: Check from inside container log "Testing from inside container..." if docker exec portfolio-app curl -f http://localhost:3000/api/health > /dev/null 2>&1; then success "Application responds from inside container" else error "Application not responding from inside container" docker logs portfolio-app --tail 20 fi # Method 2: Check port binding log "Checking port binding..." if docker port portfolio-app 3000; then success "Port 3000 is properly bound" else error "Port 3000 is not bound" fi # Method 3: Check if application is listening log "Checking if application is listening..." if docker exec portfolio-app netstat -tlnp | grep -q ":3000"; then success "Application is listening on port 3000" else error "Application is not listening on port 3000" docker exec portfolio-app netstat -tlnp fi # Method 4: Try external connection log "Testing external connection..." if timeout 5 curl -f http://localhost:3000/api/health > /dev/null 2>&1; then success "External connection successful" else warning "External connection failed - this might be normal if behind reverse proxy" # Check if there's a reverse proxy running if netstat -tlnp | grep -q ":80\|:443"; then log "Reverse proxy detected - this is expected behavior" success "Application is running behind reverse proxy" else error "No reverse proxy detected and external connection failed" # Try to restart the container log "Attempting to restart portfolio container..." docker restart portfolio-app sleep 10 if timeout 5 curl -f http://localhost:3000/api/health > /dev/null 2>&1; then success "External connection successful after restart" else error "External connection still failing after restart" fi fi fi # Check network configuration log "🌐 Checking network configuration..." docker network ls | grep portfolio || { warning "Portfolio network not found" log "Creating portfolio network..." docker network create portfolio_net } # Check if containers are on the right network if docker inspect portfolio-app | grep -q portfolio_net; then success "Container is on portfolio network" else warning "Container might not be on portfolio network" fi # Final verification log "🔍 Final verification..." if docker exec portfolio-app curl -f http://localhost:3000/api/health > /dev/null 2>&1; then success "✅ Application is healthy and responding" # Show final status log "📊 Final container status:" docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | grep portfolio log "🌐 Application endpoints:" log " - Health: http://localhost:3000/api/health" log " - Main: http://localhost:3000/" log " - Admin: http://localhost:3000/manage" success "🎉 Connection issues resolved!" else error "❌ Application is still not responding" log "Please check the logs: docker logs portfolio-app" exit 1 fi