Files
YG-Datasets/backend/Dockerfile

61 lines
1.5 KiB
Docker
Raw Normal View History

# Multi-stage build for Python FastAPI application
# Stage 1: Base image
FROM python:3.11-slim as base
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PYTHONFAULTHANDLER=1 \
PIP_NO_CACHE_DIR=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1
2026-03-17 14:36:31 +08:00
WORKDIR /app
# Stage 2: Dependencies
FROM base as deps
2026-03-17 14:36:31 +08:00
# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
2026-03-17 14:36:31 +08:00
build-essential \
libpq-dev \
&& rm -rf /var/lib/apt/lists/*
# Create virtual environment
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
2026-03-17 14:36:31 +08:00
# Install Python dependencies
COPY requirements.txt .
2026-03-17 14:36:31 +08:00
RUN pip install --no-cache-dir -r requirements.txt
# Stage 3: Production
FROM base
# Install system dependencies for production
RUN apt-get update && apt-get install -y --no-install-recommends \
libpq5 \
&& rm -rf /var/lib/apt/lists/*
# Copy virtual environment from deps stage
COPY --from=deps /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
2026-03-17 14:36:31 +08:00
# Copy application
COPY --chown=app:app . /app
RUN mkdir -p /app/uploads /app/logs && chown -R app:app /app
2026-03-17 14:36:31 +08:00
# Switch to non-root user
USER app
2026-03-17 14:36:31 +08:00
# Expose port
EXPOSE 8000
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')" || exit 1
2026-03-17 14:36:31 +08:00
# Run application
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "4"]