NitinBot002 commited on
Commit
46c51e5
·
verified ·
1 Parent(s): 2b0657a

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +62 -101
Dockerfile CHANGED
@@ -1,19 +1,27 @@
1
  # MCP-Compliant Dockerfile for Hugging Face Spaces deployment of Coral Server
 
 
 
 
2
  FROM gradle:8.5-jdk21 AS build
3
 
 
4
  WORKDIR /app
5
 
6
  # Clone the Coral Server repository
7
  RUN git clone https://github.com/Coral-Protocol/coral-server.git .
8
 
9
- # Build the application
10
  RUN gradle build --no-daemon -x test
11
 
12
- # Create configuration files
13
- RUN mkdir -p /coral-config && \
14
- if [ ! -f src/main/resources/application.yaml ]; then \
 
 
 
15
  echo "server:" > /coral-config/application.yaml && \
16
- echo " port: 5555" >> /coral-config/application.yaml && \
17
  echo "mcp:" >> /coral-config/application.yaml && \
18
  echo " transport: sse" >> /coral-config/application.yaml; \
19
  else \
@@ -25,131 +33,84 @@ RUN mkdir -p /coral-config && \
25
  cp src/main/resources/registry.toml /coral-config/; \
26
  fi
27
 
28
- # Runtime stage
 
 
29
  FROM openjdk:21-jdk-slim
30
 
31
- # Install required packages
32
  RUN apt-get update && apt-get install -y \
33
  curl \
34
- git \
35
  nginx \
