# ============================================================
# 军事科技每日资讯推送系统 - Docker 镜像
# 基于 Python 3.11-slim，内置中文字体支持
# ============================================================
FROM python:3.11-slim

LABEL maintainer="military-digest"
LABEL description="军事科技每日资讯推送系统"

# ── 安装系统依赖（使用清华镜像加速）──────────────────────────
# tini: 信号转发（SIGTERM → 子进程安全退出）
# fontconfig + Noto Sans CJK: 网摘图片渲染中文字体
RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list.d/debian.sources \
    && apt-get update && apt-get install -y --no-install-recommends \
        tini \
        fontconfig \
        fonts-noto-cjk \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# ── 工作目录 ──────────────────────────────────────────────────
WORKDIR /app

# ── 复制依赖并安装（使用清华镜像加速）────────────────────────
COPY requirements.txt .
RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple \
    && pip install --no-cache-dir -r requirements.txt

# ── 复制项目代码 ──────────────────────────────────────────────
COPY . .

# ── 备份默认订阅源（用于 docker-compose 未挂载 resources 时 fallback）──
RUN mkdir -p /app/resources_default && cp /app/resources/rss_feeds.opml /app/resources_default/

# ── 创建运行时目录 ────────────────────────────────────────────
RUN mkdir -p /app/data/fetch /app/data/hotalert /app/data/cluster /app/data/digest /app/data/images /app/data/cache /app/data/output /app/logs /app/cache

# ── 入口 ──────────────────────────────────────────────────────
# tini 确保 docker stop 信号正确传递给子进程
ENTRYPOINT ["/usr/bin/tini", "--"]
CMD ["python", "start.py"]

# ── 健康检查 ──────────────────────────────────────────────────
HEALTHCHECK --interval=60s --timeout=10s --start-period=120s --retries=3 \
    CMD python -c "import os; exit(0 if os.path.exists('/app/data') else 1)"