unis_hrms/docker-compose.yml

100 lines
4.4 KiB
YAML
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# =============================================================================
# UNIS HRMS · 部署编排(单容器整站:后端 + 前端构建产物 + /uploads)
#
# 推荐用封装脚本(会检查 docker、准备 .env、建 data 目录、等健康检查):
# ./scripts/deploy.sh up 首次部署(构建 + 后台启动)
# ./scripts/deploy.sh update 改完代码重建并替换容器
# ./scripts/deploy.sh logs 看日志
# ./scripts/deploy.sh migrate 跑 Alembic 迁移
# ./scripts/deploy.sh backup 备份 SQLite 库到 data/backups/
# ./scripts/deploy.sh down 停止并删除容器(数据留在 ./data)
#
# 等价的裸命令:docker compose up -d --build (配置来自同目录 .env)
# =============================================================================
name: unis-hrms
services:
hrms:
build:
context: .
args:
# 基础镜像仓库/版本与 pip、npm 源都可在这里覆盖(deploy.sh 会把探测结果写进 .env)
BASE_IMAGE: "${BASE_IMAGE:-python:3.12-slim}"
NODE_IMAGE: "${NODE_IMAGE:-node:22-alpine}"
PIP_INDEX_URL: "${PIP_INDEX_URL:-https://pypi.tuna.tsinghua.edu.cn/simple}"
NPM_REGISTRY: "${NPM_REGISTRY:-https://registry.npmmirror.com}"
# 用 compose 自带的 mysql 服务(--profile mysql)时改成 EXTRAS=[mysql]
EXTRAS: "${PIP_EXTRAS:-}"
image: unis-hrms:latest
container_name: unis-hrms
restart: unless-stopped
environment:
APP_NAME: "${APP_NAME:-UNIS 人力资源管理系统}"
# 生产必须替换:./scripts/deploy.sh up 首次会自动生成随机值写进 .env
SECRET_KEY: "${SECRET_KEY:-change-me-in-production}"
# 默认用 ./data 下的 SQLite;换 MySQL 只改这一行(见 docs/MySQL迁移指南.md)
DATABASE_URL: "${DATABASE_URL:-sqlite+aiosqlite:///./data/hrms.db}"
# 首次启动建表;正式环境想完全交给 Alembic 就设 false 并跑 deploy.sh migrate
AUTO_CREATE_SCHEMA: "${AUTO_CREATE_SCHEMA:-true}"
# 首次启动播种演示数据(幂等,已播种则跳过)。上线后置 false
AUTO_SEED: "${AUTO_SEED:-true}"
ACCESS_TOKEN_MINUTES: "${ACCESS_TOKEN_MINUTES:-120}"
MAX_UPLOAD_MB: "${MAX_UPLOAD_MB:-20}"
UPLOAD_DIR: "./data/uploads"
# 单端口整站部署时前端与接口同源,CORS 基本不生效;分离部署再覆盖它
CORS_ORIGINS: '${CORS_ORIGINS:-["http://localhost:5173"]}'
SERVE_WEB_DIST: "true"
# 容器内端口固定 8000(与 EXPOSE/健康检查对齐),对外端口改 HOST_PORT
PORT: "8000"
# SQLite 只支持单进程;切到 MySQL 后再调大
WORKERS: "${WORKERS:-1}"
# 覆盖宿主 TZ:日期/周次等按它计算
TZ: "${TZ:-Asia/Shanghai}"
ports:
- "${HOST_PORT:-8000}:8000"
volumes:
# SQLite 库 + 上传附件落在宿主机 ./data,换镜像不动数据
- ./data:/app/data
# Linux 上想让容器以普通用户跑(前提是 ./data 属主一致):
# user: "1000:1000"
# ---------------------------------------------------------------------------
# 可选的 MySQL 8:docker compose --profile mysql up -d
# 打开它需要同时把 .env 里的连接串指向服务名 mysql:
# DATABASE_URL=mysql+aiomysql://hrms:hrms@mysql:3306/hrms?charset=utf8mb4
# PIP_EXTRAS=[mysql] # 镜像里要装 aiomysql/PyMySQL/cryptography
# 之后 ./scripts/deploy.sh mysql-up 会先等 MySQL healthy 再起应用。
# ---------------------------------------------------------------------------
mysql:
profiles: ["mysql"]
image: "${MYSQL_IMAGE:-mysql:8.4}"
container_name: unis-hrms-mysql
restart: unless-stopped
command:
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_0900_ai_ci
- --max-allowed-packet=67108864
environment:
MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD:-hrms-root}"
MYSQL_DATABASE: "${MYSQL_DATABASE:-hrms}"
MYSQL_USER: "${MYSQL_USER:-hrms}"
MYSQL_PASSWORD: "${MYSQL_PASSWORD:-hrms}"
TZ: "${TZ:-Asia/Shanghai}"
ports:
- "${MYSQL_HOST_PORT:-3306}:3306"
volumes:
- hrms-mysql:/var/lib/mysql
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1", "-uroot", "-p${MYSQL_ROOT_PASSWORD:-hrms-root}"]
interval: 10s
timeout: 5s
retries: 12
start_period: 30s
volumes:
hrms-mysql: