Date: November 3, 2025
Symptom: 502 Bad Gateway errors, site unavailable
Root Cause: Frontend container exited cleanly (exit code 0) but didn't restart automatically
Impact: Site down for ~10 minutes until manual intervention
-
Restart Policy:
restart: unless-stoppedmeans containers won't restart if:- Docker daemon restarts while container is stopped
- Container is manually stopped
- Container exits cleanly due to internal issues
-
No Health Checks: Docker had no way to detect if services were actually working correctly
-
No Monitoring: No automated checks to detect and fix issues
Changed: restart: unless-stopped → restart: always
Why: Ensures containers ALWAYS restart, even after:
- Manual stops
- Docker daemon restarts
- System reboots
- Clean exits
Location: docker-compose.yml - all services
All services now have health checks that verify they're actually responding:
- Frontend: Checks if nginx is serving HTTP requests
- Backend: Checks if API is responding to
/api/config - Nginx: Checks if reverse proxy is working
- Redis: Checks if Redis is accepting connections
Configuration:
healthcheck:
test: ["CMD", "..."]
interval: 30s # Check every 30 seconds
timeout: 10s # Fail if no response in 10s
retries: 3 # Retry 3 times before marking unhealthy
start_period: 10s # Wait 10s before starting checksNginx now waits for backend/frontend to be healthy (not just started) before starting:
depends_on:
frontend:
condition: service_healthy
backend:
condition: service_healthyLocation: deployment/monitor-services.sh
What it does:
- Checks all services every 5 minutes (via cron)
- Detects stopped/unhealthy containers
- Automatically restarts failed services
- Logs all events to
/var/log/chainviz-monitor.log - Rotates logs when they get too large
Setup:
# Add to crontab
*/5 * * * * /home/ubuntu/ChainViz/deployment/monitor-services.sh >> /home/ubuntu/ChainViz/logs/chainviz-monitor.log 2>&1cd /home/ubuntu/ChainViz
git pull origin main
sudo docker-compose down
sudo docker-compose up -d# Add monitoring to crontab
(crontab -l 2>/dev/null; echo "*/5 * * * * /home/ubuntu/ChainViz/deployment/monitor-services.sh >> /var/log/chainviz-monitor.log 2>&1") | crontab -
# Verify it's added
crontab -l# Check health status
sudo docker-compose ps
# Check individual service health
sudo docker inspect chainviz_frontend_1 | grep -A 10 Health# Check service status
sudo docker-compose ps
# Check health
sudo docker ps --format "table {{.Names}}\t{{.Status}}"
# Check logs for errors
sudo docker-compose logs --tail=50 | grep -i error# Review monitor logs
tail -100 /var/log/chainviz-monitor.log | grep -i warning
# Check disk space
df -h
# Check Docker resource usage
sudo docker stats --no-streamConsider setting up:
-
CloudWatch Alarms for:
- High HTTP error rates (502s)
- Container restart counts
- CPU/Memory usage
-
Email/Slack Notifications for:
- Service failures
- Repeated restarts
- Disk space warnings
# Check logs
sudo docker-compose logs [service-name]
# Check health status
sudo docker inspect [container-id] | grep -A 10 Health
# Restart manually
sudo docker-compose restart [service-name]# Test health check command manually
sudo docker exec chainviz_backend_1 python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/api/config').read()"
# If it fails, check the service directly
curl http://localhost:8000/api/config# Test manually
/home/ubuntu/ChainViz/deployment/monitor-services.sh
# Check cron is running
sudo service cron status
# Check cron logs
grep CRON /var/log/syslog | tail -20- Always use
restart: alwaysfor production services - Always add health checks to detect real failures
- Monitor service health regularly (automated + manual)
- Keep logs - review them weekly for patterns
- Test disaster recovery - practice restarting services
- Document incidents - learn from each outage
DISK_SPACE_ANALYSIS.md- Disk space managementDEPLOYMENT_STATUS.md- Current deployment statusAWS_DEPLOYMENT_FIX.md- AWS-specific fixes
Last Updated: November 3, 2025
Maintained By: DevOps/Infrastructure Team