FROM python:3.12-slim as builder # Install build dependencies RUN apt-get update && apt-get install -y \ build-essential \ && rm -rf /var/lib/apt/lists/* # Create and activate virtual environment RUN python -m venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" WORKDIR /app # Copy and install requirements COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # Final stage FROM python:3.12-slim # Install runtime dependencies and security updates RUN apt-get update && apt-get upgrade -y && \ apt-get install -y \ curl \ tini \ && rm -rf /var/lib/apt/lists/* # Copy virtual environment from builder COPY --from=builder /opt/venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" WORKDIR /app # Create model directory and copy model files RUN mkdir -p /app/model COPY config_sentence_transformers.json config.json model.safetensors modules.json sentence_bert_config.json special_tokens_map.json tokenizer_config.json tokenizer.json unigram.json /app/model/ RUN mkdir -p /app/model/1_Pooling COPY 1_Pooling/config.json /app/model/1_Pooling/config.json # Copy application code COPY app.py . COPY requirements.txt . # Verify model files exist RUN python -c "import os; \ assert os.path.exists('/app/model/model.safetensors'), 'Model file not found'; \ assert os.path.exists('/app/model/config.json'), 'Config file not found'; \ print('Model files verified successfully')" # Runtime configuration for Cloud Run ENV PORT=8080 ENV WORKERS=2 ENV WORKER_TIMEOUT=300 ENV PYTHONUNBUFFERED=1 ENV PYTORCH_NUM_THREADS=4 # Set Python environment ENV PYTHONDONTWRITEBYTECODE=1 ENV PYTHONPATH=/app # Non-root user for security RUN useradd -r -u 1000 appuser && \ chown -R appuser:appuser /app USER appuser # Health check HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \ CMD curl -f http://localhost:${PORT}/health || exit 1 # Expose the port EXPOSE ${PORT} # Use tini as init system ENTRYPOINT ["/usr/bin/tini", "--"] # Use uvicorn with multiple workers for better concurrency CMD exec uvicorn \ app:app \ --host 0.0.0.0 \ --port ${PORT} \ --workers ${WORKERS} \ --timeout-keep-alive 65 \ --log-level info \ --proxy-headers \ --forwarded-allow-ips '*' \ --no-access-log