Enhance Docker deployment: structured logging, health checks, resource limits, entrypoint script
- Add structured logging config with JSON/text format support - Create docker-entrypoint.sh with dependency health checks - Enhance /health endpoint with DB and Redis connectivity checks - Add resource limits (CPU/memory) for all services - Add network aliases for service discovery - Add container hostnames for better identification - Reduce health check timeout from 20s to 5s - Add build metadata labels to Dockerfile - Use ENTRYPOINT for dependency checking before app startup - Log rotation: 50m per file, 5 files max
This commit is contained in:
Executable
+57
@@ -0,0 +1,57 @@
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
|
||||
echo "============================================"
|
||||
echo " MTG Online Backend - Starting"
|
||||
echo " $(date -u '+%Y-%m-%d %H:%M:%S UTC')"
|
||||
echo "============================================"
|
||||
|
||||
# Wait for PostgreSQL to be ready
|
||||
echo "[entrypoint] Waiting for PostgreSQL databases..."
|
||||
for db in mtgo_platform mtgdata mtgo_mirror; do
|
||||
host="postgres_${db}"
|
||||
echo " Checking ${host}..."
|
||||
until python -c "
|
||||
import asyncio, asyncpg
|
||||
async def check():
|
||||
try:
|
||||
conn = await asyncpg.connect(host='${host}', port=5432, user='postgres', password='postgres', database='${db}')
|
||||
await conn.close()
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
asyncio.run(check())
|
||||
" 2>/dev/null; do
|
||||
echo " ${host} not ready, waiting 2s..."
|
||||
sleep 2
|
||||
done
|
||||
echo " ${host} is ready!"
|
||||
done
|
||||
|
||||
# Wait for Redis to be ready
|
||||
echo "[entrypoint] Waiting for Redis..."
|
||||
until python -c "
|
||||
import asyncio, redis.asyncio as aioredis
|
||||
async def check():
|
||||
try:
|
||||
r = aioredis.from_url('redis://redis:6379/0')
|
||||
await r.ping()
|
||||
await r.close()
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
asyncio.run(check())
|
||||
" 2>/dev/null; do
|
||||
echo " Redis not ready, waiting 2s..."
|
||||
sleep 2
|
||||
done
|
||||
echo " Redis is ready!"
|
||||
|
||||
# Run database migrations
|
||||
echo "[entrypoint] Running Alembic migrations..."
|
||||
python -m alembic upgrade head
|
||||
echo "[entrypoint] Migrations complete."
|
||||
|
||||
# Start the application
|
||||
echo "[entrypoint] Starting uvicorn..."
|
||||
exec python -m uvicorn app.main:app --host 0.0.0.0 --port 8000 --forwarded-allow-ips '*' --log-level info
|
||||
Executable
+236
@@ -0,0 +1,236 @@
|
||||
#!/bin/bash
|
||||
# MTG Online Backend - Docker Deployment Helper Scripts
|
||||
# ============================================================
|
||||
|
||||
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
|
||||
|
||||
# Project root directory
|
||||
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
|
||||
echo -e "${BLUE}========================================${NC}"
|
||||
echo -e "${BLUE} MTG Online Backend - Docker Deployment${NC}"
|
||||
echo -e "${BLUE}========================================${NC}"
|
||||
echo ""
|
||||
|
||||
# Function to display usage
|
||||
usage() {
|
||||
echo "Usage: $0 <command>"
|
||||
echo ""
|
||||
echo "Commands:"
|
||||
echo " start - Start all services"
|
||||
echo " stop - Stop all services"
|
||||
echo " restart - Restart all services"
|
||||
echo " logs - View logs (follow mode)"
|
||||
echo " logs-app - View application logs only"
|
||||
echo " logs-db - View database logs only"
|
||||
echo " status - Show service status"
|
||||
echo " health - Check health of all services"
|
||||
echo " exec-app - Execute command in backend container"
|
||||
echo " exec-db - Execute command in database container"
|
||||
echo " migrate - Run database migrations"
|
||||
echo " cleanup - Stop and remove all containers and volumes"
|
||||
echo " rebuild - Rebuild and start all services"
|
||||
echo " help - Show this help message"
|
||||
echo ""
|
||||
exit 0
|
||||
}
|
||||
|
||||
# Function to check if docker is running
|
||||
check_docker() {
|
||||
if ! docker info > /dev/null 2>&1; then
|
||||
echo -e "${RED}Error: Docker is not running${NC}"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to check if docker-compose is available
|
||||
check_compose() {
|
||||
if ! command -v docker-compose &> /dev/null; then
|
||||
echo -e "${RED}Error: docker-compose is not installed${NC}"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to start services
|
||||
start_services() {
|
||||
echo -e "${GREEN}Starting MTG Online Backend services...${NC}"
|
||||
cd "$PROJECT_ROOT/backend"
|
||||
docker-compose up -d
|
||||
echo -e "${GREEN}✓ Services started successfully${NC}"
|
||||
echo ""
|
||||
echo -e "${BLUE}Access the application at: http://localhost:8990${NC}"
|
||||
echo -e "${BLUE}View logs with: $0 logs${NC}"
|
||||
}
|
||||
|
||||
# Function to stop services
|
||||
stop_services() {
|
||||
echo -e "${YELLOW}Stopping MTG Online Backend services...${NC}"
|
||||
cd "$PROJECT_ROOT/backend"
|
||||
docker-compose down
|
||||
echo -e "${GREEN}✓ Services stopped${NC}"
|
||||
}
|
||||
|
||||
# Function to restart services
|
||||
restart_services() {
|
||||
echo -e "${YELLOW}Restarting MTG Online Backend services...${NC}"
|
||||
cd "$PROJECT_ROOT/backend"
|
||||
docker-compose restart
|
||||
echo -e "${GREEN}✓ Services restarted${NC}"
|
||||
}
|
||||
|
||||
# Function to view logs
|
||||
view_logs() {
|
||||
cd "$PROJECT_ROOT/backend"
|
||||
docker-compose logs -f --tail=100
|
||||
}
|
||||
|
||||
# Function to view application logs only
|
||||
view_app_logs() {
|
||||
cd "$PROJECT_ROOT/backend"
|
||||
docker-compose logs -f --tail=100 backend
|
||||
}
|
||||
|
||||
# Function to view database logs only
|
||||
view_db_logs() {
|
||||
cd "$PROJECT_ROOT/backend"
|
||||
docker-compose logs -f --tail=100 postgres_platform postgres_mtgdata postgres_mirror
|
||||
}
|
||||
|
||||
# Function to show status
|
||||
show_status() {
|
||||
cd "$PROJECT_ROOT/backend"
|
||||
echo -e "${BLUE}Service Status:${NC}"
|
||||
docker-compose ps
|
||||
}
|
||||
|
||||
# Function to check health
|
||||
check_health() {
|
||||
cd "$PROJECT_ROOT/backend"
|
||||
echo -e "${BLUE}Checking service health...${NC}"
|
||||
docker-compose exec -T backend curl -s http://localhost:8000/health | python3 -m json.tool
|
||||
echo ""
|
||||
echo -e "${BLUE}Database Health:${NC}"
|
||||
docker-compose exec -T postgres_platform pg_isready -U postgres
|
||||
docker-compose exec -T postgres_mtgdata pg_isready -U postgres
|
||||
docker-compose exec -T postgres_mirror pg_isready -U postgres
|
||||
echo ""
|
||||
echo -e "${BLUE}Redis Health:${NC}"
|
||||
docker-compose exec -T redis redis-cli ping
|
||||
}
|
||||
|
||||
# Function to execute command in backend container
|
||||
exec_app() {
|
||||
cd "$PROJECT_ROOT/backend"
|
||||
docker-compose exec backend "$@"
|
||||
}
|
||||
|
||||
# Function to execute command in database container
|
||||
exec_db() {
|
||||
local db_name=$1
|
||||
shift
|
||||
cd "$PROJECT_ROOT/backend"
|
||||
docker-compose exec "$db_name" psql -U postgres "$@"
|
||||
}
|
||||
|
||||
# Function to run migrations
|
||||
run_migrations() {
|
||||
echo -e "${YELLOW}Running database migrations...${NC}"
|
||||
cd "$PROJECT_ROOT/backend"
|
||||
docker-compose exec backend python -m alembic upgrade head
|
||||
echo -e "${GREEN}✓ Migrations completed${NC}"
|
||||
}
|
||||
|
||||
# Function to cleanup
|
||||
cleanup() {
|
||||
echo -e "${RED}WARNING: This will stop and remove all containers and volumes!${NC}"
|
||||
read -p "Are you sure? (y/N) " -n 1 -r
|
||||
echo ""
|
||||
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
||||
cd "$PROJECT_ROOT/backend"
|
||||
docker-compose down -v
|
||||
echo -e "${GREEN}✓ All containers and volumes removed${NC}"
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to rebuild
|
||||
rebuild() {
|
||||
echo -e "${YELLOW}Rebuilding and starting all services...${NC}"
|
||||
cd "$PROJECT_ROOT/backend"
|
||||
docker-compose down
|
||||
docker-compose build --no-cache
|
||||
docker-compose up -d
|
||||
echo -e "${GREEN}✓ Services rebuilt and started${NC}"
|
||||
echo ""
|
||||
echo -e "${BLUE}Access the application at: http://localhost:8990${NC}"
|
||||
}
|
||||
|
||||
# Main command handler
|
||||
case "${1:-help}" in
|
||||
start)
|
||||
check_docker
|
||||
start_services
|
||||
;;
|
||||
stop)
|
||||
check_docker
|
||||
stop_services
|
||||
;;
|
||||
restart)
|
||||
check_docker
|
||||
restart_services
|
||||
;;
|
||||
logs)
|
||||
check_docker
|
||||
view_logs
|
||||
;;
|
||||
logs-app)
|
||||
check_docker
|
||||
view_app_logs
|
||||
;;
|
||||
logs-db)
|
||||
check_docker
|
||||
view_db_logs
|
||||
;;
|
||||
status)
|
||||
check_docker
|
||||
show_status
|
||||
;;
|
||||
health)
|
||||
check_docker
|
||||
check_health
|
||||
;;
|
||||
exec-app)
|
||||
check_docker
|
||||
shift
|
||||
exec_app "$@"
|
||||
;;
|
||||
exec-db)
|
||||
check_docker
|
||||
exec_db "$@"
|
||||
;;
|
||||
migrate)
|
||||
check_docker
|
||||
run_migrations
|
||||
;;
|
||||
cleanup)
|
||||
check_docker
|
||||
cleanup
|
||||
;;
|
||||
rebuild)
|
||||
check_docker
|
||||
rebuild
|
||||
;;
|
||||
help|--help|-h)
|
||||
usage
|
||||
;;
|
||||
*)
|
||||
echo -e "${RED}Unknown command: $1${NC}"
|
||||
usage
|
||||
;;
|
||||
esac
|
||||
Reference in New Issue
Block a user