NitinBot002/coral-server
0
1# MCP-Compliant Dockerfile for Hugging Face Spaces deployment of Coral Server2 3# ==============================================================================4# Build Stage: Compile the Coral Server application5# ==============================================================================6FROM gradle:8.5-jdk21 AS build7 8# Set working directory9WORKDIR /app10 11# Clone the Coral Server repository12RUN git clone https://github.com/Coral-Protocol/coral-server.git .13 14# Build the application, skipping tests to speed up the build15RUN gradle build --no-daemon -x test16 17# Create configuration directory18RUN mkdir -p /coral-config19 20# Copy default configs if they exist, otherwise create minimal ones.21# Note: The server port here is set to 7860, but it will be overridden at runtime.22RUN if [ ! -f src/main/resources/application.yaml ]; then \23 echo "server:" > /coral-config/application.yaml && \24 echo " port: 7860" >> /coral-config/application.yaml && \25 echo "mcp:" >> /coral-config/application.yaml && \26 echo " transport: sse" >> /coral-config/application.yaml; \27 else \28 cp src/main/resources/application.yaml /coral-config/; \29 fi && \30 if [ ! -f src/main/resources/registry.toml ]; then \31 touch /coral-config/registry.toml; \32 else \33 cp src/main/resources/registry.toml /coral-config/; \34 fi35 36# ==============================================================================37# Runtime Stage: Set up the final container with Nginx and the Java app38# ==============================================================================39FROM openjdk:21-jdk-slim40 41# Install Nginx for reverse proxying42RUN apt-get update && apt-get install -y \43 curl \44 nginx \45 && rm -rf /var/lib/apt/lists/*46 47# Set working directory48WORKDIR /app49 50# Copy built application and configs from the build stage51COPY --from=build /app/build/libs/*.jar app.jar52COPY --from=build /coral-config /app/coral-config53 54# Create Nginx config to proxy requests from public port 7860 to the internal Coral Server port 555555RUN echo 'server {' > /etc/nginx/sites-available/mcp-proxy && \56 echo ' listen 7860;' >> /etc/nginx/sites-available/mcp-proxy && \57 echo ' server_name localhost;' >> /etc/nginx/sites-available/mcp-proxy && \58 echo '' >> /etc/nginx/sites-available/mcp-proxy && \59 echo ' # Proxy all locations to the Coral Server' >> /etc/nginx/sites-available/mcp-proxy && \60 echo ' location / {' >> /etc/nginx/sites-available/mcp-proxy && \61 echo ' proxy_pass http://127.0.0.1:5555;' >> /etc/nginx/sites-available/mcp-proxy && \62 echo ' proxy_http_version 1.1;' >> /etc/nginx/sites-available/mcp-proxy && \63 echo ' proxy_set_header Upgrade $http_upgrade;' >> /etc/nginx/sites-available/mcp-proxy && \64 echo ' proxy_set_header Connection "upgrade";' >> /etc/nginx/sites-available/mcp-proxy && \65 echo ' proxy_set_header Host $host;' >> /etc/nginx/sites-available/mcp-proxy && \66 echo ' proxy_cache_bypass $http_upgrade;' >> /etc/nginx/sites-available/mcp-proxy && \67 echo ' proxy_buffering off;' >> /etc/nginx/sites-available/mcp-proxy && \68 echo ' proxy_read_timeout 86400;' >> /etc/nginx/sites-available/mcp-proxy && \69 echo ' }' >> /etc/nginx/sites-available/mcp-proxy && \70 echo '}' >> /etc/nginx/sites-available/mcp-proxy && \71 ln -s /etc/nginx/sites-available/mcp-proxy /etc/nginx/sites-enabled/ && \72 rm /etc/nginx/sites-enabled/default73 74# Create a startup script to run both Nginx and the Coral Server75RUN echo '#!/bin/bash' > /app/start.sh && \76 echo 'set -e' >> /app/start.sh && \77 echo '' >> /app/start.sh && \78 echo '# Start Nginx in the background, listening on port 7860' >> /app/start.sh && \79 echo 'nginx -g "daemon off;" &' >> /app/start.sh && \80 echo 'NGINX_PID=$!' >> /app/start.sh && \81 echo '' >> /app/start.sh && \82 echo '# Start Coral Server, telling it to listen on internal port 5555' >> /app/start.sh && \83 echo 'java -Xmx1g -Xms512m -jar app.jar --sse-server 5555 &' >> /app/start.sh && \84 echo 'CORAL_PID=$!' >> /app/start.sh && \85 echo '' >> /app/start.sh && \86 echo '# Wait for either process to exit' >> /app/start.sh && \87 echo 'wait -n $NGINX_PID $CORAL_PID' >> /app/start.sh && \88 echo '' >> /app/start.sh && \89 echo '# Clean up both processes if one exits' >> /app/start.sh && \90 echo 'kill $NGINX_PID $CORAL_PID 2>/dev/null || true' >> /app/start.sh && \91 chmod +x /app/start.sh92 93# Create a non-root user as required by Hugging Face Spaces94RUN useradd -m -u 1000 user && \95 chown -R user:user /app && \96 chown -R user:user /var/log/nginx && \97 chown -R user:user /var/lib/nginx && \98 touch /var/run/nginx.pid && \99 chown user:user /var/run/nginx.pid100 101USER user102 103# Set environment variables104ENV HOME=/home/user \105 PATH=/home/user/.local/bin:$PATH \106 CONFIG_PATH=/app/coral-config/107 108# Expose the public port that Nginx is listening on109EXPOSE 7860110 111# Health check to ensure the service is running and accessible via the proxy112HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \113 CMD curl -f http://localhost:7860/devmode/exampleApplication/privkey/session1/sse || exit 1114 115# Start the application using the script116CMD ["/app/start.sh"]