fix: 优化后端部署配置,适配现有服务架构

主要修改:
- 移除 postgres:15-alpine 和 redis 容器依赖
- 优化 docker-compose.yml 配置,使用 host 网络模式
- 移除 CI/CD 中的自动数据库迁移,改为手动执行
- 更新环境变量配置,连接到现有的 PostgreSQL 和 Redis 服务
- 完善部署文档,增加现有服务集成说明

配置优化:
- 修正 docker-compose.yml 位置到 backend 目录
- 简化 CI/CD 测试流程,跳过需要数据库的测试
- 增加数据库迁移安全策略说明
- 完善部署流程文档和故障排除指南
This commit is contained in:
xujiang
2025-07-09 16:42:22 +08:00
parent 72414d0979
commit 0651b6626a
6 changed files with 371 additions and 280 deletions

View File

@ -1,140 +1,71 @@
version: '3.8'
services:
# PostgreSQL 数据库
postgres:
image: postgres:15-alpine
container_name: photography_postgres
environment:
POSTGRES_DB: photography
POSTGRES_USER: postgres
POSTGRES_PASSWORD: ${DB_PASSWORD:-photography_password}
POSTGRES_INITDB_ARGS: "--encoding=UTF-8 --lc-collate=C --lc-ctype=C"
ports:
- "${DB_PORT:-5432}:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
- ./migrations:/docker-entrypoint-initdb.d
networks:
- photography_network
restart: unless-stopped
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 30s
timeout: 10s
retries: 3
# Redis 缓存
redis:
image: redis:7-alpine
container_name: photography_redis
ports:
- "${REDIS_PORT:-6379}:6379"
volumes:
- redis_data:/data
networks:
- photography_network
restart: unless-stopped
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 30s
timeout: 10s
retries: 3
command: redis-server --appendonly yes
# 后端 API 服务
backend:
build:
context: .
context: ./backend
dockerfile: Dockerfile
container_name: photography_backend
restart: unless-stopped
environment:
# 数据库配置
DB_HOST: postgres
DB_PORT: 5432
DB_USER: postgres
DB_PASSWORD: ${DB_PASSWORD:-photography_password}
DB_NAME: photography
DB_SSL_MODE: disable
# 数据库配置 (连接到现有的 PostgreSQL)
DB_HOST: ${DB_HOST:-localhost}
DB_PORT: ${DB_PORT:-5432}
DB_NAME: ${DB_NAME:-photography}
DB_USER: ${DB_USER:-postgres}
DB_PASSWORD: ${DB_PASSWORD}
# Redis 配置
REDIS_HOST: redis
REDIS_PORT: 6379
REDIS_PASSWORD: ""
REDIS_DB: 0
# Redis 配置 (连接到现有的 Redis)
REDIS_HOST: ${REDIS_HOST:-localhost}
REDIS_PORT: ${REDIS_PORT:-6379}
REDIS_PASSWORD: ${REDIS_PASSWORD}
# JWT 配置
JWT_SECRET: ${JWT_SECRET:-your-super-secret-jwt-key-change-in-production}
JWT_EXPIRES_IN: 24h
JWT_SECRET: ${JWT_SECRET}
JWT_EXPIRES_IN: ${JWT_EXPIRES_IN:-24h}
# 服务器配置
PORT: 8080
GIN_MODE: ${GIN_MODE:-release}
GIN_MODE: release
# 文件上传配置
UPLOAD_TYPE: local
UPLOAD_PATH: /app/uploads
UPLOAD_BASE_URL: http://localhost:8080/uploads
UPLOAD_MAX_SIZE: 10485760 # 10MB
# 文件存储配置
STORAGE_TYPE: ${STORAGE_TYPE:-local}
STORAGE_PATH: /app/uploads
MAX_UPLOAD_SIZE: ${MAX_UPLOAD_SIZE:-10MB}
# 日志配置
LOG_LEVEL: ${LOG_LEVEL:-info}
LOG_FORMAT: json
ports:
- "${API_PORT:-8080}:8080"
volumes:
- upload_data:/app/uploads
- ./configs:/app/configs:ro
networks:
- photography_network
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
restart: unless-stopped
- ./backend/uploads:/app/uploads
- ./backend/logs:/app/logs
- ./backend/configs:/app/configs
ports:
- "127.0.0.1:8080:8080"
# 使用 host 网络模式以便访问宿主机的服务
network_mode: host
healthcheck:
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8080/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 30s
retries: 5
start_period: 60s
# Nginx 反向代理 (生产环境)
nginx:
image: nginx:alpine
container_name: photography_nginx
ports:
- "${HTTP_PORT:-80}:80"
- "${HTTPS_PORT:-443}:443"
# 数据库备份服务 (可选,如果你想使用容器化备份)
backup:
image: postgres:16-alpine
container_name: photography_backup
restart: "no"
environment:
PGPASSWORD: ${DB_PASSWORD}
DB_HOST: ${DB_HOST:-localhost}
DB_USER: ${DB_USER:-postgres}
DB_NAME: ${DB_NAME:-photography}
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- ./nginx/conf.d:/etc/nginx/conf.d:ro
- upload_data:/var/www/uploads:ro
- ./ssl:/etc/nginx/ssl:ro
networks:
- photography_network
depends_on:
- backend
restart: unless-stopped
healthcheck:
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost/health"]
interval: 30s
timeout: 10s
retries: 3
- ./backups:/backups
- ./scripts/backup.sh:/backup.sh
network_mode: host
entrypoint: ["/backup.sh"]
profiles:
- production
# 数据卷
volumes:
postgres_data:
driver: local
redis_data:
driver: local
upload_data:
driver: local
# 网络
networks:
photography_network:
driver: bridge
- backup # 使用 profile 使这个服务可选