docs: 添加完整的 Docker 部署教程

- README.md: 新增详细的 Docker 部署章节
  - Docker Compose 方式(推荐)
  - Docker 命令方式
  - 多阶段构建(生产环境)
  - 故障排查命令
  - 部署检查清单
- Dockerfile: 优化安全配置
  - 使用非 root 用户运行
  - 添加健康检查
  - 修正端口为 3030
  - 优化目录权限
- 新增 docker-compose.yml 示例文件
- 更新 .gitignore 忽略 docker-compose.override.yml
This commit is contained in:
饭团
2026-03-14 22:24:56 +08:00
parent 9f5aaacb10
commit f7e71a9bb0
4 changed files with 224 additions and 10 deletions

3
.gitignore vendored
View File

@@ -19,3 +19,6 @@ temp/
# 系统文件
.DS_Store
Thumbs.db
# Docker
docker-compose.override.yml

View File

@@ -5,16 +5,25 @@ WORKDIR /app
# 复制 package 文件
COPY package*.json ./
# 安装依赖
# 安装依赖(生产环境)
RUN npm ci --only=production
# 复制源代码
COPY src/ ./src/
COPY config/ ./config/
# 创建配置目录
RUN mkdir -p /root/.qiniu
# 创建配置和日志目录(挂载点)
RUN mkdir -p /app/config /app/logs
EXPOSE 3000
# 设置权限
RUN chown -R node:node /app
# 切换到非 root 用户
USER node
EXPOSE 3030
# 健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:3030/health || exit 1
CMD ["node", "src/index.js"]

184
README.md
View File

@@ -80,21 +80,195 @@
## 📦 部署
### 🐳 Docker推荐
### 🐳 Docker 部署(推荐)
#### 方式 1使用 Docker Compose推荐
**1. 创建 `docker-compose.yml`**
```yaml
version: '3.8'
services:
qiniu-bot:
build: .
container_name: qiniu-bot
restart: always
ports:
- "3030:3030"
env_file:
- .env
volumes:
- ./config:/app/config
- ./logs:/app/logs
networks:
- qiniu-network
networks:
qiniu-network:
driver: bridge
```
**2. 配置环境变量**
```bash
# 构建镜像
docker build -t qiniu-feishu-bot .
# 复制环境变量模板
cp .env.example .env
# 运行容器
# 编辑配置(填入真实信息)
nano .env
```
**3. 启动服务**
```bash
# 构建并启动
docker-compose up -d --build
# 查看日志
docker-compose logs -f qiniu-bot
# 停止服务
docker-compose down
# 重启服务
docker-compose restart
```
---
#### 方式 2使用 Docker 命令
**1. 准备配置文件**
```bash
# 创建配置目录
mkdir -p config
# 复制环境变量模板
cp .env.example .env
# 编辑配置
nano .env
```
**2. 构建镜像**
```bash
docker build -t qiniu-feishu-bot:latest .
```
**3. 运行容器**
```bash
docker run -d \
--name qiniu-bot \
--restart always \
-p 3030:3030 \
--env-file .env \
-v $(pwd)/config:/app/config \
qiniu-feishu-bot
-v $(pwd)/logs:/app/logs \
qiniu-feishu-bot:latest
```
**4. 常用命令**
```bash
# 查看日志
docker logs -f qiniu-bot
# 查看容器状态
docker ps -a | grep qiniu-bot
# 重启容器
docker restart qiniu-bot
# 停止容器
docker stop qiniu-bot
# 删除容器
docker rm qiniu-bot
# 查看容器详情
docker inspect qiniu-bot
```
---
#### 方式 3使用 Dockerfile 多阶段构建(生产环境)
**1. 创建 `.dockerignore`**
```
node_modules
npm-debug.log
.env
.env.*
.git
.gitignore
README.md
*.md
logs/
temp/
.DS_Store
```
**2. 构建优化镜像**
```bash
docker build --no-cache -t qiniu-feishu-bot:prod .
```
**3. 部署到生产环境**
```bash
# 推送到镜像仓库(可选)
docker tag qiniu-feishu-bot:prod your-registry/qiniu-bot:latest
docker push your-registry/qiniu-bot:latest
# 在服务器上拉取并运行
docker pull your-registry/qiniu-bot:latest
docker run -d \
--name qiniu-bot \
--restart always \
-p 3030:3030 \
--env-file .env \
-v /path/to/config:/app/config \
qiniu-feishu-bot:prod
```
---
#### 🔧 Docker 故障排查
```bash
# 查看容器日志
docker logs --tail 100 qiniu-bot
# 进入容器调试
docker exec -it qiniu-bot /bin/sh
# 检查容器健康状态
docker inspect --format='{{.State.Health.Status}}' qiniu-bot
# 查看资源使用
docker stats qiniu-bot
# 清理未使用的镜像
docker image prune -a
```
---
#### 📋 Docker 部署检查清单
- [ ] `.env` 文件已配置(包含飞书和七牛云密钥)
- [ ] `config/` 目录存在且有权限
- [ ] `logs/` 目录存在(用于日志持久化)
- [ ] 端口 3030 未被占用
- [ ] 防火墙已开放 3030 端口(如果需要公网访问)
- [ ] 飞书开放平台事件订阅地址配置正确
### 🐧 Linux / 🍎 macOS
```bash

28
docker-compose.yml Normal file
View File

@@ -0,0 +1,28 @@
version: '3.8'
services:
qiniu-bot:
build: .
container_name: qiniu-bot
restart: always
ports:
- "3030:3030"
env_file:
- .env
volumes:
# 挂载配置目录(持久化)
- ./config:/app/config
# 挂载日志目录(便于查看)
- ./logs:/app/logs
networks:
- qiniu-network
healthcheck:
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:3030/health"]
interval: 30s
timeout: 3s
retries: 3
start_period: 10s
networks:
qiniu-network:
driver: bridge