# 🎯 阶段一:源码克隆与构建阶段 FROM node:20-alpine AS builder # 安装构建原生插件所需的依赖 (python3, make, g++) 以及 git RUN apk add --no-cache \ git \ python3 \ make \ g++ # 设置工作目录 WORKDIR /app # 克隆仓库(在此阶段克隆一次即可) ARG GIT_REPO_URL=https://github.com/Wei-Shaw/claude-relay-service.git ARG GIT_BRANCH=main RUN git clone --depth 1 --branch ${GIT_BRANCH} ${GIT_REPO_URL} . # --- 构建前端 --- WORKDIR /app/web/admin-spa RUN npm ci && npm run build # --- 准备后端生产环境依赖 --- WORKDIR /app # 再次运行 npm ci 确保生产环境依赖(如 heapdump)能成功编译 RUN npm ci --only=production && \ npm cache clean --force # 🐳 阶段二:最终运行镜像 FROM node:20-alpine # 📋 设置标签 LABEL maintainer="claude-relay-service@example.com" LABEL description="Claude Code API Relay Service" LABEL version="1.0.0" # 🔧 安装运行时必要的系统依赖 RUN apk add --no-cache \ curl \ dumb-init \ sed \ git \ && rm -rf /var/cache/apk/* # 📁 设置工作目录 WORKDIR /app # 📦 从 builder 阶段复制整个应用(含已编译的生产环境 node_modules) COPY --from=builder /app /app # 📦 确保前端产物已正确放置(覆盖可能存在的旧文件) COPY --from=builder /app/web/admin-spa/dist /app/web/admin-spa/dist # 🔧 设置启动脚本权限 RUN chmod +x docker-entrypoint.sh && \ cp docker-entrypoint.sh /usr/local/bin/ # 📁 创建必要目录并设置权限 RUN mkdir -p logs data temp config && \ chmod -R 777 /app/logs /app/data /app/temp /app/config # 🔧 预先处理配置文件 RUN if [ ! -f "/app/config/config.js" ] && [ -f "/app/config/config.example.js" ]; then \ cp /app/config/config.example.js /app/config/config.js; \ fi && \ chmod 777 /app/config/config.js 2>/dev/null || true # 🔧 统一设置应用目录权限(生产环境建议根据需求收紧权限) RUN chmod -R 777 /app # 🌐 暴露端口 EXPOSE 3000 # 🏥 健康检查 HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ CMD curl -f http://localhost:3000/health || exit 1 # 🚀 启动应用 ENTRYPOINT ["dumb-init", "--", "/usr/local/bin/docker-entrypoint.sh"] CMD ["node", "src/app.js"]