Spaces:
Runtime error
Runtime error
da03
commited on
Commit
Β·
dd0c9d5
1
Parent(s):
e733937
- Dockerfile +100 -8
Dockerfile
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
# Use the official Python 3.9 image
|
2 |
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9
|
3 |
|
4 |
-
RUN apt-get update && apt-get install ffmpeg libsm6 libxext6 curl -y
|
5 |
|
6 |
# Set the working directory to /code
|
7 |
WORKDIR /code
|
@@ -15,6 +15,11 @@ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
|
15 |
|
16 |
# Set up a new user named "user" with user ID 1000
|
17 |
RUN useradd -m -u 1000 user
|
|
|
|
|
|
|
|
|
|
|
18 |
# Switch to the "user" user
|
19 |
USER user
|
20 |
# Set home to the user's home directory
|
@@ -27,16 +32,72 @@ WORKDIR $HOME/app
|
|
27 |
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
28 |
COPY --chown=user . $HOME/app
|
29 |
|
30 |
-
# Create
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
RUN echo '#!/bin/bash' > start_hf_spaces.sh && \
|
32 |
echo 'set -e' >> start_hf_spaces.sh && \
|
33 |
echo '' >> start_hf_spaces.sh && \
|
34 |
-
echo 'echo "π Starting Neural OS for HF Spaces
|
35 |
echo 'echo "===================================="' >> start_hf_spaces.sh && \
|
36 |
echo '' >> start_hf_spaces.sh && \
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
echo '# Start dispatcher' >> start_hf_spaces.sh && \
|
38 |
echo 'echo "π― Starting dispatcher..."' >> start_hf_spaces.sh && \
|
39 |
-
echo 'python dispatcher.py --port
|
40 |
echo 'DISPATCHER_PID=$!' >> start_hf_spaces.sh && \
|
41 |
echo 'echo "π Dispatcher PID: $DISPATCHER_PID"' >> start_hf_spaces.sh && \
|
42 |
echo '' >> start_hf_spaces.sh && \
|
@@ -44,9 +105,19 @@ RUN echo '#!/bin/bash' > start_hf_spaces.sh && \
|
|
44 |
echo 'echo "β³ Waiting for dispatcher to initialize..."' >> start_hf_spaces.sh && \
|
45 |
echo 'sleep 5' >> start_hf_spaces.sh && \
|
46 |
echo '' >> start_hf_spaces.sh && \
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
echo '# Start worker' >> start_hf_spaces.sh && \
|
48 |
echo 'echo "π§ Starting worker..."' >> start_hf_spaces.sh && \
|
49 |
-
echo 'python worker.py --worker-address localhost:8001 --dispatcher-url http://localhost:
|
50 |
echo 'WORKER_PID=$!' >> start_hf_spaces.sh && \
|
51 |
echo 'echo "π Worker PID: $WORKER_PID"' >> start_hf_spaces.sh && \
|
52 |
echo '' >> start_hf_spaces.sh && \
|
@@ -54,11 +125,32 @@ RUN echo '#!/bin/bash' > start_hf_spaces.sh && \
|
|
54 |
echo 'echo "β³ Waiting for worker to initialize..."' >> start_hf_spaces.sh && \
|
55 |
echo 'sleep 30' >> start_hf_spaces.sh && \
|
56 |
echo '' >> start_hf_spaces.sh && \
|
57 |
-
echo '
|
58 |
-
echo '
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
echo '' >> start_hf_spaces.sh && \
|
60 |
echo '# Keep running' >> start_hf_spaces.sh && \
|
61 |
-
echo '
|
|
|
62 |
echo 'wait $DISPATCHER_PID' >> start_hf_spaces.sh && \
|
63 |
chmod +x start_hf_spaces.sh
|
64 |
|
|
|
1 |
# Use the official Python 3.9 image
|
2 |
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9
|
3 |
|
4 |
+
RUN apt-get update && apt-get install ffmpeg libsm6 libxext6 curl nginx -y
|
5 |
|
6 |
# Set the working directory to /code
|
7 |
WORKDIR /code
|
|
|
15 |
|
16 |
# Set up a new user named "user" with user ID 1000
|
17 |
RUN useradd -m -u 1000 user
|
18 |
+
|
19 |
+
# Create directories for nginx that user can write to
|
20 |
+
RUN mkdir -p /home/user/nginx/logs /home/user/nginx/cache /home/user/nginx/tmp && \
|
21 |
+
chown -R user:user /home/user/nginx
|
22 |
+
|
23 |
# Switch to the "user" user
|
24 |
USER user
|
25 |
# Set home to the user's home directory
|
|
|
32 |
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
33 |
COPY --chown=user . $HOME/app
|
34 |
|
35 |
+
# Create custom nginx.conf for non-root operation
|
36 |
+
RUN echo 'pid /home/user/nginx/nginx.pid;' > /home/user/nginx.conf && \
|
37 |
+
echo 'error_log /home/user/nginx/logs/error.log;' >> /home/user/nginx.conf && \
|
38 |
+
echo 'worker_processes 1;' >> /home/user/nginx.conf && \
|
39 |
+
echo '' >> /home/user/nginx.conf && \
|
40 |
+
echo 'events {' >> /home/user/nginx.conf && \
|
41 |
+
echo ' worker_connections 1024;' >> /home/user/nginx.conf && \
|
42 |
+
echo '}' >> /home/user/nginx.conf && \
|
43 |
+
echo '' >> /home/user/nginx.conf && \
|
44 |
+
echo 'http {' >> /home/user/nginx.conf && \
|
45 |
+
echo ' access_log /home/user/nginx/logs/access.log;' >> /home/user/nginx.conf && \
|
46 |
+
echo ' client_body_temp_path /home/user/nginx/tmp/client_body;' >> /home/user/nginx.conf && \
|
47 |
+
echo ' proxy_temp_path /home/user/nginx/tmp/proxy;' >> /home/user/nginx.conf && \
|
48 |
+
echo ' fastcgi_temp_path /home/user/nginx/tmp/fastcgi;' >> /home/user/nginx.conf && \
|
49 |
+
echo ' uwsgi_temp_path /home/user/nginx/tmp/uwsgi;' >> /home/user/nginx.conf && \
|
50 |
+
echo ' scgi_temp_path /home/user/nginx/tmp/scgi;' >> /home/user/nginx.conf && \
|
51 |
+
echo '' >> /home/user/nginx.conf && \
|
52 |
+
echo ' server {' >> /home/user/nginx.conf && \
|
53 |
+
echo ' listen 7860;' >> /home/user/nginx.conf && \
|
54 |
+
echo ' server_name localhost;' >> /home/user/nginx.conf && \
|
55 |
+
echo '' >> /home/user/nginx.conf && \
|
56 |
+
echo ' # WebSocket support' >> /home/user/nginx.conf && \
|
57 |
+
echo ' location /ws {' >> /home/user/nginx.conf && \
|
58 |
+
echo ' proxy_pass http://localhost:8080/ws;' >> /home/user/nginx.conf && \
|
59 |
+
echo ' proxy_http_version 1.1;' >> /home/user/nginx.conf && \
|
60 |
+
echo ' proxy_set_header Upgrade $http_upgrade;' >> /home/user/nginx.conf && \
|
61 |
+
echo ' proxy_set_header Connection "upgrade";' >> /home/user/nginx.conf && \
|
62 |
+
echo ' proxy_set_header Host $host;' >> /home/user/nginx.conf && \
|
63 |
+
echo ' proxy_set_header X-Real-IP $remote_addr;' >> /home/user/nginx.conf && \
|
64 |
+
echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> /home/user/nginx.conf && \
|
65 |
+
echo ' proxy_set_header X-Forwarded-Proto $scheme;' >> /home/user/nginx.conf && \
|
66 |
+
echo ' proxy_read_timeout 86400;' >> /home/user/nginx.conf && \
|
67 |
+
echo ' proxy_send_timeout 86400;' >> /home/user/nginx.conf && \
|
68 |
+
echo ' }' >> /home/user/nginx.conf && \
|
69 |
+
echo '' >> /home/user/nginx.conf && \
|
70 |
+
echo ' # Regular HTTP requests' >> /home/user/nginx.conf && \
|
71 |
+
echo ' location / {' >> /home/user/nginx.conf && \
|
72 |
+
echo ' proxy_pass http://localhost:8080;' >> /home/user/nginx.conf && \
|
73 |
+
echo ' proxy_set_header Host $host;' >> /home/user/nginx.conf && \
|
74 |
+
echo ' proxy_set_header X-Real-IP $remote_addr;' >> /home/user/nginx.conf && \
|
75 |
+
echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> /home/user/nginx.conf && \
|
76 |
+
echo ' proxy_set_header X-Forwarded-Proto $scheme;' >> /home/user/nginx.conf && \
|
77 |
+
echo ' }' >> /home/user/nginx.conf && \
|
78 |
+
echo ' }' >> /home/user/nginx.conf && \
|
79 |
+
echo '}' >> /home/user/nginx.conf
|
80 |
+
|
81 |
+
# Create necessary temp directories
|
82 |
+
RUN mkdir -p /home/user/nginx/tmp/{client_body,proxy,fastcgi,uwsgi,scgi}
|
83 |
+
|
84 |
+
# Create startup script
|
85 |
RUN echo '#!/bin/bash' > start_hf_spaces.sh && \
|
86 |
echo 'set -e' >> start_hf_spaces.sh && \
|
87 |
echo '' >> start_hf_spaces.sh && \
|
88 |
+
echo 'echo "π Starting Neural OS for HF Spaces with nginx proxy"' >> start_hf_spaces.sh && \
|
89 |
echo 'echo "===================================="' >> start_hf_spaces.sh && \
|
90 |
echo '' >> start_hf_spaces.sh && \
|
91 |
+
echo '# Start nginx as user' >> start_hf_spaces.sh && \
|
92 |
+
echo 'echo "π Starting nginx proxy..."' >> start_hf_spaces.sh && \
|
93 |
+
echo 'nginx -c /home/user/nginx.conf -t' >> start_hf_spaces.sh && \
|
94 |
+
echo 'nginx -c /home/user/nginx.conf &' >> start_hf_spaces.sh && \
|
95 |
+
echo 'NGINX_PID=$!' >> start_hf_spaces.sh && \
|
96 |
+
echo 'echo "π Nginx PID: $NGINX_PID"' >> start_hf_spaces.sh && \
|
97 |
+
echo '' >> start_hf_spaces.sh && \
|
98 |
echo '# Start dispatcher' >> start_hf_spaces.sh && \
|
99 |
echo 'echo "π― Starting dispatcher..."' >> start_hf_spaces.sh && \
|
100 |
+
echo 'python dispatcher.py --port 8080 > dispatcher.log 2>&1 &' >> start_hf_spaces.sh && \
|
101 |
echo 'DISPATCHER_PID=$!' >> start_hf_spaces.sh && \
|
102 |
echo 'echo "π Dispatcher PID: $DISPATCHER_PID"' >> start_hf_spaces.sh && \
|
103 |
echo '' >> start_hf_spaces.sh && \
|
|
|
105 |
echo 'echo "β³ Waiting for dispatcher to initialize..."' >> start_hf_spaces.sh && \
|
106 |
echo 'sleep 5' >> start_hf_spaces.sh && \
|
107 |
echo '' >> start_hf_spaces.sh && \
|
108 |
+
echo '# Test if everything is working' >> start_hf_spaces.sh && \
|
109 |
+
echo 'echo "π Testing system health..."' >> start_hf_spaces.sh && \
|
110 |
+
echo 'curl -f http://localhost:7860/ > /dev/null 2>&1' >> start_hf_spaces.sh && \
|
111 |
+
echo 'if [ $? -eq 0 ]; then' >> start_hf_spaces.sh && \
|
112 |
+
echo ' echo "β
System is responding"' >> start_hf_spaces.sh && \
|
113 |
+
echo 'else' >> start_hf_spaces.sh && \
|
114 |
+
echo ' echo "β System health check failed"' >> start_hf_spaces.sh && \
|
115 |
+
echo ' exit 1' >> start_hf_spaces.sh && \
|
116 |
+
echo 'fi' >> start_hf_spaces.sh && \
|
117 |
+
echo '' >> start_hf_spaces.sh && \
|
118 |
echo '# Start worker' >> start_hf_spaces.sh && \
|
119 |
echo 'echo "π§ Starting worker..."' >> start_hf_spaces.sh && \
|
120 |
+
echo 'python worker.py --worker-address localhost:8001 --dispatcher-url http://localhost:8080 > worker.log 2>&1 &' >> start_hf_spaces.sh && \
|
121 |
echo 'WORKER_PID=$!' >> start_hf_spaces.sh && \
|
122 |
echo 'echo "π Worker PID: $WORKER_PID"' >> start_hf_spaces.sh && \
|
123 |
echo '' >> start_hf_spaces.sh && \
|
|
|
125 |
echo 'echo "β³ Waiting for worker to initialize..."' >> start_hf_spaces.sh && \
|
126 |
echo 'sleep 30' >> start_hf_spaces.sh && \
|
127 |
echo '' >> start_hf_spaces.sh && \
|
128 |
+
echo '# Final health check' >> start_hf_spaces.sh && \
|
129 |
+
echo 'if kill -0 $DISPATCHER_PID 2>/dev/null && kill -0 $WORKER_PID 2>/dev/null; then' >> start_hf_spaces.sh && \
|
130 |
+
echo ' echo "β
System ready!"' >> start_hf_spaces.sh && \
|
131 |
+
echo ' echo "π Web interface: http://localhost:7860"' >> start_hf_spaces.sh && \
|
132 |
+
echo ' echo "π Dispatcher: http://localhost:8080"' >> start_hf_spaces.sh && \
|
133 |
+
echo 'else' >> start_hf_spaces.sh && \
|
134 |
+
echo ' echo "β System failed to start properly"' >> start_hf_spaces.sh && \
|
135 |
+
echo ' echo "π Dispatcher log:"' >> start_hf_spaces.sh && \
|
136 |
+
echo ' tail -n 20 dispatcher.log' >> start_hf_spaces.sh && \
|
137 |
+
echo ' echo "π Worker log:"' >> start_hf_spaces.sh && \
|
138 |
+
echo ' tail -n 20 worker.log' >> start_hf_spaces.sh && \
|
139 |
+
echo ' exit 1' >> start_hf_spaces.sh && \
|
140 |
+
echo 'fi' >> start_hf_spaces.sh && \
|
141 |
+
echo '' >> start_hf_spaces.sh && \
|
142 |
+
echo '# Function to cleanup' >> start_hf_spaces.sh && \
|
143 |
+
echo 'cleanup() {' >> start_hf_spaces.sh && \
|
144 |
+
echo ' echo "π Shutting down..."' >> start_hf_spaces.sh && \
|
145 |
+
echo ' kill $NGINX_PID $DISPATCHER_PID $WORKER_PID 2>/dev/null || true' >> start_hf_spaces.sh && \
|
146 |
+
echo ' exit 0' >> start_hf_spaces.sh && \
|
147 |
+
echo '}' >> start_hf_spaces.sh && \
|
148 |
+
echo '' >> start_hf_spaces.sh && \
|
149 |
+
echo 'trap cleanup SIGINT SIGTERM' >> start_hf_spaces.sh && \
|
150 |
echo '' >> start_hf_spaces.sh && \
|
151 |
echo '# Keep running' >> start_hf_spaces.sh && \
|
152 |
+
echo 'echo "π Following logs (Ctrl+C to stop):"' >> start_hf_spaces.sh && \
|
153 |
+
echo 'tail -f dispatcher.log worker.log /home/user/nginx/logs/error.log &' >> start_hf_spaces.sh && \
|
154 |
echo 'wait $DISPATCHER_PID' >> start_hf_spaces.sh && \
|
155 |
chmod +x start_hf_spaces.sh
|
156 |
|