neural-os / Dockerfile
da03
.
ba65ca0
raw
history blame
9.78 kB
# Use the official Python 3.9 image
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9
RUN apt-get update && apt-get install ffmpeg libsm6 libxext6 curl nginx sudo -y
# Set the working directory to /code
WORKDIR /code
# Copy the current directory contents into the container at /code
COPY ./requirements.txt /code/requirements.txt
# Install requirements.txt
RUN pip install pip==24.0
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
# Set up a new user named "user" with user ID 1000
RUN useradd -m -u 1000 user
# Create nginx configuration for WebSocket support using echo commands
RUN echo 'server {' > /etc/nginx/sites-available/default && \
echo ' listen 7860;' >> /etc/nginx/sites-available/default && \
echo ' server_name _;' >> /etc/nginx/sites-available/default && \
echo '' >> /etc/nginx/sites-available/default && \
echo ' # WebSocket support' >> /etc/nginx/sites-available/default && \
echo ' location /ws {' >> /etc/nginx/sites-available/default && \
echo ' proxy_pass http://localhost:8080/ws;' >> /etc/nginx/sites-available/default && \
echo ' proxy_http_version 1.1;' >> /etc/nginx/sites-available/default && \
echo ' proxy_set_header Upgrade $http_upgrade;' >> /etc/nginx/sites-available/default && \
echo ' proxy_set_header Connection "upgrade";' >> /etc/nginx/sites-available/default && \
echo ' proxy_set_header Host $host;' >> /etc/nginx/sites-available/default && \
echo ' proxy_set_header X-Real-IP $remote_addr;' >> /etc/nginx/sites-available/default && \
echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> /etc/nginx/sites-available/default && \
echo ' proxy_set_header X-Forwarded-Proto $scheme;' >> /etc/nginx/sites-available/default && \
echo ' proxy_read_timeout 86400;' >> /etc/nginx/sites-available/default && \
echo ' }' >> /etc/nginx/sites-available/default && \
echo '' >> /etc/nginx/sites-available/default && \
echo ' # Regular HTTP requests' >> /etc/nginx/sites-available/default && \
echo ' location / {' >> /etc/nginx/sites-available/default && \
echo ' proxy_pass http://localhost:8080;' >> /etc/nginx/sites-available/default && \
echo ' proxy_set_header Host $host;' >> /etc/nginx/sites-available/default && \
echo ' proxy_set_header X-Real-IP $remote_addr;' >> /etc/nginx/sites-available/default && \
echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> /etc/nginx/sites-available/default && \
echo ' proxy_set_header X-Forwarded-Proto $scheme;' >> /etc/nginx/sites-available/default && \
echo ' }' >> /etc/nginx/sites-available/default && \
echo '}' >> /etc/nginx/sites-available/default
# Create directories for nginx
RUN mkdir -p /run/nginx && chmod 755 /run/nginx
# Give user sudo permissions for nginx
RUN echo "user ALL=(ALL) NOPASSWD: /usr/sbin/nginx, /usr/sbin/nginx -t, /usr/sbin/nginx -s quit" >> /etc/sudoers
# Create a startup script that runs as root initially (while we're still root)
RUN echo '#!/bin/bash' > /start_hf_spaces.sh && \
echo 'set -e' >> /start_hf_spaces.sh && \
echo '' >> /start_hf_spaces.sh && \
echo 'echo "πŸš€ Starting Neural OS for HF Spaces with nginx proxy"' >> /start_hf_spaces.sh && \
echo 'echo "===================================="' >> /start_hf_spaces.sh && \
echo 'echo "πŸ“ Current directory: $(pwd)"' >> /start_hf_spaces.sh && \
echo 'cd /home/user/app' >> /start_hf_spaces.sh && \
echo 'echo "πŸ“‹ Files in current directory:"' >> /start_hf_spaces.sh && \
echo 'ls -la' >> /start_hf_spaces.sh && \
echo '' >> /start_hf_spaces.sh && \
echo '# Check if required files exist' >> /start_hf_spaces.sh && \
echo 'if [[ ! -f "dispatcher.py" ]]; then' >> /start_hf_spaces.sh && \
echo ' echo "❌ Error: dispatcher.py not found"' >> /start_hf_spaces.sh && \
echo ' exit 1' >> /start_hf_spaces.sh && \
echo 'fi' >> /start_hf_spaces.sh && \
echo '' >> /start_hf_spaces.sh && \
echo 'if [[ ! -f "worker.py" ]]; then' >> /start_hf_spaces.sh && \
echo ' echo "❌ Error: worker.py not found"' >> /start_hf_spaces.sh && \
echo ' exit 1' >> /start_hf_spaces.sh && \
echo 'fi' >> /start_hf_spaces.sh && \
echo '' >> /start_hf_spaces.sh && \
echo 'if [[ ! -f "static/index.html" ]]; then' >> /start_hf_spaces.sh && \
echo ' echo "❌ Error: static/index.html not found"' >> /start_hf_spaces.sh && \
echo ' exit 1' >> /start_hf_spaces.sh && \
echo 'fi' >> /start_hf_spaces.sh && \
echo '' >> /start_hf_spaces.sh && \
echo 'echo "βœ… All required files found"' >> /start_hf_spaces.sh && \
echo '' >> /start_hf_spaces.sh && \
echo '# Start nginx as root' >> /start_hf_spaces.sh && \
echo 'echo "🌐 Starting nginx proxy..."' >> /start_hf_spaces.sh && \
echo 'mkdir -p /run/nginx' >> /start_hf_spaces.sh && \
echo 'nginx -t && nginx' >> /start_hf_spaces.sh && \
echo '' >> /start_hf_spaces.sh && \
echo '# Start dispatcher as user using runuser' >> /start_hf_spaces.sh && \
echo 'echo "🎯 Starting dispatcher..."' >> /start_hf_spaces.sh && \
echo 'cd /home/user/app' >> /start_hf_spaces.sh && \
echo 'runuser -l user -c "cd /home/user/app && python dispatcher.py --port 8080" > dispatcher.log 2>&1 &' >> /start_hf_spaces.sh && \
echo 'DISPATCHER_PID=$!' >> /start_hf_spaces.sh && \
echo 'echo "πŸ“Š Dispatcher PID: $DISPATCHER_PID"' >> /start_hf_spaces.sh && \
echo '' >> /start_hf_spaces.sh && \
echo '# Wait for dispatcher to start' >> /start_hf_spaces.sh && \
echo 'echo "⏳ Waiting for dispatcher to initialize..."' >> /start_hf_spaces.sh && \
echo 'sleep 5' >> /start_hf_spaces.sh && \
echo '' >> /start_hf_spaces.sh && \
echo '# Test if dispatcher is responding' >> /start_hf_spaces.sh && \
echo 'echo "πŸ” Testing dispatcher health (via nginx)..."' >> /start_hf_spaces.sh && \
echo 'curl -f http://localhost:7860/ > /dev/null 2>&1' >> /start_hf_spaces.sh && \
echo 'if [ $? -eq 0 ]; then' >> /start_hf_spaces.sh && \
echo ' echo "βœ… Nginx proxy is working"' >> /start_hf_spaces.sh && \
echo 'else' >> /start_hf_spaces.sh && \
echo ' echo "⚠️ Nginx proxy test failed"' >> /start_hf_spaces.sh && \
echo 'fi' >> /start_hf_spaces.sh && \
echo '' >> /start_hf_spaces.sh && \
echo '# Start worker as user using runuser' >> /start_hf_spaces.sh && \
echo 'echo "πŸ”§ Starting worker..."' >> /start_hf_spaces.sh && \
echo 'runuser -l user -c "cd /home/user/app && python worker.py --worker-address localhost:8001 --dispatcher-url http://localhost:8080" > worker.log 2>&1 &' >> /start_hf_spaces.sh && \
echo 'WORKER_PID=$!' >> /start_hf_spaces.sh && \
echo 'echo "πŸ“Š Worker PID: $WORKER_PID"' >> /start_hf_spaces.sh && \
echo '' >> /start_hf_spaces.sh && \
echo '# Wait for worker to initialize' >> /start_hf_spaces.sh && \
echo 'echo "⏳ Waiting for worker to initialize..."' >> /start_hf_spaces.sh && \
echo 'sleep 30' >> /start_hf_spaces.sh && \
echo '' >> /start_hf_spaces.sh && \
echo '# Check if worker is running' >> /start_hf_spaces.sh && \
echo 'if ! kill -0 $WORKER_PID 2>/dev/null; then' >> /start_hf_spaces.sh && \
echo ' echo "❌ Worker failed to start"' >> /start_hf_spaces.sh && \
echo ' echo "πŸ“‹ Worker log:"' >> /start_hf_spaces.sh && \
echo ' tail -n 50 worker.log' >> /start_hf_spaces.sh && \
echo ' echo "πŸ“‹ Dispatcher log:"' >> /start_hf_spaces.sh && \
echo ' tail -n 50 dispatcher.log' >> /start_hf_spaces.sh && \
echo ' exit 1' >> /start_hf_spaces.sh && \
echo 'fi' >> /start_hf_spaces.sh && \
echo '' >> /start_hf_spaces.sh && \
echo 'echo "βœ… System ready!"' >> /start_hf_spaces.sh && \
echo 'echo "🌍 Web interface: http://localhost:7860 (via nginx)"' >> /start_hf_spaces.sh && \
echo 'echo "🎯 Dispatcher: http://localhost:8080 (direct)"' >> /start_hf_spaces.sh && \
echo 'echo "πŸ“Š Dispatcher PID: $DISPATCHER_PID"' >> /start_hf_spaces.sh && \
echo 'echo "πŸ“Š Worker PID: $WORKER_PID"' >> /start_hf_spaces.sh && \
echo '' >> /start_hf_spaces.sh && \
echo '# Function to cleanup' >> /start_hf_spaces.sh && \
echo 'cleanup() {' >> /start_hf_spaces.sh && \
echo ' echo "πŸ›‘ Shutting down..."' >> /start_hf_spaces.sh && \
echo ' kill $DISPATCHER_PID $WORKER_PID 2>/dev/null || true' >> /start_hf_spaces.sh && \
echo ' nginx -s quit 2>/dev/null || true' >> /start_hf_spaces.sh && \
echo ' exit 0' >> /start_hf_spaces.sh && \
echo '}' >> /start_hf_spaces.sh && \
echo '' >> /start_hf_spaces.sh && \
echo 'trap cleanup SIGINT SIGTERM' >> /start_hf_spaces.sh && \
echo '' >> /start_hf_spaces.sh && \
echo '# Keep the script running' >> /start_hf_spaces.sh && \
echo 'echo "πŸ“‹ Following logs (Ctrl+C to stop):"' >> /start_hf_spaces.sh && \
echo 'tail -f dispatcher.log worker.log &' >> /start_hf_spaces.sh && \
echo 'TAIL_PID=$!' >> /start_hf_spaces.sh && \
echo 'wait $DISPATCHER_PID' >> /start_hf_spaces.sh && \
echo 'kill $TAIL_PID 2>/dev/null || true' >> /start_hf_spaces.sh && \
chmod +x /start_hf_spaces.sh
# Switch to user and copy files
USER user
# Set home to the user's home directory
ENV HOME=/home/user \
PATH=/home/user/.local/bin:$PATH
# Set the working directory to the user's home directory
WORKDIR $HOME/app
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
COPY --chown=user . $HOME/app
# Switch back to root for final startup
USER root
CMD ["/start_hf_spaces.sh"]