#!/bin/sh # 尚数网本体管理平台 · 单镜像 entrypoint # # 设计要点: # 1. 用户挂 -v /host/x:/data 时: /data/pg -> PG 数据; /data/archive -> 文档 # 2. 不挂: PG 用匿名卷 (不持久化), archive 用镜像内自带目录 # 3. PG 用官方 docker-entrypoint.sh 初始化 (用户/库/权限都由它管) # 4. ontology exec 成 PID 1 (接 SIGTERM 优雅退出), PG 在后台, trap 保证一起收 set -eu # ---------------------------------------------------------------------------- # 1. 数据目录决策 # ---------------------------------------------------------------------------- # /data/pg 存在 -> 用户明确要持久化 PG 到挂载卷 if [ -d /data ] && [ -d /data/pg ]; then export PGDATA=/data/pg echo "[entrypoint] PGDATA=/data/pg (mounted)" else export PGDATA=/var/lib/postgresql/data echo "[entrypoint] PGDATA=/var/lib/postgresql/data (anonymous)" fi # /data/archive 存在 -> 软链到 /app/archive, 让 ontology 写入透明生效 if [ -d /data ] && [ -d /data/archive ]; then rm -rf /app/archive ln -sfn /data/archive /app/archive echo "[entrypoint] archive -> /data/archive (mounted)" else mkdir -p /app/archive echo "[entrypoint] archive -> /app/archive (ephemeral)" fi # ---------------------------------------------------------------------------- # 2. 启动 Postgres (用官方 entrypoint 完成 initdb/建用户/建库等) # ---------------------------------------------------------------------------- echo "[entrypoint] starting postgres..." /usr/local/bin/docker-entrypoint.sh postgres & PG_PID=$! # ---------------------------------------------------------------------------- # 3. 等 PG 就绪 (最多 60s) # ---------------------------------------------------------------------------- echo "[entrypoint] waiting for postgres at 127.0.0.1:5432..." READY=no for i in $(seq 1 60); do if pg_isready -h 127.0.0.1 -p 5432 \ -U "${POSTGRES_USER:-ssw}" \ -d "${POSTGRES_DB:-ssw-ontology}" -q 2>/dev/null; then READY=yes break fi sleep 1 done if [ "$READY" != "yes" ]; then echo "[entrypoint] FATAL: postgres not ready after 60s, dumping logs..." kill -TERM "$PG_PID" 2>/dev/null || true wait "$PG_PID" 2>/dev/null || true exit 1 fi echo "[entrypoint] postgres ready (db=${POSTGRES_DB:-ssw-ontology})" # ---------------------------------------------------------------------------- # 4. trap: ontology 退出时, 顺手把 PG 也带走 (避免孤儿进程) # ---------------------------------------------------------------------------- cleanup() { echo "[entrypoint] stopping postgres (pid=$PG_PID)..." kill -TERM "$PG_PID" 2>/dev/null || true wait "$PG_PID" 2>/dev/null || true } trap cleanup EXIT TERM INT # ---------------------------------------------------------------------------- # 5. 前台启动 ontology (exec -> 它就是 PID 1, 接收 docker stop 的 SIGTERM) # ---------------------------------------------------------------------------- echo "[entrypoint] starting ontology on :${HTTP_PORT:-8081}..." exec /app/server