feat: 添加Docker支持,包含Dockerfile、docker-compose.yml、.dockerignore和部署文档

This commit is contained in:
claw_bot 2026-03-09 08:28:21 +00:00
parent 4ac8a5f063
commit 69468cea00
4 changed files with 177 additions and 0 deletions

30
.dockerignore Normal file
View File

@ -0,0 +1,30 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
*.md
.git
.vs
.vscode
bin
obj
logs
*.user

72
DEPLOY.md Normal file
View File

@ -0,0 +1,72 @@
# Docker 部署说明
## 快速启动
### 1. 直接使用 docker-compose 启动
```bash
# 启动服务(需要提前配置好外部 MySQL 连接)
docker-compose up -d
# 查看日志
docker-compose logs -f api
# 停止服务
docker-compose down
```
### 2. 单独构建镜像
```bash
# 构建镜像
docker build -t assetmanager-api:latest .
# 运行容器(需要自行配置 MySQL 连接)
docker run -d -p 7040:8080 \
-e ConnectionStrings__Default="server=你的MySQL地址;Database=assetmanager;Uid=root;Pwd=你的密码;CharSet=utf8mb4;" \
-e Jwt__SecretKey="你的自定义密钥" \
assetmanager-api:latest
```
## 环境变量说明
| 环境变量 | 说明 | 默认值 |
|---------|------|--------|
| `ASPNETCORE_ENVIRONMENT` | 运行环境 | `Production` |
| `ConnectionStrings__Default` | MySQL 连接字符串 | - |
| `Jwt__SecretKey` | JWT 签名密钥 | 开发环境默认值 |
| `Jwt__Issuer` | JWT 签发者 | `AssetManager` |
| `Jwt__Audience` | JWT 受众 | `AssetManager` |
| `Wechat__AppId` | 微信小程序 AppId | 测试默认值 |
| `Wechat__AppSecret` | 微信小程序 AppSecret | 测试默认值 |
| `Tiingo__ApiKey` | Tiingo 股票数据 API Key | 测试默认值 |
| `TZ` | 时区 | `Asia/Shanghai` |
## 端口说明
| 端口 | 服务 | 说明 |
|------|------|------|
| `7040` | 后端 API | HTTP 接口 |
## 数据持久化
- 应用日志:存储在 `logs` 卷中
## 生产环境注意事项
1. **必须修改默认密码和密钥**
- MySQL 连接密码
- JWT 签名密钥 (`Jwt__SecretKey`)
- 微信 AppId/AppSecret
- Tiingo API Key
2. 建议使用反向代理Nginx/Caddy提供 HTTPS 访问
## 常见问题
### 启动后 API 访问 500
检查 MySQL 连接是否正常,确认连接字符串配置正确。
### 微信登录失败
检查 `Wechat__AppId``Wechat__AppSecret` 是否配置正确。
### 市场数据获取失败
检查 `Tiingo__ApiKey` 是否有效,网络是否能访问 Tiingo API。

45
Dockerfile Normal file
View File

@ -0,0 +1,45 @@
# 使用 .NET 8 SDK 作为构建镜像
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
# 复制解决方案文件和项目文件
COPY ["AssetManager.sln", "."]
COPY ["AssetManager.API/AssetManager.API.csproj", "AssetManager.API/"]
COPY ["AssetManager.Services/AssetManager.Services.csproj", "AssetManager.Services/"]
COPY ["AssetManager.Infrastructure/AssetManager.Infrastructure.csproj", "AssetManager.Infrastructure/"]
COPY ["AssetManager.Models/AssetManager.Models.csproj", "AssetManager.Models/"]
COPY ["AssetManager.Data/AssetManager.Data.csproj", "AssetManager.Data/"]
# 还原 NuGet 包
RUN dotnet restore "AssetManager.API/AssetManager.API.csproj"
# 复制所有文件并构建
COPY . .
WORKDIR "/src/AssetManager.API"
RUN dotnet build "AssetManager.API.csproj" -c Release -o /app/build
# 发布应用
FROM build AS publish
RUN dotnet publish "AssetManager.API.csproj" -c Release -o /app/publish /p:UseAppHost=false
# 构建最终运行时镜像
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
# 设置时区为 Asia/Shanghai
ENV TZ=Asia/Shanghai
RUN apt-get update && apt-get install -y tzdata && ln -sf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# 从发布阶段复制文件
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
# 配置环境变量(可以在运行时覆盖)
ENV ASPNETCORE_ENVIRONMENT=Production
ENV ASPNETCORE_URLS=http://+:8080
# 启动应用
ENTRYPOINT ["dotnet", "AssetManager.API.dll"]

30
docker-compose.yml Normal file
View File

@ -0,0 +1,30 @@
version: '3.8'
services:
# 后端 API 服务
api:
build: .
container_name: assetmanager-api
restart: always
ports:
- "7040:8080"
environment:
- ASPNETCORE_ENVIRONMENT=Production
# 外部 MySQL 连接配置(自行修改)
- ConnectionStrings__Default=server=你的MySQL地址;Database=assetmanager;Uid=你的用户名;Pwd=你的密码;CharSet=utf8mb4;Allow User Variables=true;
# JWT 配置
- Jwt__SecretKey=your-strong-secret-key-here-change-in-production
- Jwt__Issuer=AssetManager
- Jwt__Audience=AssetManager
# 微信配置
- Wechat__AppId=wx245f0f3ebcfcf5a7
- Wechat__AppSecret=809c740129bc8b434177ce12ef292dd0
# Tiingo API 配置
- Tiingo__ApiKey=bd00fee76d3012b047473078904001b33322cb46
# 时区
- TZ=Asia/Shanghai
volumes:
- logs:/app/logs
volumes:
logs: