# 宇之然内容创作平台 - 后端Docker镜像

FROM python:3.10-slim as builder

WORKDIR /app

# 安装系统依赖
RUN apt-get update && apt-get install -y \
    build-essential \
    libpq-dev \
    && rm -rf /var/lib/apt/lists/*

# 复制requirements文件
COPY requirements.txt .

# 安装Python依赖（带缓存优化）
RUN pip install --user --no-cache-dir -r requirements.txt

# 生产阶段
FROM python:3.10-slim

WORKDIR /app

# 从builder阶段复制已安装的依赖
COPY --from=builder /root/.local /root/.local
COPY . .

# 确保PATH包含用户本地bin目录
ENV PATH=/root/.local/bin:$PATH

# 创建非root用户
RUN groupadd -r appuser && useradd -r -g appuser appuser
RUN chown -R appuser:appuser /app

USER appuser

# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
    CMD curl -f http://localhost:8001/health || exit 1

EXPOSE 8001

# 运行应用
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8001"]

# 标签信息
LABEL maintainer="宇之然团队"
LABEL version="1.0.0"
LABEL description="企业级内容创作管理系统"