# # 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"] # Stage 1: Build stage FROM python:3.12-slim as builder # Set environment variables ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 \ PATH="/opt/venv/bin:$PATH" # Install system dependencies (curl and ca-certificates for uv installer) RUN apt-get update && apt-get install -y --no-install-recommends \ curl \ ca-certificates \ && rm -rf /var/lib/apt/lists/* # Install uv using the official installer RUN curl -sSfL https://astral.sh/uv/install.sh | sh # Create a non-root user RUN useradd -m -u 1000 user # Set the working directory WORKDIR /app # Create a virtual environment RUN uv venv /opt/venv # Copy only the requirements file first to leverage Docker cache COPY --chown=user ./requirements.txt /app/requirements.txt # Install dependencies into the virtual environment using uv RUN uv 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 # Set environment variables ENV PATH="/opt/venv/bin:$PATH" \ PYTHONUNBUFFERED=1 # Set the working directory WORKDIR /app # Copy the virtual environment from the builder stage COPY --from=builder --chown=user /opt/venv /opt/venv # 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"]