构建: 入口脚本 chown 数据目录后降权运行

绑定挂载/命名卷被 docker 以 root 自动创建时,容器非 root 用户
写不进 db(SQLITE_CANTOPEN)。改为 entrypoint 以 root 启动,
chown 数据目录到 OT_UID:OT_GID(默认 1000:1000)后 su-exec 降权,
compose 不再需要 user: 覆盖。

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Sakurasan
2026-08-18 22:25:43 +08:00
co-authored by Claude
parent 5aa0be13dc
commit 8495694671
3 changed files with 20 additions and 6 deletions
+5 -4
View File
@@ -35,18 +35,19 @@ RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH \
# ---------- 阶段 3:运行镜像 ---------- # ---------- 阶段 3:运行镜像 ----------
FROM alpine:3.21 FROM alpine:3.21
RUN apk add --no-cache ca-certificates tzdata \ RUN apk add --no-cache ca-certificates tzdata su-exec
&& addgroup -S openteam && adduser -S -G openteam openteam
WORKDIR /app WORKDIR /app
COPY --from=go-builder /out/openteam ./openteam COPY --from=go-builder /out/openteam ./openteam
# 与二进制同目录放置,符合服务 CWD 约定(router 按相对路径 web/dist 找静态资源) # 与二进制同目录放置,符合服务 CWD 约定(router 按相对路径 web/dist 找静态资源)
COPY --from=web-builder /src/dist ./web/dist COPY --from=web-builder /src/dist ./web/dist
RUN mkdir -p /app/data && chown -R openteam:openteam /app # 入口脚本以 root 启动,chown 数据目录后 su-exec 降权为 OT_UID:OT_GID(默认 1000:1000)
USER openteam COPY docker/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh && mkdir -p /app/data
ENV OT_ENV=production \ ENV OT_ENV=production \
OT_PORT=8080 \ OT_PORT=8080 \
OT_DB_DRIVER=sqlite \ OT_DB_DRIVER=sqlite \
OT_DB_DSN=data/openteam.db OT_DB_DSN=data/openteam.db
EXPOSE 8080 EXPOSE 8080
VOLUME ["/app/data"] VOLUME ["/app/data"]
ENTRYPOINT ["/entrypoint.sh"]
CMD ["./openteam"] CMD ["./openteam"]
+3 -2
View File
@@ -17,8 +17,6 @@ services:
restart: unless-stopped restart: unless-stopped
ports: ports:
- "${HOST_PORT:-8080}:8080" - "${HOST_PORT:-8080}:8080"
# 以宿主用户运行,保证能写当前目录的 ./data 绑盘(默认 1000:1000,可用 OT_UID/OT_GID 覆盖)
user: "${OT_UID:-1000}:${OT_GID:-1000}"
environment: environment:
OT_ENV: production OT_ENV: production
OT_PORT: 8080 OT_PORT: 8080
@@ -29,6 +27,9 @@ services:
OT_MASTER_KEY: ${OT_MASTER_KEY:?请在 .env 中设置 OT_MASTER_KEY} OT_MASTER_KEY: ${OT_MASTER_KEY:?请在 .env 中设置 OT_MASTER_KEY}
OT_ADMIN_PASSWORD: ${OT_ADMIN_PASSWORD:?请在 .env 中设置 OT_ADMIN_PASSWORD} OT_ADMIN_PASSWORD: ${OT_ADMIN_PASSWORD:?请在 .env 中设置 OT_ADMIN_PASSWORD}
OT_JWT_SECRET: ${OT_JWT_SECRET:?请在 .env 中设置 OT_JWT_SECRET} OT_JWT_SECRET: ${OT_JWT_SECRET:?请在 .env 中设置 OT_JWT_SECRET}
# 入口脚本据此 chown 数据目录并降权运行(默认 1000:1000)
OT_UID: "${OT_UID:-1000}"
OT_GID: "${OT_GID:-1000}"
volumes: volumes:
- ./data:/app/data - ./data:/app/data
healthcheck: healthcheck:
+12
View File
@@ -0,0 +1,12 @@
#!/bin/sh
# openteam 容器入口:以 root 短暂启动,修正数据目录属主后降权执行服务。
# 解决绑定挂载/命名卷被 docker 以 root 自动创建、应用非 root 用户写不进 db 的问题。
set -e
PUID="${OT_UID:-1000}"
PGID="${OT_GID:-1000}"
mkdir -p /app/data
chown -R "${PUID}:${PGID}" /app/data
exec su-exec "${PUID}:${PGID}" "$@"