# 基于官方的 Ubuntu 镜像
FROM debian:stable-slim AS ubuntu-base

# 设置环境变量，避免在安装过程中提示交互式输入
ENV DEBIAN_FRONTEND=noninteractive

# 安装必要的依赖
RUN apt-get update && apt-get install -y \
    wget \
    bzip2 \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

FROM ubuntu-base AS ubuntu-anaconda
# 下载并安装 Anaconda
RUN wget --quiet https://repo.anaconda.com/archive/Anaconda3-2024.10-1-Linux-x86_64.sh -O ~/anaconda.sh && \
    /bin/bash ~/anaconda.sh -b -p /opt/conda && \
    rm ~/anaconda.sh && \
    echo "export PATH=/opt/conda/bin:$PATH" >> ~/.bashrc && \
    /opt/conda/bin/conda clean --all -y

ENV PATH="/opt/conda/bin:$PATH"
#设置清华源

FROM ubuntu-anaconda AS ubuntu-anaconda-config

RUN conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ && \
    conda config --set show_channel_urls yes
COPY environment.yml /app/environment.yml
RUN apt-get update && \
    apt-get install -y python3-dev libpq-dev
RUN apt-get install -y build-essential 

RUN conda env create -f /app/environment.yml
RUN apt-get update && apt-get install -y --no-install-recommends curl && apt-get clean && rm -rf /var/lib/apt/lists/*
# 配置 Conda 环境变量

# 创建名为 server 的 Python 虚拟环境
#RUN conda create -y -n server python=3.9 && \
#    conda clean --all -y

# 将 requirements.txt 文件复制到镜像中
#COPY requirements.txt /app/requirements.txt
# 激活 server 环境并使用 pip 安装依赖库
# RUN conda run -n server pip install --no-cache-dir -r /app/requirements.txt

# 将 FastAPI 应用代码复制到镜像中
FROM ubuntu-anaconda-config AS ubuntu-anaconda-app
COPY . /app

# 设置环境变量（这些环境变量可以在容器启动时覆盖）
ENV POSTGRESQL_HOST="localhost"
ENV POSTGRESQL_DATABASE="kg"
ENV POSTGRESQL_USER="postgres"
#ENV POSTGRESQL_PASSWORD="difyai123456"
ENV DEEPSEEK_API_URL="https://api.siliconflow.cn/v1/chat/completions"
#ENV DEEPSEEK_API_KEY="sk-vecnpjmtmelcefdbtbbpqvzcegopxrherbnbjhscugbpxuif"
ENV JOB_PATH="/app/agent/jobs"
ENV JOB_SCRIPT_PATH="/app/agent/job_script"
# 创建日志目录
RUN mkdir -p /app/logs

# 创建一个启动脚本
RUN echo '#!/bin/bash' > /start.sh && \
    echo 'source /opt/conda/etc/profile.d/conda.sh' >> /start.sh && \
    echo 'conda activate kgbuilder' >> /start.sh && \
    echo 'cd /app' >> /start.sh && \
    echo 'python agent/main.py' >> /start.sh && \
    chmod +x /start.sh

# 设置默认命令
CMD ["/bin/bash", "/start.sh"]
