update
This commit is contained in:
42
backend/.dockerignore
Normal file
42
backend/.dockerignore
Normal file
@@ -0,0 +1,42 @@
|
||||
# 依赖目录
|
||||
node_modules
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
|
||||
# 构建输出
|
||||
dist
|
||||
build
|
||||
|
||||
# 环境文件(在运行时通过环境变量或挂载提供)
|
||||
.env
|
||||
.env.local
|
||||
.env.development
|
||||
.env.production
|
||||
|
||||
# 测试相关
|
||||
coverage
|
||||
.nyc_output
|
||||
|
||||
# 编辑器文件
|
||||
.vscode
|
||||
.idea
|
||||
*.swp
|
||||
*.swo
|
||||
|
||||
# 系统文件
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
# Git
|
||||
.git
|
||||
.gitignore
|
||||
|
||||
# Docker相关
|
||||
.dockerignore
|
||||
Dockerfile
|
||||
docker-compose.yml
|
||||
|
||||
# 日志文件
|
||||
logs
|
||||
*.log
|
||||
84
backend/DOCKER-README.md
Normal file
84
backend/DOCKER-README.md
Normal file
@@ -0,0 +1,84 @@
|
||||
# Docker 部署说明
|
||||
|
||||
## 1. 仅构建后端镜像
|
||||
|
||||
```bash
|
||||
# 在 backend 目录中
|
||||
cd backend
|
||||
|
||||
# 构建 Docker 镜像
|
||||
docker build -t employee-performance-backend:latest .
|
||||
|
||||
# 运行容器
|
||||
docker run -d \
|
||||
-p 3001:3001 \
|
||||
-e DB_HOST=localhost \
|
||||
-e DB_PORT=3306 \
|
||||
-e DB_USER=performance_user \
|
||||
-e DB_PASSWORD=userpassword123 \
|
||||
-e DB_NAME=employee_performance \
|
||||
-e JWT_SECRET=your_jwt_secret_here_change_in_production \
|
||||
-e FASTGPT_API_KEY=your_fastgpt_api_key_here \
|
||||
--name employee-performance-backend \
|
||||
employee-performance-backend:latest
|
||||
```
|
||||
|
||||
## 2. 使用 Docker Compose(推荐)
|
||||
|
||||
```bash
|
||||
# 在项目根目录(包含 docker-compose.yml)
|
||||
cd ..
|
||||
|
||||
# 启动所有服务(MySQL + 后端)
|
||||
docker-compose up -d
|
||||
|
||||
# 查看日志
|
||||
docker-compose logs -f
|
||||
|
||||
# 停止服务
|
||||
docker-compose down
|
||||
|
||||
# 停止并删除数据卷
|
||||
docker-compose down -v
|
||||
```
|
||||
|
||||
## 3. 环境变量配置
|
||||
|
||||
复制 `.env.example` 为 `.env` 并根据需要修改:
|
||||
|
||||
```bash
|
||||
cd backend
|
||||
cp .env.example .env
|
||||
```
|
||||
|
||||
或者通过 Docker Compose 的环境变量直接配置(见 docker-compose.yml)。
|
||||
|
||||
## 4. 数据库初始化
|
||||
|
||||
Docker Compose 会自动:
|
||||
1. 创建 MySQL 8.0 容器
|
||||
2. 初始化数据库结构(使用 `src/db/init.sql`)
|
||||
3. 插入测试数据(使用 `src/db/seed.sql`)
|
||||
|
||||
## 5. 健康检查
|
||||
|
||||
后端服务提供健康检查端点:
|
||||
```
|
||||
GET http://localhost:3001/health
|
||||
```
|
||||
|
||||
Docker 容器已配置健康检查,确保服务正常运行。
|
||||
|
||||
## 6. 构建优化
|
||||
|
||||
Dockerfile 使用多阶段构建:
|
||||
- 第一阶段:安装依赖并构建 TypeScript
|
||||
- 第二阶段:仅复制运行所需文件,减小镜像大小
|
||||
|
||||
## 7. 生产环境注意事项
|
||||
|
||||
1. **修改默认密码**:在 docker-compose.yml 中修改 MySQL 密码
|
||||
2. **设置强 JWT_SECRET**:使用强密钥替换默认值
|
||||
3. **配置 FastGPT API**:提供有效的 API 密钥
|
||||
4. **数据持久化**:MySQL 数据存储在 `mysql_data` 卷中
|
||||
5. **网络安全**:生产环境应考虑使用 Docker 网络隔离
|
||||
53
backend/Dockerfile
Normal file
53
backend/Dockerfile
Normal file
@@ -0,0 +1,53 @@
|
||||
# 构建阶段
|
||||
FROM node:20-alpine AS builder
|
||||
|
||||
# 设置工作目录
|
||||
WORKDIR /app
|
||||
|
||||
# 复制package.json和package-lock.json
|
||||
COPY package*.json ./
|
||||
|
||||
# 安装依赖(包括devDependencies,因为需要typescript等构建工具)
|
||||
RUN npm ci --only=production --ignore-scripts && \
|
||||
npm ci --only=development --ignore-scripts
|
||||
|
||||
# 复制源代码
|
||||
COPY . .
|
||||
|
||||
# 构建TypeScript
|
||||
RUN npm run build
|
||||
|
||||
# 运行时阶段
|
||||
FROM node:20-alpine AS runtime
|
||||
|
||||
# 设置工作目录
|
||||
WORKDIR /app
|
||||
|
||||
# 创建非root用户
|
||||
RUN addgroup -g 1001 -S nodejs && \
|
||||
adduser -S nodejs -u 1001 -G nodejs
|
||||
|
||||
# 复制package.json
|
||||
COPY --from=builder --chown=nodejs:nodejs /app/package*.json ./
|
||||
|
||||
# 安装仅生产依赖
|
||||
RUN npm ci --only=production --ignore-scripts
|
||||
|
||||
# 复制构建产物
|
||||
COPY --from=builder --chown=nodejs:nodejs /app/dist ./dist
|
||||
|
||||
# 复制可能需要的其他文件(如数据库初始化脚本)
|
||||
COPY --from=builder --chown=nodejs:nodejs /app/src/db ./src/db
|
||||
|
||||
# 切换用户
|
||||
USER nodejs
|
||||
|
||||
# 暴露端口(默认3001,可通过环境变量覆盖)
|
||||
EXPOSE 3001
|
||||
|
||||
# 健康检查
|
||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
|
||||
CMD node -e "require('http').get('http://localhost:3001/health', (r) => {if(r.statusCode===200)process.exit(0);process.exit(1)}).on('error',()=>process.exit(1))"
|
||||
|
||||
# 启动命令
|
||||
CMD ["npm", "start"]
|
||||
1
backend/tmpclaude-14d8-cwd
Normal file
1
backend/tmpclaude-14d8-cwd
Normal file
@@ -0,0 +1 @@
|
||||
/c/Users/99095/Desktop/优一科技/performance-evaluation-system/backend
|
||||
1
backend/tmpclaude-3607-cwd
Normal file
1
backend/tmpclaude-3607-cwd
Normal file
@@ -0,0 +1 @@
|
||||
/c/Users/99095/Desktop/优一科技/performance-evaluation-system/backend
|
||||
1
backend/tmpclaude-7c27-cwd
Normal file
1
backend/tmpclaude-7c27-cwd
Normal file
@@ -0,0 +1 @@
|
||||
/c/Users/99095/Desktop/优一科技/performance-evaluation-system/backend
|
||||
Reference in New Issue
Block a user