# Start with Ubuntu 24.04 (Noble) FROM ubuntu:noble # Avoid prompts from apt ENV DEBIAN_FRONTEND=noninteractive # Install essential packages and tools RUN apt-get update && apt-get install -y curl wget git nginx # Free advertising :) LABEL maintainer="sam@defact.org" \ description="Free multimodal inference api running in node via docker and HF serverless inference" \ usage="https://huggingface.co/spaces/DeFactOfficial/MMAPI" # Switch to root for system installations USER root # Set home to the user's home directory ENV HOME=/home/root \ PATH=/home/root/.local/bin:$PATH \ STATIC_SITE_ROOT=$HOME/code/public # Install Node.js 20 (using n instead of nodesource for better HF compatibility) RUN curl -L https://raw.githubusercontent.com/tj/n/master/bin/n -o n \ && bash n 20 \ && rm n \ && npm install -g npm@latest # pm2 is awesome... lets you run node.js scripts as services with zero configuration RUN npm install pm2 -g # Create working directory that matches HF Spaces expectations WORKDIR $HOME/code # Clone your repository (replace with your actual repo URL) #RUN git clone https://your-repo-url.git . # Copy the current directory contents into the container at $HOME/app setting the owner to the user ADD . $HOME/code COPY --chown=root . $HOME/code # INSTALL NPM PACKAGES # INSTALL FFMPEG TOOLING # FIRE UP API # Loading Dependencies RUN npm install RUN $HOME/code/ffmpeg_install.sh # Expose application's default port EXPOSE 7860 # Configure nginx RUN rm -f /etc/nginx/sites-enabled/default COPY <<-'EOF' /etc/nginx/sites-available/reverse-proxy.conf server { listen 7860; server_name localhost; # Specific to HF Spaces: Allow larger headers for their proxy setup large_client_header_buffers 4 32k; proxy_connect_timeout 600; proxy_send_timeout 600; proxy_read_timeout 600; send_timeout 600; # Additional headers specific to running behind HF Spaces proxy 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; proxy_set_header X-Forwarded-Host $host; client_max_body_size 50M; # the node.js api runs on localhost:6666 # here we tell nginx that requests to /API should forward there location /api/ { #rewrite ^/API/images/(.*) /$1 break; proxy_pass http://localhost:6666; proxy_buffering on; proxy_buffer_size 128k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; } #location /API/tts/ { # rewrite ^/API/tts/(.*) /$1 break; # proxy_pass http://localhost:5555; # proxy_http_version 1.1; # proxy_set_header Upgrade $http_upgrade; # proxy_set_header Connection "upgrade"; #} # Required for HF Spaces health checks location / { return 200 'OK'; add_header Content-Type text/plain; } } EOF RUN ln -s /etc/nginx/sites-available/reverse-proxy.conf /etc/nginx/sites-enabled/ # Create startup script with HF Spaces specific considerations COPY <<-'EOF' $HOME/code/start.sh #!/bin/bash # Print environment info for debugging echo "Container Environment:" echo "=====================" env | grep -i HF_ || true echo "=====================" echo "Stopping old services..." pm2 delete all # start the imaging + tts api echo "Starting the API" cd $HOME/code && pm2 start api.js # Start all services in background with logging #cd /code/service1 && ./run.sh > /var/log/service1.log 2>&1 & #cd /code/service2 && ./run.sh > /var/log/service2.log 2>&1 & #cd /code/service3 && ./run.sh > /var/log/service3.log 2>&1 & # Wait for services to start sleep 5 # Check service status echo "Running services:" pm2 list # Tail the logs in background #tail -f /var/log/*.log & # Start nginx in foreground nginx -g 'daemon off;' EOF #make our shell script runnable RUN chmod +x $HOME/code/start.sh # Start everything CMD ["$HOME/code/start.sh"] #ENTRYPOINT ["nodejs", "./api.js"]