#!/bin/bash # Production diagnosis script # Checks container status, network connectivity, and API endpoints set -e echo "๐Ÿ” Production Diagnosis Script" echo "==============================" echo "" # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Check if docker is running if ! docker info > /dev/null 2>&1; then echo -e "${RED}โŒ Docker is not running${NC}" exit 1 fi echo -e "${GREEN}โœ… Docker is running${NC}" echo "" # Check container status echo "๐Ÿ“ฆ Container Status:" echo "-------------------" CONTAINER_ID=$(docker ps -q -f "name=portfolio-app") if [ -z "$CONTAINER_ID" ]; then echo -e "${RED}โŒ portfolio-app container is not running${NC}" echo "" echo "Checking for stopped containers:" docker ps -a -f "name=portfolio-app" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" exit 1 else echo -e "${GREEN}โœ… Container is running (ID: ${CONTAINER_ID})${NC}" docker ps -f "name=portfolio-app" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}\t{{.Health}}" fi echo "" # Check container health echo "๐Ÿฅ Container Health:" echo "-------------------" HEALTH=$(docker inspect portfolio-app --format='{{.State.Health.Status}}' 2>/dev/null || echo "no-healthcheck") STATUS=$(docker inspect portfolio-app --format='{{.State.Status}}' 2>/dev/null || echo "unknown") echo "Status: $STATUS" echo "Health: $HEALTH" echo "" # Check networks echo "๐ŸŒ Network Connectivity:" echo "----------------------" if docker network ls | grep -q "proxy"; then echo -e "${GREEN}โœ… 'proxy' network exists${NC}" if docker inspect portfolio-app --format='{{range $net, $conf := .NetworkSettings.Networks}}{{$net}} {{end}}' | grep -q "proxy"; then echo -e "${GREEN}โœ… Container is connected to 'proxy' network${NC}" else echo -e "${YELLOW}โš ๏ธ Container is NOT connected to 'proxy' network${NC}" echo "Connected networks:" docker inspect portfolio-app --format='{{range $net, $conf := .NetworkSettings.Networks}}{{$net}} {{end}}' fi else echo -e "${RED}โŒ 'proxy' network does not exist${NC}" echo "Creating proxy network..." docker network create proxy || echo "Failed to create network (may already exist)" fi echo "" # Check port mapping echo "๐Ÿ”Œ Port Mapping:" echo "---------------" PORT_MAPPING=$(docker port portfolio-app 2>/dev/null | grep 3000 || echo "No port mapping found") if [ -n "$PORT_MAPPING" ]; then echo -e "${GREEN}โœ… Port mapping: $PORT_MAPPING${NC}" else echo -e "${RED}โŒ No port mapping found for port 3000${NC}" fi echo "" # Test from inside container echo "๐Ÿงช Testing from inside container:" echo "-------------------------------" if docker exec portfolio-app curl -f -s --max-time 5 http://localhost:3000/api/health > /dev/null 2>&1; then echo -e "${GREEN}โœ… Health endpoint responds from inside container${NC}" docker exec portfolio-app curl -s http://localhost:3000/api/health | head -5 else echo -e "${RED}โŒ Health endpoint does not respond from inside container${NC}" echo "Container logs (last 20 lines):" docker logs portfolio-app --tail=20 fi echo "" # Test from host echo "๐Ÿงช Testing from host:" echo "-------------------" if curl -f -s --max-time 5 http://localhost:3000/api/health > /dev/null 2>&1; then echo -e "${GREEN}โœ… Health endpoint responds from host${NC}" curl -s http://localhost:3000/api/health | head -5 else echo -e "${RED}โŒ Health endpoint does not respond from host${NC}" echo "This is normal if the container is only accessible via proxy network" fi echo "" # Check environment variables echo "๐Ÿ” Environment Variables:" echo "------------------------" echo "N8N_WEBHOOK_URL: $(docker exec portfolio-app printenv N8N_WEBHOOK_URL 2>/dev/null | grep -v '^$' || echo 'NOT SET')" echo "N8N_SECRET_TOKEN: $(docker exec portfolio-app printenv N8N_SECRET_TOKEN 2>/dev/null | grep -v '^$' | sed 's/./*/g' || echo 'NOT SET')" echo "N8N_API_KEY: $(docker exec portfolio-app printenv N8N_API_KEY 2>/dev/null | grep -v '^$' | sed 's/./*/g' || echo 'NOT SET')" echo "NODE_ENV: $(docker exec portfolio-app printenv NODE_ENV 2>/dev/null || echo 'NOT SET')" echo "NEXT_PUBLIC_BASE_URL: $(docker exec portfolio-app printenv NEXT_PUBLIC_BASE_URL 2>/dev/null || echo 'NOT SET')" echo "" # Check container logs for errors echo "๐Ÿ“‹ Recent Container Logs (last 30 lines):" echo "----------------------------------------" docker logs portfolio-app --tail=30 2>&1 | tail -30 echo "" # Check API endpoints echo "๐ŸŒ Testing API Endpoints:" echo "------------------------" ENDPOINTS=("/api/health" "/api/n8n/status" "/api/n8n/chat") for endpoint in "${ENDPOINTS[@]}"; do echo -n "Testing $endpoint: " if docker exec portfolio-app curl -f -s --max-time 5 "http://localhost:3000${endpoint}" > /dev/null 2>&1; then echo -e "${GREEN}โœ… OK${NC}" else echo -e "${RED}โŒ FAILED${NC}" fi done echo "" # Summary echo "๐Ÿ“Š Summary:" echo "---------" if [ "$STATUS" == "running" ] && [ "$HEALTH" == "healthy" ]; then echo -e "${GREEN}โœ… Container appears to be healthy${NC}" echo "" echo "If you're still seeing 502 errors:" echo "1. Check Nginx Proxy Manager configuration" echo "2. Ensure the proxy host points to: portfolio-app:3000 (not localhost:3000)" echo "3. Ensure the proxy network is correctly configured" echo "4. Check Nginx Proxy Manager logs" else echo -e "${RED}โŒ Container has issues${NC}" echo "" echo "Recommended actions:" if [ "$STATUS" != "running" ]; then echo "1. Restart the container: docker compose -f docker-compose.production.yml restart portfolio" fi if [ "$HEALTH" != "healthy" ]; then echo "2. Check container logs: docker logs portfolio-app --tail=50" echo "3. Check if the application is starting correctly" fi fi