linjinpeng commited on
Commit
3d5d12f
·
verified ·
1 Parent(s): 9d0db17

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +81 -76
Dockerfile CHANGED
@@ -1,76 +1,81 @@
1
- # 🎯 前端构建阶段
2
- FROM node:18-alpine AS frontend-builder
3
-
4
- # 安装 git
5
- RUN apk add --no-cache git
6
-
7
- # 设置工作目录
8
- WORKDIR /app
9
-
10
- # 克隆仓库
11
- ARG GIT_REPO_URL=https://github.com/Wei-Shaw/claude-relay-service.git
12
- ARG GIT_BRANCH=main
13
- RUN git clone --depth 1 --branch ${GIT_BRANCH} ${GIT_REPO_URL} .
14
-
15
- # 构建前端
16
- WORKDIR /app/web/admin-spa
17
- RUN npm ci && npm run build
18
-
19
- # 🐳 主应用阶段
20
- FROM node:18-alpine
21
-
22
- # 📋 设置标签
23
- LABEL maintainer="claude-relay-service@example.com"
24
- LABEL description="Claude Code API Relay Service"
25
- LABEL version="1.0.0"
26
-
27
- # 🔧 安装系统依赖和 git
28
- RUN apk add --no-cache \
29
- curl \
30
- dumb-init \
31
- sed \
32
- git \
33
- && rm -rf /var/cache/apk/*
34
-
35
- # 📁 设置工作目录
36
- WORKDIR /app
37
-
38
- # 克隆仓库
39
- ARG GIT_REPO_URL=https://github.com/Wei-Shaw/claude-relay-service.git
40
- ARG GIT_BRANCH=main
41
- RUN git clone --depth 1 --branch ${GIT_BRANCH} ${GIT_REPO_URL} .
42
-
43
- # 🔽 安装依赖 (生产环境)
44
- RUN npm ci --only=production && \
45
- npm cache clean --force
46
-
47
- # 📦 从构建阶段复制前端产物
48
- COPY --from=frontend-builder /app/web/admin-spa/dist /app/web/admin-spa/dist
49
-
50
- # 🔧 设置启动脚本权限
51
- RUN chmod +x docker-entrypoint.sh && \
52
- cp docker-entrypoint.sh /usr/local/bin/
53
-
54
- # 📁 创建必要目录并设置权限
55
- RUN mkdir -p logs data temp config && \
56
- chmod -R 777 /app/logs /app/data /app/temp /app/config
57
-
58
- # 🔧 预先创建配置文件并设置权限
59
- RUN if [ ! -f "/app/config/config.js" ] && [ -f "/app/config/config.example.js" ]; then \
60
- cp /app/config/config.example.js /app/config/config.js; \
61
- fi && \
62
- chmod 777 /app/config/config.js 2>/dev/null || true
63
-
64
- # 🔧 设置整个应用目录权限
65
- RUN chmod -R 777 /app
66
-
67
- # 🌐 暴露端口
68
- EXPOSE 3000
69
-
70
- # 🏥 健康检查
71
- HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
72
- CMD curl -f http://localhost:3000/health || exit 1
73
-
74
- # 🚀 启动应用
75
- ENTRYPOINT ["dumb-init", "--", "/usr/local/bin/docker-entrypoint.sh"]
76
- CMD ["node", "src/app.js"]
 
 
 
 
 
 
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"]