FROM python:3.10-slim

WORKDIR /app

# 设置 pip 源为清华镜像（持久配置 + 环境变量）
RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
ENV PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple
ENV PIP_TRUSTED_HOST=pypi.tuna.tsinghua.edu.cn

# 安装依赖（使用 --progress=off 避免线程问题）
RUN pip install --no-cache-dir --progress=off fastapi[all] opensearch-py uvicorn httpx langchain-core langchain-community redis

# 仅复制应用代码（.dockerignore 已排除 .env、.git、__pycache__ 等，避免敏感数据进镜像）
COPY . /app/

# 使用非 root 用户运行，降低风险（python 镜像默认 root）
RUN adduser --disabled-password --gecos "" appuser && chown -R appuser:appuser /app
USER appuser

EXPOSE 8000
CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "8000"]

