tanbushi commited on
Commit
55285aa
·
1 Parent(s): 496b622
Files changed (1) hide show
  1. Dockerfile +37 -27
Dockerfile CHANGED
@@ -1,52 +1,62 @@
1
- # Use Node.js 20 LTS as base image
2
  FROM node:20-alpine
3
 
4
- # Create a non-root user first
5
- RUN addgroup -g 1001 -S opencode && \
6
- adduser -S opencode -u 1001
 
7
 
8
- # Set environment variables for user
9
- ENV PATH="/home/opencode/.local/bin:/usr/local/bin:$PATH"
10
  ENV NODE_ENV=production
11
  ENV PORT=7860
 
 
 
 
 
12
 
13
- # Install git and other system dependencies
14
  RUN apk add --no-cache git curl
15
 
16
- # Create workspace directory
17
  WORKDIR /app
18
 
19
- # Install opencode globally as root
20
  USER root
21
  RUN npm install -g opencode-ai@latest
22
 
23
- # Copy files and set ownership
 
24
  COPY --chown=opencode:opencode . /app
 
 
 
 
 
25
  RUN chown -R opencode:opencode /app && \
26
  chown -R opencode:opencode /home/opencode
27
 
28
- # Switch to non-root user
29
  USER opencode
30
 
31
- # Create user's bin directory
32
  RUN mkdir -p /home/opencode/.local/bin
33
 
34
- # Create startup script using cat with heredoc to avoid newline issues
35
- RUN cat > /home/opencode/.local/bin/start.sh << 'EOF'
36
- #!/bin/ash
37
- echo "Starting OpenCode AI Web Server..."
38
- echo "Server will be available at http://0.0.0.0:7860 "
39
- echo "OpenAPI documentation available at http://0.0.0.0:7860/doc "
40
- exec /usr/local/bin/opencode serve --hostname 0.0.0.0 --port 7860
41
- EOF
42
 
43
- RUN chmod +x /home/opencode/.local/bin/start.sh
44
-
45
- # 调试:验证脚本内容
46
- RUN cat -A /home/opencode/.local/bin/start.sh
47
-
48
- # Expose the port that HuggingFace Spaces expects
49
  EXPOSE 7860
50
 
51
- # 简化CMD,直接执行脚本
 
 
 
 
52
  CMD ["/home/opencode/.local/bin/start.sh"]
 
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
+ exec /usr/local/bin/opencode serve --hostname 0.0.0.0 --port 7860\n\
51
+ ' > /home/opencode/.local/bin/start.sh && \
52
+ chmod +x /home/opencode/.local/bin/start.sh
53
 
54
+ # 暴露应用程序端口[1](@ref)[2](@ref)
 
 
 
 
 
55
  EXPOSE 7860
56
 
57
+ # 健康检查[6](@ref)
58
+ HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
59
+ CMD curl -f http://localhost:7860/health || exit 1
60
+
61
+ # 设置容器启动命令[3](@ref)[5](@ref)
62
  CMD ["/home/opencode/.local/bin/start.sh"]