Spaces:
Running
Running
Update Dockerfile
Browse files- Dockerfile +81 -76
Dockerfile
CHANGED
|
@@ -1,76 +1,81 @@
|
|
| 1 |
-
# 🎯
|
| 2 |
-
FROM node:
|
| 3 |
-
|
| 4 |
-
# 安装 git
|
| 5 |
-
RUN apk add --no-cache
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
RUN
|
| 18 |
-
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 🎯 阶段一:源码克隆与构建阶段
|
| 2 |
+
FROM node:20-alpine AS builder
|
| 3 |
+
|
| 4 |
+
# 安装构建原生插件所需的依赖 (python3, make, g++) 以及 git
|
| 5 |
+
RUN apk add --no-cache \
|
| 6 |
+
git \
|
| 7 |
+
python3 \
|
| 8 |
+
make \
|
| 9 |
+
g++
|
| 10 |
+
|
| 11 |
+
# 设置工作目录
|
| 12 |
+
WORKDIR /app
|
| 13 |
+
|
| 14 |
+
# 克隆仓库(在此阶段克隆一次即可)
|
| 15 |
+
ARG GIT_REPO_URL=https://github.com/Wei-Shaw/claude-relay-service.git
|
| 16 |
+
ARG GIT_BRANCH=main
|
| 17 |
+
RUN git clone --depth 1 --branch ${GIT_BRANCH} ${GIT_REPO_URL} .
|
| 18 |
+
|
| 19 |
+
# --- 构建前端 ---
|
| 20 |
+
WORKDIR /app/web/admin-spa
|
| 21 |
+
RUN npm ci && npm run build
|
| 22 |
+
|
| 23 |
+
# --- 准备后端生产环境依赖 ---
|
| 24 |
+
WORKDIR /app
|
| 25 |
+
# 再次运行 npm ci 确保生产环境依赖(如 heapdump)能成功编译
|
| 26 |
+
RUN npm ci --only=production && \
|
| 27 |
+
npm cache clean --force
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# 🐳 阶段二:最终运行镜像
|
| 31 |
+
FROM node:20-alpine
|
| 32 |
+
|
| 33 |
+
# 📋 设置标签
|
| 34 |
+
LABEL maintainer="claude-relay-service@example.com"
|
| 35 |
+
LABEL description="Claude Code API Relay Service"
|
| 36 |
+
LABEL version="1.0.0"
|
| 37 |
+
|
| 38 |
+
# 🔧 安装运行时必要的系统依赖
|
| 39 |
+
RUN apk add --no-cache \
|
| 40 |
+
curl \
|
| 41 |
+
dumb-init \
|
| 42 |
+
sed \
|
| 43 |
+
git \
|
| 44 |
+
&& rm -rf /var/cache/apk/*
|
| 45 |
+
|
| 46 |
+
# 📁 设置工作目录
|
| 47 |
+
WORKDIR /app
|
| 48 |
+
|
| 49 |
+
# 📦 从 builder 阶段复制整个应用(含已编译的生产环境 node_modules)
|
| 50 |
+
COPY --from=builder /app /app
|
| 51 |
+
|
| 52 |
+
# 📦 确保前端产物已正确放置(覆盖可能存在的旧文件)
|
| 53 |
+
COPY --from=builder /app/web/admin-spa/dist /app/web/admin-spa/dist
|
| 54 |
+
|
| 55 |
+
# 🔧 设置启动脚本权限
|
| 56 |
+
RUN chmod +x docker-entrypoint.sh && \
|
| 57 |
+
cp docker-entrypoint.sh /usr/local/bin/
|
| 58 |
+
|
| 59 |
+
# 📁 创建必要目录并设置权限
|
| 60 |
+
RUN mkdir -p logs data temp config && \
|
| 61 |
+
chmod -R 777 /app/logs /app/data /app/temp /app/config
|
| 62 |
+
|
| 63 |
+
# 🔧 预先处理配置文件
|
| 64 |
+
RUN if [ ! -f "/app/config/config.js" ] && [ -f "/app/config/config.example.js" ]; then \
|
| 65 |
+
cp /app/config/config.example.js /app/config/config.js; \
|
| 66 |
+
fi && \
|
| 67 |
+
chmod 777 /app/config/config.js 2>/dev/null || true
|
| 68 |
+
|
| 69 |
+
# 🔧 统一设置应用目录权限(生产环境建议根据需求收紧权限)
|
| 70 |
+
RUN chmod -R 777 /app
|
| 71 |
+
|
| 72 |
+
# 🌐 暴露端口
|
| 73 |
+
EXPOSE 3000
|
| 74 |
+
|
| 75 |
+
# 🏥 健康检查
|
| 76 |
+
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
|
| 77 |
+
CMD curl -f http://localhost:3000/health || exit 1
|
| 78 |
+
|
| 79 |
+
# 🚀 启动应用
|
| 80 |
+
ENTRYPOINT ["dumb-init", "--", "/usr/local/bin/docker-entrypoint.sh"]
|
| 81 |
+
CMD ["node", "src/app.js"]
|