Files
mtgonline/backend/Dockerfile
T
akadmin 0c805b1442 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
2026-08-25 03:14:30 +00:00

84 lines
2.3 KiB
Docker

# Build stage
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/*
# 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
# 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/
# Copy Alembic configuration
COPY --chown=appuser:appuser alembic.ini ./
COPY --chown=appuser:appuser alembic/ ./alembic/
# Copy scripts (including entrypoint)
COPY --chown=appuser:appuser scripts/ ./scripts/
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
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONPATH=/app
# Logging configuration
ENV MTG_LOG_LEVEL=INFO
ENV MTG_LOG_FORMAT=text
# Create logging directory with proper permissions
RUN mkdir -p /app/logs && chown appuser:appuser /app/logs
# 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
# 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", "*"]