File size: 2,291 Bytes
80eea56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
FROM ubuntu:22.04

# 避免交互式配置
ENV DEBIAN_FRONTEND=noninteractive

# 安装基础包
RUN apt-get update && apt-get install -y \
    curl \
    wget \
    git \
    nginx \
    python3 \
    python3-pip \
    nodejs \
    npm \
    && rm -rf /var/lib/apt/lists/*

# 安装code-server
RUN curl -fsSL https://code-server.dev/install.sh | sh

# 安装cloudflared
RUN wget https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb \
    && dpkg -i cloudflared-linux-amd64.deb \
    && rm cloudflared-linux-amd64.deb

# 创建用户 (HF Spaces要求)
RUN useradd -m -u 1000 user
USER user
WORKDIR /home/user

# 配置code-server
RUN mkdir -p /home/user/.config/code-server/ \
    && echo -e "bind-addr: 0.0.0.0:7860\nauth: password\ncert: false" > /home/user/.config/code-server/config.yaml

# 切换回root配置nginx
USER root

# 创建nginx配置
RUN mkdir -p /var/www/html
RUN echo '<html><head><title>VSCode Server</title></head><body><h1>Welcome</h1><p>VSCode Server is running!</p></body></html>' > /var/www/html/index.html

RUN echo 'pid /home/user/nginx.pid;\
error_log /home/user/nginx_error.log;\
\
events {\
    worker_connections 1024;\
}\
\
http {\
    include /etc/nginx/mime.types;\
    default_type application/octet-stream;\
    \
    access_log /home/user/nginx_access.log;\
    \
    server {\
        listen 8080;\
        server_name _;\
        root /var/www/html;\
        index index.html;\
        \
        location / {\
            try_files $uri $uri/ =404;\
        }\
        \
        location /admin-vscode {\
            proxy_pass http://127.0.0.1:7860;\
            proxy_set_header Host $host;\
            proxy_set_header X-Real-IP $remote_addr;\
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\
            proxy_set_header X-Forwarded-Proto $scheme;\
        }\
    }\
}' > /etc/nginx/nginx.conf

# 设置权限
RUN chown -R user:user /var/www/html /home/user
RUN chmod -R 755 /var/www/html

# 复制启动脚本
COPY start-services-minimal.sh /usr/bin/start-services.sh
RUN chmod +x /usr/bin/start-services.sh

# 切换回用户
USER user

# 设置环境变量
ENV HOME=/home/user \
    PATH=/home/user/.local/bin:$PATH

EXPOSE 7860
EXPOSE 8080

CMD ["/usr/bin/start-services.sh"]