unis_hrms/Dockerfile

69 lines
3.0 KiB
Docker
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 运行镜像(两段式:先构建 React 前端,再装 Python 后端)
#
# 日常部署请用 ./scripts/deploy.sh up(会顺带检查 docker、准备 .env、等健康检查)
# 手工等价命令:
# docker build -t unis-hrms .
# docker run -d -p 8000:8000 -v $PWD/data:/app/data unis-hrms
#
# 镜像里是代码 + 前端构建产物;SQLite 库、上传附件都在 ./data,靠 bind mount 挂进来,
# 换镜像不动数据(data/ 已被 .dockerignore 排除,不会把本机库打进镜像)。
# 需要 MySQL 驱动:docker build --build-arg EXTRAS=[mysql] .
# =============================================================================
ARG BASE_IMAGE=python:3.12-slim
ARG NODE_IMAGE=node:22-alpine
# ---------- 1/2 前端构建(web/dist)----------
FROM ${NODE_IMAGE} AS webbuild
# 国内直连 registry.npmjs.org 很慢,默认走 npmmirror;境外构建覆盖成官方源即可
ARG NPM_REGISTRY=https://registry.npmmirror.com
WORKDIR /build
# 依赖单独一层:改业务代码不触发重装 node_modules
COPY web/package.json web/package-lock.json ./
RUN npm config set registry "${NPM_REGISTRY}" \
&& npm ci --cache /tmp/npm-cache --no-audit --no-fund
COPY web ./
# tsc --noEmit && vite build → /build/dist
RUN npm run build
# ---------- 2/2 运行镜像 ----------
# 基础镜像可替换成本地已有镜像(直连 docker.io 可能在 load metadata 阶段超时)
FROM ${BASE_IMAGE}
ARG PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple
# 追加 extras,例如 EXTRAS=[mysql] 安装 aiomysql + PyMySQL + cryptography
ARG EXTRAS=
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PIP_NO_CACHE_DIR=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1 \
TZ=Asia/Shanghai
WORKDIR /app
# 后端代码 + Alembic 迁移(deploy.sh migrate 会用到)
COPY pyproject.toml ./
COPY alembic.ini ./
COPY migrations ./migrations
COPY app ./app
RUN pip install --retries 5 --timeout 60 -i "${PIP_INDEX_URL}" ".${EXTRAS}"
# 前端构建产物:后端启动时自动托管(settings.serve_web_dist),单端口整站服务
COPY --from=webbuild /build/dist ./web/dist
# 数据目录(实际内容由 volume 提供);镜像以 root 运行,
# macOS 上 bind mount 只有 root 写 data/ 最省心;Linux 想换非特权账号:
# 在 compose 里加 user: "1000:1000",并确保 ./data 属主一致。
RUN mkdir -p data/uploads
ENV PORT=8000 \
WORKERS=1
EXPOSE 8000
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
CMD python -c "import os,urllib.request;urllib.request.urlopen('http://127.0.0.1:%s/health' % os.environ.get('PORT','8000'),timeout=3).read()"
# 建表由应用启动时完成(AUTO_CREATE_SCHEMA);演示数据由 AUTO_SEED 幂等播种,
# 正式结构变更用 ./scripts/deploy.sh migrate 走 Alembic。
CMD ["sh", "-c", "exec python -m uvicorn app.main:app --host 0.0.0.0 --port \"${PORT:-8000}\" --workers \"${WORKERS:-1}\""]