feat: 添加 Docker 一键部署支持,包括后端和前端 Dockerfile、docker-compose 配置及 Nginx 配置 (#112)

* feat: 添加 Docker 支持,包括后端和前端 Dockerfile、docker-compose 配置及 Nginx 配置

* feat: 添加 .dockerignore 文件,更新 Dockerfile 和 docker-compose 配置以支持数据持久化保存
This commit is contained in:
MarkYangkp
2026-02-01 12:55:39 +08:00
committed by GitHub
parent 666310e7b4
commit c981aff863
7 changed files with 222 additions and 4 deletions

40
.dockerignore Normal file
View File

@@ -0,0 +1,40 @@
# 忽略不需要的文件
.git
.gitignore
node_modules
__pycache__
*.pyc
*.pyo
*.pyd
.Python
env/
venv/
.env
.env.local
.DS_Store
Thumbs.db
# 开发文件
README.md
ROADMAP.md
CONTRIBUTING.md
.github/
.vscode/
.idea/
*.md
# 构建产物
web/dist/
web/node_modules/
dist/
build/
# 日志
*.log
logs/
# 测试
tests/
pytest_cache/
.coverage
htmlcov/

14
.gitignore vendored
View File

@@ -152,4 +152,16 @@ local_config.yml
台本/
笔记/
tmp*/
tmp*/
#############################
# Docker
#############################
# Docker data directories
saves/
logs/
# Docker environment files
.env

32
Dockerfile.backend Normal file
View File

@@ -0,0 +1,32 @@
# 后端 Dockerfile
FROM python:3.12-slim
# 设置工作目录
WORKDIR /app
# 安装系统依赖
RUN apt-get update && apt-get install -y \
gcc \
g++ \
curl \
&& rm -rf /var/lib/apt/lists/*
# 复制依赖文件
COPY requirements.txt .
# 安装 Python 依赖 清华园镜像源
RUN pip install --no-cache-dir -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
# 复制项目代码
COPY src/ ./src/
COPY static/ ./static/
COPY assets/ ./assets/
# 创建必要的目录
RUN mkdir -p /app/assets/saves /app/logs
# 暴露端口
EXPOSE 8002
# 启动命令
CMD ["uvicorn", "src.server.main:app", "--host", "0.0.0.0", "--port", "8002"]

30
Dockerfile.frontend Normal file
View File

@@ -0,0 +1,30 @@
# 前端 Dockerfile (多阶段构建)
FROM node:22-alpine AS builder
# 设置工作目录
WORKDIR /app
# 复制 package.json 和 package-lock.json
COPY web/package.json web/package-lock.json* ./
# 安装依赖
RUN npm ci
# 复制前端源代码
COPY web/ .
# 构建前端
RUN npm run build
# 生产阶段 - 使用 Nginx
FROM nginx:alpine
# 复制构建产物
COPY --from=builder /app/dist /usr/share/nginx/html
# 复制 Nginx 配置
COPY nginx.conf /etc/nginx/conf.d/default.conf
# 暴露端口
EXPOSE 80
# Nginx 会自动启动

View File

@@ -108,9 +108,32 @@
## 🚀 使用方法
### ⚙️ 运行步骤
#### 方式一Docker Compose 一键部署(推荐)
如果你已经安装了 Docker这是最简单的方式
# 1. 克隆项目
```bash
git clone https://github.com/AI-Cultivation/cultivation-world-simulator.git
cd cultivation-world-simulator
```
# 2. 启动服务(自动构建并运行)
```bash
docker-compose up -d --build
```
# 3. 访问应用
前端:`http://localhost:8123`
后端 API`http://localhost:8002`
```
#### 方式二:手动安装运行
1. 克隆项目到本地:
```bash
git clone https://github.com/your-username/cultivation-world-simulator.git
git clone https://github.com/AI-Cultivation/cultivation-world-simulator.git
cd cultivation-world-simulator
```
@@ -118,7 +141,7 @@
```bash
# 后端依赖
pip install -r requirements.txt
# 前端依赖 (需Node.js环境)
cd web && npm install
```
@@ -126,7 +149,7 @@
3. 配置LLM
**推荐方式:在前端直接配置(支持快速填充预设)**
<img src="assets/zh-CN/llm_config.png" alt="前端LLM配置" width="100%">
也可以在 `static/local_config.yml` 中手动配置OpenAI兼容格式

40
docker-compose.yml Normal file
View File

@@ -0,0 +1,40 @@
version: '3.8'
services:
# 后端服务
backend:
build:
context: .
dockerfile: Dockerfile.backend
container_name: cultivation-backend
ports:
- "8002:8002"
environment:
- PYTHONUNBUFFERED=1
restart: unless-stopped
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8002/api/state"]
interval: 30s
timeout: 10s
retries: 3
start_period: 30s
volumes:
- ./assets/saves:/app/assets/saves
- ./logs:/app/logs
# 前端服务
frontend:
build:
context: .
dockerfile: Dockerfile.frontend
container_name: cultivation-frontend
ports:
- "8123:80"
depends_on:
- backend
restart: unless-stopped
healthcheck:
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:80/"]
interval: 30s
timeout: 10s
retries: 3

41
nginx.conf Normal file
View File

@@ -0,0 +1,41 @@
server {
listen 80;
server_name localhost;
# 前端静态文件
location / {
root /usr/share/nginx/html;
index index.html;
try_files $uri $uri/ /index.html;
}
# 代理 API 请求到后端
location /api {
proxy_pass http://backend:8002;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# 代理 WebSocket 连接
location /ws {
proxy_pass http://backend:8002;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# 代理游戏资源
location /assets {
proxy_pass http://backend:8002;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}