From 792d761659aa420274528a8cd4ef5fc2f2b67862 Mon Sep 17 00:00:00 2001 From: Darren Date: Wed, 6 Aug 2025 10:15:37 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=80=82=E9=85=8D=20Docker=20=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=B9=B6=E5=88=9D=E5=A7=8B=E5=8C=96=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改 database.py 中的数据库连接参数,增加 timeout 设置,适应 Docker 环境 - 在 start.py 中添加数据库初始化函数,并在程序启动时执行 - 修改服务启动地址为 0.0.0.0,使得服务可在 Docker 容器中访问 - 移除不必要的代码重复,优化启动流程 --- .dockerignore | 52 +++++++++++++++++++++++++++++++++++++++++++++ Dockerfile | 53 ++++++++++++++++++++++++++++++++++++++++++++++ database.py | 8 ++++++- docker-compose.yml | 16 ++++++++++++++ start.py | 31 +++++++++++++++++++++++---- 5 files changed, 155 insertions(+), 5 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..1b7e78a --- /dev/null +++ b/.dockerignore @@ -0,0 +1,52 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# Logs +logs/ +*.log + +# Environment files +.env +.venv/ +venv/ +ENV/ + +# IDE +.idea/ +.vscode/ +*.swp +*.swo + +# OS generated files +.DS_Store +Thumbs.db + +# Git +.git/ +.gitignore + +# Docker +.dockerignore + +# Readme file (if not needed in container) +README.md \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..07a9ce6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,53 @@ +# 构建阶段 +FROM python:3.11 as builder + +WORKDIR /app + +# 安装构建依赖 +RUN apt-get update && apt-get install -y \ + gcc \ + python3-dev \ + && rm -rf /var/lib/apt/lists/* + +# 复制依赖文件 +COPY requirements.txt . + +# 安装依赖到系统路径 +RUN pip install -r requirements.txt + +# 生产阶段 +FROM python:3.11-slim + +WORKDIR /app + +# 安装运行时依赖 +RUN apt-get update && apt-get install -y \ + gcc \ + python3-dev \ + && rm -rf /var/lib/apt/lists/* + +# 复制已安装的Python包 +COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages +COPY --from=builder /usr/local/bin /usr/local/bin + +# 设置PATH以包含新复制的bin目录 +ENV PATH=/usr/local/bin:$PATH + +# 复制项目文件 +COPY . . + +# 创建必要的目录 +RUN mkdir -p logs + +# 暴露端口 +EXPOSE 8000 + +# 设置非root用户 +RUN useradd --create-home --shell /bin/bash app && chown -R app:app /app +USER app + +# 初始化数据库 +RUN python -c "from database import engine, Base; Base.metadata.create_all(bind=engine); print('✅ 数据库初始化完成')" + +# 启动命令 +CMD ["python", "start.py", "--skip-install"] \ No newline at end of file diff --git a/database.py b/database.py index f630c1c..ee36879 100644 --- a/database.py +++ b/database.py @@ -5,10 +5,16 @@ import json import hashlib import secrets +import os +# 确保数据库文件路径在Docker容器中可写 DATABASE_URL = "sqlite:///./api_records.db" -engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False}) +# 添加连接参数以适应Docker环境 +engine = create_engine(DATABASE_URL, connect_args={ + "check_same_thread": False, + "timeout": 30 +}) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) Base = declarative_base() diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..711141a --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,16 @@ +version: '3.8' + +services: + redwolf: + build: . + container_name: redwolf-app + ports: + - "8000:8000" + volumes: + - redwolf_data:/app + environment: + - DEBUG_MODE=false + command: python start.py --skip-install + +volumes: + redwolf_data: \ No newline at end of file diff --git a/start.py b/start.py index eea0300..f71e43f 100644 --- a/start.py +++ b/start.py @@ -8,6 +8,23 @@ import os import argparse +# 初始化数据库 +def init_database(): + """初始化数据库表""" + try: + from database import engine, Base + from database import ( + APIRecord, PlatformConfig, ModelConfig, RoutingConfig, RoutingScene, + SystemConfig, ClaudeCodeServer, UserAuth, LoginSession + ) + + print("正在初始化数据库...") + # 创建所有表 + Base.metadata.create_all(bind=engine) + print("✅ 数据库初始化完成") + except Exception as e: + print(f"❌ 数据库初始化失败: {e}") + def install_dependencies(): """安装依赖包""" print("正在安装Python依赖包...") @@ -21,7 +38,7 @@ def install_dependencies(): def start_server(debug=False): """启动服务器""" print("正在启动API Hook监控系统...") - print("服务地址: http://127.0.0.1:8000") + print("服务地址: http://0.0.0.0:8000") if debug: print("🐛 DEBUG模式已启用 - 将显示详细调试信息") os.environ['DEBUG_MODE'] = 'true' @@ -32,7 +49,8 @@ def start_server(debug=False): print("-" * 50) try: - subprocess.run([sys.executable, "-m", "uvicorn", "main:app", "--host", "127.0.0.1", "--port", "8000", "--reload"]) + # 修改host为0.0.0.0以在Docker容器中可访问 + subprocess.run([sys.executable, "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]) except KeyboardInterrupt: print("\n服务已停止") @@ -50,7 +68,12 @@ def start_server(debug=False): print("❌ 请在项目根目录运行此脚本") sys.exit(1) - # 安装依赖并启动服务 + # 初始化数据库 + init_database() + + # 如果没有跳过安装,则安装依赖 if not args.skip_install: install_dependencies() - start_server(debug=args.debug) \ No newline at end of file + + # 启动服务器 + start_server(args.debug) \ No newline at end of file