Files

96 lines
3.0 KiB
Docker
Raw Permalink Normal View History

2025-10-30 11:14:43 +08:00
# 多阶段构建 Dockerfile for AI Story Creator
2026-01-22 11:03:28 +08:00
# 支持多架构构建: linux/amd64, linux/arm64
# 构建参数
ARG USE_CN_MIRROR=false
2026-05-18 14:31:54 +08:00
# 阶段1: 最终镜像
2025-10-30 11:14:43 +08:00
FROM python:3.11-slim
2026-01-22 11:03:28 +08:00
ARG USE_CN_MIRROR
ARG TARGETPLATFORM
ARG TARGETARCH
2025-10-30 11:14:43 +08:00
# 设置工作目录
WORKDIR /app
2026-01-22 11:03:28 +08:00
# 根据参数决定是否使用国内镜像源
RUN if [ "$USE_CN_MIRROR" = "true" ]; then \
2026-05-18 14:31:54 +08:00
sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list.d/debian.sources && \
sed -i 's/security.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list.d/debian.sources; \
2026-01-22 11:03:28 +08:00
fi
2025-10-30 11:14:43 +08:00
# 安装系统依赖(添加数据库工具)
2025-10-30 11:14:43 +08:00
RUN apt-get update && apt-get install -y \
gcc \
postgresql-client \
netcat-traditional \
2025-10-30 11:14:43 +08:00
&& rm -rf /var/lib/apt/lists/*
# 复制后端依赖文件
COPY backend/requirements.txt ./
# 安装 Python 依赖
2026-01-22 11:03:28 +08:00
RUN if [ "$USE_CN_MIRROR" = "true" ]; then \
pip install --no-cache-dir torch==2.8.0 --index-url https://mirrors.aliyun.com/pypi/simple/ --extra-index-url https://download.pytorch.org/whl/cpu && \
2026-01-22 11:03:28 +08:00
pip install --no-cache-dir -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/; \
else \
pip install --no-cache-dir torch==2.8.0 --index-url https://download.pytorch.org/whl/cpu && \
2026-01-22 11:03:28 +08:00
pip install --no-cache-dir -r requirements.txt; \
fi
# 创建embedding目录
RUN mkdir -p /app/embedding
# 设置 Sentence-Transformers 缓存目录
ENV SENTENCE_TRANSFORMERS_HOME=/app/embedding
2025-10-30 11:14:43 +08:00
2026-01-22 11:03:28 +08:00
# 下载 embedding 模型(从 HuggingFace
RUN python -c "\
from sentence_transformers import SentenceTransformer; \
import os; \
os.environ['SENTENCE_TRANSFORMERS_HOME'] = '/app/embedding'; \
print('Downloading sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2...'); \
model = SentenceTransformer('sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2'); \
print('Model downloaded successfully!'); \
"
2026-05-18 14:31:54 +08:00
# 复制后端代码
2025-10-30 11:14:43 +08:00
COPY backend/ ./
2026-05-18 14:31:54 +08:00
# 复制宿主机预构建的静态文件
# 这样可以避免 Docker 内部构建前端时的各种环境问题
COPY backend/static/ ./static
2025-10-30 11:14:43 +08:00
# 复制 Alembic 迁移配置和脚本(PostgreSQL
COPY backend/alembic-postgres.ini ./alembic.ini
COPY backend/alembic/postgres ./alembic
COPY backend/scripts/entrypoint.sh /app/entrypoint.sh
COPY backend/scripts/migrate.py ./scripts/migrate.py
2026-05-18 14:31:54 +08:00
# 修复 Windows CRLF 换行导致的启动失败,并赋予执行权限
RUN sed -i 's/\r$//' /app/entrypoint.sh && chmod +x /app/entrypoint.sh
2025-10-30 11:14:43 +08:00
# 创建必要的目录
RUN mkdir -p /app/data /app/logs
2025-11-04 17:27:39 +08:00
2025-10-30 11:14:43 +08:00
# 暴露端口
EXPOSE 8000
# 设置环境变量
ENV PYTHONUNBUFFERED=1
ENV APP_HOST=0.0.0.0
ENV APP_PORT=8000
2026-05-18 14:31:54 +08:00
# 设置运行时为离线模式
2025-11-04 17:27:39 +08:00
ENV TRANSFORMERS_OFFLINE=1
ENV HF_DATASETS_OFFLINE=1
ENV HF_HUB_OFFLINE=1
2025-10-30 11:14:43 +08:00
# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')" || exit 1
2026-05-18 14:31:54 +08:00
# 使用 entrypoint 脚本启动
ENTRYPOINT ["/app/entrypoint.sh"]