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
|
||||
Reference in New Issue
Block a user