lydgs commited on
Commit
57727be
·
verified ·
1 Parent(s): 93381bd

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +31 -44
Dockerfile CHANGED
@@ -1,61 +1,48 @@
 
1
  FROM node:20-slim AS builder
2
  WORKDIR /app
3
-
4
- # 安装编译 better-sqlite3 所需的工具
5
- RUN apt-get update && apt-get install -y \
6
- git \
7
- python3 \
8
- make \
9
- g++ \
10
- && rm -rf /var/lib/apt/lists/*
11
-
12
- # 克隆 FreeLLMAPI 仓库(使用 GitHub 镜像加速,可选)
13
  RUN git clone https://github.com/tashfeenahmed/freellmapi.git .
14
-
15
- # 安装依赖(使用国内 npm 镜像加速,可选)
16
- RUN npm config set registry https://registry.npmmirror.com && \
17
- npm install
18
-
19
- # 构建项目(生成 dist 目录)
20
  RUN npm run build
21
 
22
- # --- 生产运行环境 ---
23
  FROM node:20-slim AS runner
24
  WORKDIR /app
25
 
26
- COPY --from=builder /app ./
 
 
 
 
 
 
27
 
28
- # 创建数据目录
29
- RUN mkdir -p /data/freellm
30
 
31
- # 前端静态文件适配(如果存在)
 
32
  RUN cp -r client/dist/* server/dist/public/ 2>/dev/null || cp -r client/dist/* server/public/ 2>/dev/null || true
33
 
34
- # 设置运行端口(Hugging Face 要求 7860)
35
  EXPOSE 7860
36
  ENV PORT=7860
37
  ENV NODE_ENV=production
38
  ENV DATABASE_URL="file:/data/database.sqlite"
39
 
40
- # 可选:添加简单的 Basic Auth 保护(防止你的网关被公开滥用)
41
- # 你可以在 Hugging Face Space 的 Settings -> Repository secrets 中设置 SPACE_BASIC_AUTH_USERNAME 和 SPACE_BASIC_AUTH_PASSWORD
42
- RUN echo "import http from 'http';" > security.js && \
43
- echo "const originalCreateServer = http.createServer;" >> security.js && \
44
- echo "http.createServer = function(onRequest) {" >> security.js && \
45
- echo " return originalCreateServer.call(this, (req, res) => {" >> security.js && \
46
- echo " const auth = req.headers.authorization || '';" >> security.js && \
47
- echo " const b64auth = auth.split(' ')[1] || '';" >> security.js && \
48
- echo " const [user, pwd] = Buffer.from(b64auth, 'base64').toString().split(':');" >> security.js && \
49
- echo " const validUser = process.env.SPACE_BASIC_AUTH_USERNAME || 'admin';" >> security.js && \
50
- echo " const validPwd = process.env.SPACE_BASIC_AUTH_PASSWORD || 'admin123';" >> security.js && \
51
- echo " if (req.url.startsWith('/v1') || (user === validUser && pwd === validPwd)) {" >> security.js && \
52
- echo " return onRequest(req, res);" >> security.js && \
53
- echo " }" >> security.js && \
54
- echo " res.statusCode = 401;" >> security.js && \
55
- echo " res.setHeader('WWW-Authenticate', 'Basic realm=\"FreeLLMAPI\"');" >> security.js && \
56
- echo " res.end('Unauthorized');" >> security.js && \
57
- echo " });" >> security.js && \
58
- echo "};" >> security.js
59
-
60
- # 启动命令:链接数据目录、注入安全中间件、设置 ENCRYPTION_KEY、启动服务
61
- CMD ["sh", "-c", "rm -rf /app/server/data && ln -s /data/freellm /app/server/data && node security.js && export ENCRYPTION_KEY=$(node -e \"console.log(require('crypto').randomBytes(32).toString('hex'))\") && node server/dist/index.js"]
 
1
+ # ========== 第一阶段:构建 ==========
2
  FROM node:20-slim AS builder
3
  WORKDIR /app
4
+ RUN apt-get update && apt-get install -y git python3 make g++ && rm -rf /var/lib/apt/lists/*
 
 
 
 
 
 
 
 
 
5
  RUN git clone https://github.com/tashfeenahmed/freellmapi.git .
6
+ RUN npm config set registry https://registry.npmmirror.com && npm install
 
 
 
 
 
7
  RUN npm run build
8
 
9
+ # ========== 第二阶段:运行 ==========
10
  FROM node:20-slim AS runner
11
  WORKDIR /app
12
 
13
+ # 安装运行时依赖:cron, supervisor, python3, pip
14
+ RUN apt-get update && apt-get install -y \
15
+ cron \
16
+ supervisor \
17
+ python3 \
18
+ python3-pip \
19
+ && rm -rf /var/lib/apt/lists/*
20
 
21
+ # 安装 huggingface-hub(用于上传到数据集)
22
+ RUN pip3 install huggingface-hub --break-system-packages || pip3 install huggingface-hub
23
 
24
+ COPY --from=builder /app ./
25
+ RUN mkdir -p /data/freellm
26
  RUN cp -r client/dist/* server/dist/public/ 2>/dev/null || cp -r client/dist/* server/public/ 2>/dev/null || true
27
 
 
28
  EXPOSE 7860
29
  ENV PORT=7860
30
  ENV NODE_ENV=production
31
  ENV DATABASE_URL="file:/data/database.sqlite"
32
 
33
+ # 创建备份脚本目录
34
+ RUN mkdir -p /app/scripts /var/log/backup
35
+
36
+ # 复制备份脚本
37
+ COPY scripts/backup_to_dataset.py /app/scripts/backup_to_dataset.py
38
+ RUN chmod +x /app/scripts/backup_to_dataset.py
39
+
40
+ # 配置 cron 任务:每天凌晨3点执行备份(UTC时间)
41
+ RUN echo "0 3 * * * root /usr/bin/python3 /app/scripts/backup_to_dataset.py >> /var/log/backup/cron.log 2>&1" > /etc/cron.d/backup-job && \
42
+ chmod 0644 /etc/cron.d/backup-job
43
+
44
+ # 复制 supervisord 配置
45
+ COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
46
+
47
+ # 启动 supervisord(同时管理 node 应用和 cron)
48
+ CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]