chore: 完善 Docker 部署配置并修复测试超时

- 新增 Dockerfile: 多阶段构建,优化镜像大小
- 新增 .dockerignore: 加速构建,排除不必要文件
- 更新 docker-compose.yml: 使用 SQLite 简化部署
- 修复 vitest.config.js: testTimeout 改为 60000ms 修复慢测试超时
This commit is contained in:
2026-04-08 22:13:46 +08:00
parent a85d822419
commit 1b96715b55
4 changed files with 130 additions and 12 deletions

47
.dockerignore Normal file
View File

@@ -0,0 +1,47 @@
# Git
.git
.gitignore
# IDE
.idea
.vscode
*.swp
*.swo
# 构建产物
bin/
dist/
frontend/dist/
frontend/node_modules/
coverage/
# 测试文件
*_test.go
*.test.ts
*.test.tsx
vitest.config.*
# 文档
*.md
docs/
# 配置文件 (使用打包内的 configs 目录)
configs/
# 部署脚本
scripts/
deployment/
# Docker 文件
docker-compose*.yml
Dockerfile*
# 其他
.tmp/
.cache/
.gomdcache/
.gocache/
.workbuddy/
.coverage/
.env
.env.*

49
Dockerfile Normal file
View File

@@ -0,0 +1,49 @@
# 构建阶段
FROM golang:1.23-alpine AS builder
WORKDIR /build
# 安装构建依赖
RUN apk add --no-cache git ca-certificates tzdata
# 复制 Go 模块文件
COPY go.mod go.sum ./
RUN go mod download
# 复制源代码
COPY . .
# 编译应用
ARG GIN_MODE=release
ENV GIN_MODE=${GIN_MODE}
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o server ./cmd/server
# 运行阶段
FROM alpine:3.19
WORKDIR /app
# 安装运行时依赖
RUN apk add --no-cache ca-certificates tzdata
# 从构建阶段复制二进制文件
COPY --from=builder /build/server .
COPY --from=builder /build/configs ./configs
COPY --from=builder /build/data ./data
# 创建日志目录
RUN mkdir -p /app/logs
# 设置时区
ENV TZ=Asia/Shanghai
ENV GIN_MODE=release
# 暴露端口
EXPOSE 8080
# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --retries=3 --start-period=5s \
CMD wget -q --spider http://localhost:8080/api/v1/health || exit 1
# 启动命令
CMD ["./server"]

View File

@@ -1,26 +1,48 @@
version: '3.8'
# 用户管理系统 - Docker 部署配置
# 使用 SQLite 数据库,无需额外数据库服务
#
# 使用方法:
# docker compose up -d # 启动服务
# docker compose logs -f # 查看日志
# docker compose down # 停止服务
#
# 访问: http://localhost:8080
services:
# 用户管理服务
user-management:
build: .
container_name: user-ms-app
app:
build:
context: .
dockerfile: Dockerfile
container_name: user-management-app
restart: unless-stopped
ports:
- "8080:8080"
volumes:
# 持久化 SQLite 数据库
- app-data:/app/data
# 持久化日志
- app-logs:/app/logs
environment:
- DB_HOST=postgres
- DB_PORT=5432
- DB_USER=user_ms
- DB_PASSWORD=user_ms_pass
- DB_NAME=user_ms
depends_on:
- postgres
- TZ=Asia/Shanghai
healthcheck:
test: ["CMD", "wget", "-q", "--spider", "http://localhost:8080/api/v1/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 10s
networks:
- user-ms-network
volumes:
postgres-data:
app-data:
driver: local
app-logs:
driver: local
networks:
user-ms-network:
driver: bridge

View File

@@ -29,7 +29,7 @@ export default defineConfig({
'src/vite-env.d.ts',
],
},
timeout: 10000,
testTimeout: 60000,
clearMocks: true,
},
})