Spaces:
Sleeping
Sleeping
| # Stage 1: Build stage | |
| FROM python:3.12-slim as builder | |
| # Set environment variables | |
| ENV PYTHONDONTWRITEBYTECODE=1 | |
| ENV PYTHONUNBUFFERED=1 | |
| # Create a non-root user | |
| RUN useradd -m -u 1000 user | |
| # Set the working directory | |
| WORKDIR /app | |
| # Copy only the requirements file first to leverage Docker cache | |
| COPY --chown=user ./requirements.txt /app/requirements.txt | |
| # Install dependencies in a virtual environment | |
| RUN python -m venv /opt/venv | |
| ENV PATH="/opt/venv/bin:$PATH" | |
| RUN pip install --no-cache-dir --upgrade pip && \ | |
| pip install --no-cache-dir -r requirements.txt | |
| # Copy the rest of the application code | |
| COPY --chown=user . /app | |
| # Stage 2: Runtime stage | |
| FROM python:3.12-slim | |
| # Create a non-root user | |
| RUN useradd -m -u 1000 user | |
| USER user | |
| # Copy the virtual environment from the builder stage | |
| COPY --from=builder /opt/venv /opt/venv | |
| ENV PATH="/opt/venv/bin:$PATH" | |
| # Set the working directory | |
| WORKDIR /app | |
| # Copy only the necessary files from the builder stage | |
| COPY --from=builder --chown=user /app /app | |
| # Expose the port the app runs on | |
| EXPOSE 7860 | |
| # Health check to ensure the application is running | |
| HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \ | |
| CMD curl -f http://localhost:7860/health || exit 1 | |
| # Command to run the application with hot reloading | |
| CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "7860", "--reload"] |