36
  && rm -rf /var/lib/apt/lists/*
37
 
 
38
  WORKDIR /app
39
 
40
- # Copy built application from build stage
41
  COPY --from=build /app/build/libs/*.jar app.jar
42
- COPY --from=build /app/build /app/build
43
  COPY --from=build /coral-config /app/coral-config
44
- COPY --from=build /app/gradlew /app/gradlew
45
- COPY --from=build /app/gradle /app/gradle
46
-
47
- # Create user first (before modifying files)
48
- RUN useradd -m -u 1000 user
49
-
50
- # Configure nginx for non-root operation
51
- RUN mkdir -p /var/cache/nginx /var/log/nginx /var/lib/nginx /var/run && \
52
- chown -R user:user /var/cache/nginx /var/log/nginx /var/lib/nginx /var/run && \
53
- chmod -R 755 /var/cache/nginx /var/log/nginx /var/lib/nginx /var/run
54
-
55
- # Create nginx config that works with non-root user
56
- RUN echo 'daemon off;' > /etc/nginx/nginx.conf && \
57
- echo 'user user;' >> /etc/nginx/nginx.conf && \
58
- echo 'worker_processes 1;' >> /etc/nginx/nginx.conf && \
59
- echo 'error_log /var/log/nginx/error.log warn;' >> /etc/nginx/nginx.conf && \
60
- echo 'pid /var/run/nginx.pid;' >> /etc/nginx/nginx.conf && \
61
- echo '' >> /etc/nginx/nginx.conf && \
62
- echo 'events {' >> /etc/nginx/nginx.conf && \
63
- echo ' worker_connections 1024;' >> /etc/nginx/nginx.conf && \
64
- echo '}' >> /etc/nginx/nginx.conf && \
65
- echo '' >> /etc/nginx/nginx.conf && \
66
- echo 'http {' >> /etc/nginx/nginx.conf && \
67
- echo ' include /etc/nginx/mime.types;' >> /etc/nginx/nginx.conf && \
68
- echo ' default_type application/octet-stream;' >> /etc/nginx/nginx.conf && \
69
- echo ' sendfile on;' >> /etc/nginx/nginx.conf && \
70
- echo ' keepalive_timeout 65;' >> /etc/nginx/nginx.conf && \
71
- echo '' >> /etc/nginx/nginx.conf && \
72
- echo ' server {' >> /etc/nginx/nginx.conf && \
73
- echo ' listen 7860;' >> /etc/nginx/nginx.conf && \
74
- echo ' server_name _;' >> /etc/nginx/nginx.conf && \
75
- echo '' >> /etc/nginx/nginx.conf && \
76
- echo ' # Root endpoint' >> /etc/nginx/nginx.conf && \
77
- echo ' location = / {' >> /etc/nginx/nginx.conf && \
78
- echo ' return 200 "Coral Server is running";' >> /etc/nginx/nginx.conf && \
79
- echo ' add_header Content-Type text/plain;' >> /etc/nginx/nginx.conf && \
80
- echo ' }' >> /etc/nginx/nginx.conf && \
81
- echo '' >> /etc/nginx/nginx.conf && \
82
- echo ' # SSE endpoint for MCP' >> /etc/nginx/nginx.conf && \
83
- echo ' location /devmode/ {' >> /etc/nginx/nginx.conf && \
84
- echo ' proxy_pass http://127.0.0.1:5555;' >> /etc/nginx/nginx.conf && \
85
- echo ' proxy_http_version 1.1;' >> /etc/nginx/nginx.conf && \
86
- echo ' proxy_set_header Upgrade $http_upgrade;' >> /etc/nginx/nginx.conf && \
87
- echo ' proxy_set_header Connection "";' >> /etc/nginx/nginx.conf && \
88
- echo ' proxy_set_header Host $host;' >> /etc/nginx/nginx.conf && \
89
- echo ' proxy_set_header X-Real-IP $remote_addr;' >> /etc/nginx/nginx.conf && \
90
- echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> /etc/nginx/nginx.conf && \
91
- echo ' proxy_set_header X-Forwarded-Proto $scheme;' >> /etc/nginx/nginx.conf && \
92
- echo ' proxy_buffering off;' >> /etc/nginx/nginx.conf && \
93
- echo ' proxy_cache off;' >> /etc/nginx/nginx.conf && \
94
- echo ' proxy_read_timeout 86400;' >> /etc/nginx/nginx.conf && \
95
- echo ' chunked_transfer_encoding off;' >> /etc/nginx/nginx.conf && \
96
- echo ' }' >> /etc/nginx/nginx.conf && \
97
- echo '' >> /etc/nginx/nginx.conf && \
98
- echo ' # General proxy for other endpoints' >> /etc/nginx/nginx.conf && \
99
- echo ' location / {' >> /etc/nginx/nginx.conf && \
100
- echo ' proxy_pass http://127.0.0.1:5555;' >> /etc/nginx/nginx.conf && \
101
- echo ' proxy_http_version 1.1;' >> /etc/nginx/nginx.conf && \
102
- echo ' proxy_set_header Host $host;' >> /etc/nginx/nginx.conf && \
103
- echo ' proxy_set_header X-Real-IP $remote_addr;' >> /etc/nginx/nginx.conf && \
104
- echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> /etc/nginx/nginx.conf && \
105
- echo ' proxy_set_header X-Forwarded-Proto $scheme;' >> /etc/nginx/nginx.conf && \
106
- echo ' }' >> /etc/nginx/nginx.conf && \
107
- echo ' }' >> /etc/nginx/nginx.conf && \
108
- echo '}' >> /etc/nginx/nginx.conf
109
-
110
- # Create startup script
111
  RUN echo '#!/bin/bash' > /app/start.sh && \
112
  echo 'set -e' >> /app/start.sh && \
113
  echo '' >> /app/start.sh && \
114
- echo 'echo "Starting Coral Server on port 5555..."' >> /app/start.sh && \
115
- echo 'cd /app' >> /app/start.sh && \
 
116
  echo '' >> /app/start.sh && \
117
- echo '# Start Coral Server with Gradle (using SSE Ktor server)' >> /app/start.sh && \
118
- echo './gradlew run --args="--sse-server-ktor 5555" &' >> /app/start.sh && \
119
  echo 'CORAL_PID=$!' >> /app/start.sh && \
120
  echo '' >> /app/start.sh && \
121
- echo '# Wait for Coral Server to start' >> /app/start.sh && \
122
- echo 'sleep 10' >> /app/start.sh && \
123
- echo '' >> /app/start.sh && \
124
- echo '# Check if Coral Server is running' >> /app/start.sh && \
125
- echo 'if ! ps -p $CORAL_PID > /dev/null; then' >> /app/start.sh && \
126
- echo ' echo "Coral Server failed to start"' >> /app/start.sh && \
127
- echo ' exit 1' >> /app/start.sh && \
128
- echo 'fi' >> /app/start.sh && \
129
  echo '' >> /app/start.sh && \
130
- echo 'echo "Starting nginx on port 7860..."' >> /app/start.sh && \
131
- echo 'nginx' >> /app/start.sh && \
132
  chmod +x /app/start.sh
133
 
134
- # Set ownership
135
- RUN chown -R user:user /app && \
136
- chown -R user:user /etc/nginx && \
137
- chmod -R 755 /app
 
 
 
138
 
139
  USER user
140
 
141
  # Set environment variables
142
  ENV HOME=/home/user \
143
  PATH=/home/user/.local/bin:$PATH \
144
- CONFIG_PATH=/app/coral-config/ \
145
- GRADLE_USER_HOME=/home/user/.gradle
146
 
147
- # HF Spaces expects port 7860
148
  EXPOSE 7860
149
 
150
- # Health check
151
  HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
152
- CMD curl -f http://localhost:7860/ || exit 1
153
 
154
- # Start both nginx proxy and Coral Server
155
  CMD ["/app/start.sh"]
 
1
  # MCP-Compliant Dockerfile for Hugging Face Spaces deployment of Coral Server
2
+
3
+ # ==============================================================================
4
+ # Build Stage: Compile the Coral Server application
5
+ # ==============================================================================
6
  FROM gradle:8.5-jdk21 AS build
7
 
8
+ # Set working directory
9
  WORKDIR /app
10
 
11
  # Clone the Coral Server repository
12
  RUN git clone https://github.com/Coral-Protocol/coral-server.git .
13
 
14
+ # Build the application, skipping tests to speed up the build
15
  RUN gradle build --no-daemon -x test
16
 
17
+ # Create configuration directory
18
+ RUN mkdir -p /coral-config
19
+
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.
22
+ RUN 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 \
 
33
  cp src/main/resources/registry.toml /coral-config/; \
34
  fi
35
 
36
+ # ==============================================================================
37
+ # Runtime Stage: Set up the final container with Nginx and the Java app
38
+ # ==============================================================================
39
  FROM openjdk:21-jdk-slim
40
 
41
+ # Install Nginx for reverse proxying
42
  RUN apt-get update && apt-get install -y \
43
  curl \
 
44
  nginx \
45
  && rm -rf /var/lib/apt/lists/*
46
 
47
+ # Set working directory
48
  WORKDIR /app
49
 
50
+ # Copy built application and configs from the build stage
51
  COPY --from=build /app/build/libs/*.jar app.jar
 
52
  COPY --from=build /coral-config /app/coral-config
53
+
54
+ # Create Nginx config to proxy requests from public port 7860 to the internal Coral Server port 5555
55
+ RUN 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/default
73
+
74
+ # Create a startup script to run both Nginx and the Coral Server
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  RUN 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.sh
92
 
93
+ # Create a non-root user as required by Hugging Face Spaces
94
+ RUN 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.pid
100
 
101
  USER user
102
 
103
  # Set environment variables
104
  ENV 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 on
109
  EXPOSE 7860
110
 
111
+ # Health check to ensure the service is running and accessible via the proxy
112
  HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
113
+ CMD curl -f http://localhost:7860/devmode/exampleApplication/privkey/session1/sse || exit 1
114
 
115
+ # Start the application using the script
116
  CMD ["/app/start.sh"]