feat: Update Docker and Kubernetes for database infrastructure
- Update backend Dockerfile with PostgreSQL deps and entrypoint - Add entrypoint.sh with db/redis wait and auto-migration - Add /ready endpoint for Kubernetes readiness probe - Enhance /health endpoint with database and Redis status - Update k8s deployment with PostgreSQL and Redis services - Add proper secrets management for database credentials - Update k8s readiness probe to use /ready endpoint
This commit is contained in:
@@ -9,6 +9,7 @@ WORKDIR /app
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
build-essential \
|
||||
libmagic1 \
|
||||
libpq-dev \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Copy requirements first for better caching
|
||||
@@ -28,6 +29,7 @@ WORKDIR /app
|
||||
# Install runtime dependencies only
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
libmagic1 \
|
||||
libpq5 \
|
||||
curl \
|
||||
&& rm -rf /var/lib/apt/lists/* \
|
||||
&& apt-get clean
|
||||
@@ -46,20 +48,24 @@ RUN mkdir -p /app/uploads /app/outputs /app/logs /app/temp \
|
||||
# Copy application code
|
||||
COPY --chown=translator:translator . .
|
||||
|
||||
# Make entrypoint executable
|
||||
RUN chmod +x docker/backend/entrypoint.sh
|
||||
|
||||
# Switch to non-root user
|
||||
USER translator
|
||||
|
||||
# Environment variables
|
||||
ENV PYTHONUNBUFFERED=1 \
|
||||
PYTHONDONTWRITEBYTECODE=1 \
|
||||
PORT=8000
|
||||
PORT=8000 \
|
||||
WORKERS=4
|
||||
|
||||
# Health check
|
||||
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
|
||||
HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \
|
||||
CMD curl -f http://localhost:${PORT}/health || exit 1
|
||||
|
||||
# Expose port
|
||||
EXPOSE ${PORT}
|
||||
|
||||
# Run with uvicorn
|
||||
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "4"]
|
||||
# Use entrypoint script to handle migrations and startup
|
||||
ENTRYPOINT ["docker/backend/entrypoint.sh"]
|
||||
|
||||
66
docker/backend/entrypoint.sh
Normal file
66
docker/backend/entrypoint.sh
Normal file
@@ -0,0 +1,66 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
echo "🚀 Starting Document Translation API..."
|
||||
|
||||
# Wait for database to be ready (if DATABASE_URL is set)
|
||||
if [ -n "$DATABASE_URL" ]; then
|
||||
echo "⏳ Waiting for database to be ready..."
|
||||
|
||||
# Extract host and port from DATABASE_URL
|
||||
# postgresql://user:pass@host:port/db
|
||||
DB_HOST=$(echo $DATABASE_URL | sed -e 's/.*@\([^:]*\):.*/\1/')
|
||||
DB_PORT=$(echo $DATABASE_URL | sed -e 's/.*:\([0-9]*\)\/.*/\1/')
|
||||
|
||||
# Wait up to 30 seconds for database
|
||||
for i in {1..30}; do
|
||||
if python -c "
|
||||
import socket
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
try:
|
||||
s.connect(('$DB_HOST', $DB_PORT))
|
||||
s.close()
|
||||
exit(0)
|
||||
except:
|
||||
exit(1)
|
||||
" 2>/dev/null; then
|
||||
echo "✅ Database is ready!"
|
||||
break
|
||||
fi
|
||||
echo " Waiting for database... ($i/30)"
|
||||
sleep 1
|
||||
done
|
||||
|
||||
# Run database migrations
|
||||
echo "📦 Running database migrations..."
|
||||
alembic upgrade head || echo "⚠️ Migration skipped (may already be up to date)"
|
||||
fi
|
||||
|
||||
# Wait for Redis if configured
|
||||
if [ -n "$REDIS_URL" ]; then
|
||||
echo "⏳ Waiting for Redis..."
|
||||
REDIS_HOST=$(echo $REDIS_URL | sed -e 's/redis:\/\/\([^:]*\):.*/\1/')
|
||||
REDIS_PORT=$(echo $REDIS_URL | sed -e 's/.*:\([0-9]*\)\/.*/\1/')
|
||||
|
||||
for i in {1..10}; do
|
||||
if python -c "
|
||||
import socket
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
try:
|
||||
s.connect(('$REDIS_HOST', $REDIS_PORT))
|
||||
s.close()
|
||||
exit(0)
|
||||
except:
|
||||
exit(1)
|
||||
" 2>/dev/null; then
|
||||
echo "✅ Redis is ready!"
|
||||
break
|
||||
fi
|
||||
echo " Waiting for Redis... ($i/10)"
|
||||
sleep 1
|
||||
done
|
||||
fi
|
||||
|
||||
# Start the application
|
||||
echo "🎯 Starting uvicorn..."
|
||||
exec uvicorn main:app --host 0.0.0.0 --port ${PORT:-8000} --workers ${WORKERS:-4}
|
||||
Reference in New Issue
Block a user