tanbushi commited on
Commit
6c7338b
·
1 Parent(s): 6c1e251
Files changed (1) hide show
  1. Dockerfile +22 -46
Dockerfile CHANGED
@@ -1,68 +1,44 @@
1
- # 使用Node.js 20 LTS Alpine版本作为基础镜像[7](@ref)[8](@ref)
2
  FROM node:20-alpine
3
 
4
- # 设置元数据标签[2](@ref)[6](@ref)
5
- LABEL maintainer="your-email@example.com"
6
- LABEL description="OpenCode AI Web Application"
7
- LABEL version="1.0.0"
8
 
9
- # 设置环境变量[2](@ref)[5](@ref)
10
- ENV NODE_ENV=production
11
- ENV PORT=7860
12
- ENV PATH="/home/opencode/.local/bin:/usr/local/bin:$PATH"
13
 
14
- # 创建非root用户以提高安全性[7](@ref)[8](@ref)
15
  RUN addgroup -g 1001 -S opencode && \
16
  adduser -S opencode -u 1001
17
 
18
- # 安装必要的系统依赖[8](@ref)
19
- RUN apk add --no-cache git curl
 
 
 
 
 
20
 
21
- # 设置工作目录[1](@ref)[3](@ref)
22
  WORKDIR /app
23
-
24
- # 以root用户安装全局依赖[1](@ref)
25
- USER root
26
- RUN npm install -g opencode-ai@latest
27
-
28
- # 复制应用程序文件并设置权限[1](@ref)[4](@ref)
29
  COPY --chown=opencode:opencode package*.json ./
30
  COPY --chown=opencode:opencode . /app
 
31
 
32
- # 安装应用程序依赖(如果存在package.json)[4](@ref)
33
- RUN if [ -f "package.json" ]; then npm ci --only=production; fi
34
 
35
- # 设置文件所有权[8](@ref)
36
- RUN chown -R opencode:opencode /app && \
37
- chown -R opencode:opencode /home/opencode
38
-
39
- # 切换到非root用户[8](@ref)
40
  USER opencode
41
-
42
- # 创建用户目录
43
  RUN mkdir -p /home/opencode/.local/bin
44
-
45
- # 创建启动脚本 - 使用printf避免换行符问题
46
  RUN printf '#!/bin/sh\n\
47
- echo "Starting OpenCode AI Web Server..."\n\
48
- echo "Server will be available at http://0.0.0.0:7860 "\n\
49
- echo "OpenAPI documentation available at http://0.0.0.0:7860/doc "\n\
50
- echo "Current PATH before export: $PATH"\n\
51
- ls -l /usr/local/bin/opencode\n\
52
- ls -l /usr/local/lib/node_modules/opencode-ai/bin/opencode\n\
53
- export PATH="/usr/local/bin:$PATH"\n\
54
- echo "Current PATH after export: $PATH"\n\
55
- env\n\
56
- exec /usr/local/bin/opencode serve --hostname 0.0.0.0 --port 7860\n\
57
- ' > /home/opencode/.local/bin/start.sh && \
58
  chmod +x /home/opencode/.local/bin/start.sh
59
 
60
- # 暴露应用程序端口[1](@ref)[2](@ref)
61
  EXPOSE 7860
62
-
63
- # 健康检查[6](@ref)
64
  HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
65
  CMD curl -f http://localhost:7860/health || exit 1
66
 
67
- # 设置容器启动命令[3](@ref)[5](@ref)
68
- CMD ["/home/opencode/.local/bin/start.sh"]
 
1
+ # ---------- 1. 基础镜像 ----------
2
  FROM node:20-alpine
3
 
4
+ # ---------- 2. 让 Alpine 能跑 glibc 程序 ----------
5
+ RUN apk add --no-cache gcompat
 
 
6
 
7
+ # ---------- 3. 系统级依赖 ----------
8
+ RUN apk add --no-cache git curl
 
 
9
 
10
+ # ---------- 4. 创建非 root 用户 ----------
11
  RUN addgroup -g 1001 -S opencode && \
12
  adduser -S opencode -u 1001
13
 
14
+ # ---------- 5. 全局安装 opencode-ai ----------
15
+ # 安装完先验证二进制是否存在,若不存在就手动解压
16
+ RUN npm install -g opencode-ai@latest && \
17
+ if [ ! -f /usr/local/lib/node_modules/opencode-ai/node_modules/opencode-linux-x64/bin/opencode ]; then \
18
+ cd /usr/local/lib/node_modules/opencode-ai && \
19
+ npm run postinstall; \
20
+ fi
21
 
22
+ # ---------- 6. 工作目录 ----------
23
  WORKDIR /app
 
 
 
 
 
 
24
  COPY --chown=opencode:opencode package*.json ./
25
  COPY --chown=opencode:opencode . /app
26
+ RUN if [ -f package.json ]; then npm ci --only=production; fi
27
 
28
+ # ---------- 7. 修正权限 ----------
29
+ RUN chown -R opencode:opencode /app /home/opencode
30
 
31
+ # ---------- 8. 启动脚本 ----------
 
 
 
 
32
  USER opencode
 
 
33
  RUN mkdir -p /home/opencode/.local/bin
 
 
34
  RUN printf '#!/bin/sh\n\
35
+ echo "Starting OpenCode AI Web Server ..."\n\
36
+ exec /usr/local/bin/opencode serve --hostname 0.0.0.0 --port 7860\n' > /home/opencode/.local/bin/start.sh && \
 
 
 
 
 
 
 
 
 
37
  chmod +x /home/opencode/.local/bin/start.sh
38
 
39
+ # ---------- 9. 端口与健康检查 ----------
40
  EXPOSE 7860
 
 
41
  HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
42
  CMD curl -f http://localhost:7860/health || exit 1
43
 
44
+ CMD ["/home/opencode/.local/bin/start.sh"]