You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
2.6 KiB
160 lines
2.6 KiB
# Docker 快速启动指南
|
|
|
|
## 前置要求
|
|
|
|
- 安装 [Docker Desktop](https://www.docker.com/products/docker-desktop)
|
|
|
|
## 启动步骤
|
|
|
|
### 1. 启动数据库
|
|
|
|
Windows:
|
|
```bash
|
|
start-db-only.bat
|
|
```
|
|
|
|
或者手动启动:
|
|
```bash
|
|
docker run -d --name market_data_postgres \
|
|
-e POSTGRES_USER=postgres \
|
|
-e POSTGRES_PASSWORD=postgres123 \
|
|
-e POSTGRES_DB=marketdata \
|
|
-p 5432:5432 \
|
|
postgres:15-alpine
|
|
```
|
|
|
|
### 2. 初始化数据库表
|
|
|
|
```bash
|
|
python test_db.py
|
|
```
|
|
|
|
输出示例:
|
|
```
|
|
==================================================
|
|
数据库连接测试
|
|
==================================================
|
|
✅ 数据库连接成功
|
|
PostgreSQL 版本: PostgreSQL 15.5 on x86_64-pc-linux-musl...
|
|
|
|
正在初始化数据库表...
|
|
✅ 数据库表创建成功
|
|
|
|
已创建的表 (13 个):
|
|
- data_quality_checks
|
|
- data_source_config
|
|
- futures_klines_1d
|
|
- futures_klines_1m
|
|
- futures_symbols
|
|
- futures_trading_calendar
|
|
- stock_klines_1d
|
|
- stock_klines_1m
|
|
- stock_klines_5m
|
|
- stock_symbols
|
|
- stock_trading_calendar
|
|
...
|
|
|
|
==================================================
|
|
数据库初始化完成!
|
|
==================================================
|
|
```
|
|
|
|
### 3. 启动服务
|
|
|
|
```bash
|
|
python -m app.main
|
|
```
|
|
|
|
## 验证服务
|
|
|
|
打开浏览器访问:
|
|
- http://localhost:8080/admin - 管理后台
|
|
- http://localhost:8080/docs - API 文档
|
|
|
|
## 完整 Docker Compose 启动
|
|
|
|
一键启动所有服务(数据库 + Redis + 应用):
|
|
|
|
```bash
|
|
docker compose up -d
|
|
```
|
|
|
|
查看日志:
|
|
```bash
|
|
docker compose logs -f
|
|
```
|
|
|
|
停止服务:
|
|
```bash
|
|
docker compose down
|
|
```
|
|
|
|
## 故障排查
|
|
|
|
### 问题 1: 端口被占用
|
|
|
|
错误:
|
|
```
|
|
Bind for 0.0.0.0:5432 failed: port is already allocated
|
|
```
|
|
|
|
解决:
|
|
```bash
|
|
# 查看占用端口的进程
|
|
netstat -ano | findstr 5432
|
|
|
|
# 停止冲突的容器
|
|
docker stop <container_id>
|
|
```
|
|
|
|
### 问题 2: 数据库连接失败
|
|
|
|
检查步骤:
|
|
```bash
|
|
# 1. 检查容器是否运行
|
|
docker ps | findstr postgres
|
|
|
|
# 2. 查看容器日志
|
|
docker logs market_data_postgres
|
|
|
|
# 3. 测试连接
|
|
docker exec -it market_data_postgres pg_isready -U postgres
|
|
```
|
|
|
|
### 问题 3: 表未创建
|
|
|
|
手动初始化:
|
|
```bash
|
|
python test_db.py
|
|
```
|
|
|
|
## 数据持久化
|
|
|
|
数据存储在 Docker 卷中:
|
|
|
|
```bash
|
|
# 查看卷
|
|
docker volume ls
|
|
|
|
# 备份数据
|
|
docker exec -it market_data_postgres pg_dump -U postgres marketdata > backup.sql
|
|
|
|
# 恢复数据
|
|
docker exec -i market_data_postgres psql -U postgres marketdata < backup.sql
|
|
```
|
|
|
|
## 完全重置
|
|
|
|
删除所有数据并重新启动:
|
|
|
|
```bash
|
|
# 停止并删除容器
|
|
docker compose down -v
|
|
|
|
# 重新启动
|
|
docker compose up -d
|
|
|
|
# 初始化表
|
|
python test_db.py
|
|
```
|