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:
+25
-10
@@ -3,33 +3,45 @@ FROM python:3.12-slim as builder
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Install build dependencies
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
gcc \
|
||||
libpq-dev \
|
||||
curl \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
RUN groupadd -r appuser && useradd -r -g appuser -d /app -s /sbin/nologin appuser
|
||||
|
||||
# Install Python dependencies
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir --user -r requirements.txt
|
||||
|
||||
# Application stage
|
||||
FROM python:3.12-slim
|
||||
|
||||
LABEL org.label-schema.name="mtgonline-backend" \
|
||||
org.label-schema.description="MTG Online Backend API" \
|
||||
org.label-schema.version="0.2.0" \
|
||||
org.label-schema.build-date="${BUILD_DATE}" \
|
||||
org.label-schema.vcs-url="https://gitea.wallomation.com/wall-o/mtgonline"
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Install runtime dependencies
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
libpq5 \
|
||||
curl \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Copy installed packages from builder
|
||||
COPY --from=builder /root/.local /app/.local
|
||||
|
||||
ENV PATH=/app/.local/bin:$PATH
|
||||
|
||||
# Create non-root user
|
||||
RUN groupadd -r appuser && useradd -r -g appuser -d /app -s /sbin/nologin appuser
|
||||
|
||||
RUN mkdir -p /app/data /app/uploads /app/logs /app/scripts /app/alembic/versions && chown -R appuser:appuser /app
|
||||
# Create necessary directories
|
||||
RUN mkdir -p /app/data /app/uploads /app/logs /app/scripts /app/alembic/versions && \
|
||||
chown -R appuser:appuser /app
|
||||
|
||||
# Copy application code
|
||||
COPY --chown=appuser:appuser app/ ./app/
|
||||
@@ -38,31 +50,34 @@ COPY --chown=appuser:appuser app/ ./app/
|
||||
COPY --chown=appuser:appuser alembic.ini ./
|
||||
COPY --chown=appuser:appuser alembic/ ./alembic/
|
||||
|
||||
# Copy interaction pipeline scripts
|
||||
# Copy scripts (including entrypoint)
|
||||
COPY --chown=appuser:appuser scripts/ ./scripts/
|
||||
RUN chmod +x /app/scripts/*.py
|
||||
RUN chmod +x /app/scripts/*.py /app/scripts/docker-entrypoint.sh
|
||||
|
||||
# Copy environment example
|
||||
COPY --chown=appuser:appuser .env.example ./.env.example
|
||||
COPY --chown=appuser:appuser pyproject.toml ./
|
||||
|
||||
# Python environment variables for proper logging
|
||||
# Python environment variables
|
||||
ENV PYTHONUNBUFFERED=1
|
||||
ENV PYTHONDONTWRITEBYTECODE=1
|
||||
ENV PYTHONPATH=/app
|
||||
|
||||
# Logging configuration
|
||||
ENV MTG_LOG_LEVEL=INFO
|
||||
ENV MTG_LOG_FORMAT=json
|
||||
ENV MTG_LOG_FORMAT=text
|
||||
|
||||
# Create logging directory with proper permissions
|
||||
RUN mkdir -p /app/logs && chown appuser:appuser /app/logs
|
||||
|
||||
HEALTHCHECK --interval=30s --timeout=20s --start-period=5s --retries=3 \
|
||||
# Health check
|
||||
HEALTHCHECK --interval=30s --timeout=5s --start-period=40s --retries=3 \
|
||||
CMD curl -f http://localhost:8000/health || exit 1
|
||||
|
||||
USER appuser
|
||||
|
||||
EXPOSE 8000
|
||||
|
||||
# Run migrations before starting the app
|
||||
CMD ["sh", "-c", "python -m alembic upgrade head && python -m uvicorn app.main:app --host 0.0.0.0 --port 8000"]
|
||||
# Use entrypoint script for dependency checking and migrations
|
||||
ENTRYPOINT ["/app/scripts/docker-entrypoint.sh"]
|
||||
CMD ["python", "-m", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--forwarded-allow-ips", "*"]
|
||||
|
||||
Reference in New Issue
Block a user