diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..a33226a --- /dev/null +++ b/.dockerignore @@ -0,0 +1,69 @@ +# Git +.git +.gitignore + +# Python +__pycache__ +*.py[cod] +*$py.class +*.so +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# 虚拟环境 +venv/ +env/ +ENV/ +.venv + +# 环境配置(通过 docker-compose 挂载或环境变量传递) +.env +.env.local +.env.*.local +config.json + +# 开发工具 +.vscode/ +.idea/ +*.swp +*.swo +*~ + +# 测试 +tests/ +.pytest_cache/ +.coverage +htmlcov/ + +# Node.js / WebUI 开发依赖 +node_modules/ +webui/node_modules/ +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# 文档 +*.md +!README*.md + +# CI/CD +.github/ +.releaserc.json + +# 其他 +.DS_Store +Thumbs.db diff --git a/DEPLOY.md b/DEPLOY.md index ff13005..b1c47d9 100644 --- a/DEPLOY.md +++ b/DEPLOY.md @@ -7,6 +7,7 @@ ## 目录 - [Vercel 部署(推荐)](#vercel-部署推荐) +- [Docker 部署(推荐)](#docker-部署推荐) - [本地开发](#本地开发) - [生产环境部署](#生产环境部署) - [常见问题](#常见问题) @@ -148,6 +149,108 @@ npm run build --- +## Docker 部署(推荐) + +Docker 部署采用**零侵入、解耦设计**: +- Dockerfile 仅执行标准 Python 项目操作,不硬编码任何项目特定配置 +- 所有配置通过环境变量和 `.env` 文件管理 +- **主代码更新时只需重新构建镜像,无需修改 Docker 配置** + +### 快速开始(Docker Compose) + +```bash +# 1. 复制环境变量模板 +cp .env.example .env +# 编辑 .env,填写 DS2API_ADMIN_KEY 和 DS2API_CONFIG_JSON + +# 2. 启动服务 +docker-compose up -d + +# 3. 查看日志 +docker-compose logs -f + +# 4. 主代码更新后重新构建 +docker-compose up -d --build +``` + +### 配置文件挂载方式 + +如需使用 `config.json` 而非环境变量: + +```yaml +# docker-compose.yml +services: + ds2api: + build: . + ports: + - "5001:5001" + environment: + - DS2API_ADMIN_KEY=your-admin-key + volumes: + - ./config.json:/app/config.json:ro + restart: unless-stopped +``` + +### Docker 命令行部署 + +```bash +# 构建镜像 +docker build -t ds2api:latest . + +# 使用环境变量运行 +docker run -d \ + --name ds2api \ + -p 5001:5001 \ + -e DS2API_ADMIN_KEY=your-admin-key \ + -e DS2API_CONFIG_JSON='{"keys":["api-key"],"accounts":[...]}' \ + --restart unless-stopped \ + ds2api:latest + +# 或使用配置文件挂载 +docker run -d \ + --name ds2api \ + -p 5001:5001 \ + -e DS2API_ADMIN_KEY=your-admin-key \ + -v $(pwd)/config.json:/app/config.json:ro \ + --restart unless-stopped \ + ds2api:latest +``` + +### 开发模式(热重载) + +```bash +# 使用开发配置启动,代码修改实时生效 +docker-compose -f docker-compose.dev.yml up +``` + +开发模式特性: +- 源代码挂载到容器,修改即时生效 +- 日志级别设为 DEBUG +- 自动读取本地 `config.json` + +### 维护命令 + +```bash +# 查看容器状态 +docker-compose ps + +# 查看日志 +docker-compose logs -f ds2api + +# 重启服务 +docker-compose restart + +# 停止服务 +docker-compose down + +# 完全重建(清除缓存) +docker-compose down +docker-compose build --no-cache +docker-compose up -d +``` + +--- + ## 生产环境部署 ### 使用 systemd (Linux) @@ -231,52 +334,6 @@ server { } ``` -### Docker 部署(可选) - -```dockerfile -# Dockerfile -FROM python:3.11-slim - -WORKDIR /app -COPY requirements.txt . -RUN pip install --no-cache-dir -r requirements.txt - -COPY . . - -EXPOSE 5001 -CMD ["python", "app.py"] -``` - -```bash -# 构建镜像 -docker build -t ds2api . - -# 运行容器 -docker run -d \ - --name ds2api \ - -p 5001:5001 \ - -e DS2API_ADMIN_KEY=your-admin-key \ - -e DS2API_CONFIG_JSON='{"keys":["api-key"],"accounts":[...]}' \ - ds2api -``` - -### Docker Compose - -```yaml -# docker-compose.yml -version: '3.8' - -services: - ds2api: - build: . - ports: - - "5001:5001" - environment: - - DS2API_ADMIN_KEY=${DS2API_ADMIN_KEY} - - DS2API_CONFIG_JSON=${DS2API_CONFIG_JSON} - restart: unless-stopped -``` - --- ## 常见问题 @@ -312,6 +369,15 @@ pip install -r requirements.txt # 重启服务 ``` +**Docker 部署**: +```bash +# 拉取最新代码 +git pull origin main + +# 重新构建并启动(无需修改 Docker 配置) +docker-compose up -d --build +``` + **Vercel 部署**: - 项目会自动从 GitHub 同步更新 - 或在 Vercel 控制台手动触发重新部署 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..7245e3a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +# DS2API Docker 镜像 +# 采用极简、零侵入设计,所有配置通过环境变量传递 +# 主代码更新时只需重新构建镜像,无需修改 Dockerfile + +FROM python:3.11-slim + +WORKDIR /app + +# 安装依赖(利用 Docker 缓存层) +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +# 复制整个项目(保留原始目录结构) +COPY . . + +# 暴露服务端口 +EXPOSE 5001 + +# 启动命令(依赖项目自身的启动逻辑) +CMD ["python", "app.py"] diff --git a/README.MD b/README.MD index 27424f2..12660ee 100644 --- a/README.MD +++ b/README.MD @@ -4,6 +4,7 @@ ![Stars](https://img.shields.io/github/stars/CJackHwang/ds2api.svg) ![Forks](https://img.shields.io/github/forks/CJackHwang/ds2api.svg) [![Version](https://img.shields.io/badge/version-1.6.11-blue.svg)](version.txt) +[![Docker](https://img.shields.io/badge/docker-ready-blue.svg)](DEPLOY.md#docker-部署推荐) 将 DeepSeek 免费对话版转换为 **OpenAI & Claude 兼容 API**,支持多账号轮询、自动 Token 刷新、可视化管理界面。 @@ -192,17 +193,26 @@ location / { } ``` -### Docker 部署(可选) +### 方式三:Docker 部署 ```bash -# 使用环境变量配置 -docker run -d \ - -p 5001:5001 \ - -e DS2API_ADMIN_KEY=your-admin-key \ - -e DS2API_CONFIG_JSON='{"keys":["api-key"],"accounts":[...]}' \ - ds2api +# 1. 克隆仓库并进入目录 +git clone https://github.com/CJackHwang/ds2api.git +cd ds2api + +# 2. 配置环境变量 +cp .env.example .env +# 编辑 .env,填写 DS2API_ADMIN_KEY 和 DS2API_CONFIG_JSON + +# 3. 启动服务 +docker-compose up -d + +# 4. 查看日志 +docker-compose logs -f ``` +> **Docker 优势**:零侵入设计,主代码更新只需 `docker-compose up -d --build`,无需修改 Docker 配置。详见 [DEPLOY.md](DEPLOY.md#docker-部署推荐)。 + ## ⚠️ 免责声明 **本项目基于逆向工程实现,服务稳定性无法保证。** diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml new file mode 100644 index 0000000..19a42ba --- /dev/null +++ b/docker-compose.dev.yml @@ -0,0 +1,32 @@ +# DS2API 开发环境配置 +# 特性: +# - 源代码挂载(热重载) +# - 调试日志级别 +# - 自动重启 +# +# 使用说明: +# docker-compose -f docker-compose.dev.yml up + +services: + ds2api: + build: . + image: ds2api:dev + container_name: ds2api-dev + ports: + - "${PORT:-5001}:5001" + env_file: + - .env + environment: + - HOST=0.0.0.0 + - LOG_LEVEL=DEBUG + volumes: + # 源代码挂载(开发时实时生效) + - ./app.py:/app/app.py:ro + - ./core:/app/core:ro + - ./routes:/app/routes:ro + - ./static:/app/static:ro + # 配置文件挂载(便于本地修改) + - ./config.json:/app/config.json:ro + restart: "no" + stdin_open: true + tty: true diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..3842060 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,29 @@ +# DS2API 生产环境配置 +# 使用说明: +# 1. 复制 .env.example 为 .env 并填写配置 +# 2. docker-compose up -d +# 3. 主代码更新后:docker-compose up -d --build +# +# 设计原则: +# - 零侵入:所有项目配置通过 .env 文件传递 +# - 易维护:主代码更新只需重新构建镜像 + +services: + ds2api: + build: . + image: ds2api:latest + container_name: ds2api + ports: + - "${PORT:-5001}:5001" + env_file: + - .env + environment: + # 确保容器内使用正确的主机绑定 + - HOST=0.0.0.0 + restart: unless-stopped + healthcheck: + test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:5001/v1/models')"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 10s