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

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +92 -62
Dockerfile CHANGED
@@ -1,7 +1,6 @@
1
  # MCP-Compliant Dockerfile for Hugging Face Spaces deployment of Coral Server
2
  FROM gradle:8.5-jdk21 AS build
3
 
4
- # Set working directory
5
  WORKDIR /app
6
 
7
  # Clone the Coral Server repository
@@ -10,11 +9,11 @@ RUN git clone https://github.com/Coral-Protocol/coral-server.git .
10
  # Build the application
11
  RUN gradle build --no-daemon -x test
12
 
13
- # Create configuration files if they don't exist
14
  RUN mkdir -p /coral-config && \
15
  if [ ! -f src/main/resources/application.yaml ]; then \
16
  echo "server:" > /coral-config/application.yaml && \
17
- echo " port: 7860" >> /coral-config/application.yaml && \
18
  echo "mcp:" >> /coral-config/application.yaml && \
19
  echo " transport: sse" >> /coral-config/application.yaml; \
20
  else \
@@ -29,97 +28,128 @@ RUN mkdir -p /coral-config && \
29
  # Runtime stage
30
  FROM openjdk:21-jdk-slim
31
 
32
- # Install required packages including nginx for reverse proxy
33
  RUN apt-get update && apt-get install -y \
34
  curl \
35
  git \
36
  nginx \
37
  && rm -rf /var/lib/apt/lists/*
38
 
39
- # Set working directory
40
  WORKDIR /app
41
 
42
  # Copy built application from build stage
43
  COPY --from=build /app/build/libs/*.jar app.jar
 
44
  COPY --from=build /coral-config /app/coral-config
45
-
46
- # Create nginx config for MCP proxying
47
- RUN echo 'server {' > /etc/nginx/sites-available/mcp-proxy && \
48
- echo ' listen 7860;' >> /etc/nginx/sites-available/mcp-proxy && \
49
- echo ' server_name localhost;' >> /etc/nginx/sites-available/mcp-proxy && \
50
- echo '' >> /etc/nginx/sites-available/mcp-proxy && \
51
- echo ' # MCP SSE endpoint' >> /etc/nginx/sites-available/mcp-proxy && \
52
- echo ' location /sse {' >> /etc/nginx/sites-available/mcp-proxy && \
53
- echo ' proxy_pass http://127.0.0.1:5555;' >> /etc/nginx/sites-available/mcp-proxy && \
54
- echo ' proxy_http_version 1.1;' >> /etc/nginx/sites-available/mcp-proxy && \
55
- echo ' proxy_set_header Upgrade $http_upgrade;' >> /etc/nginx/sites-available/mcp-proxy && \
56
- echo ' proxy_set_header Connection "upgrade";' >> /etc/nginx/sites-available/mcp-proxy && \
57
- echo ' proxy_set_header Host $host;' >> /etc/nginx/sites-available/mcp-proxy && \
58
- echo ' proxy_cache_bypass $http_upgrade;' >> /etc/nginx/sites-available/mcp-proxy && \
59
- echo ' proxy_buffering off;' >> /etc/nginx/sites-available/mcp-proxy && \
60
- echo ' proxy_read_timeout 86400;' >> /etc/nginx/sites-available/mcp-proxy && \
61
- echo ' }' >> /etc/nginx/sites-available/mcp-proxy && \
62
- echo '' >> /etc/nginx/sites-available/mcp-proxy && \
63
- echo ' # Proxy all MCP requests to Coral Server' >> /etc/nginx/sites-available/mcp-proxy && \
64
- echo ' location / {' >> /etc/nginx/sites-available/mcp-proxy && \
65
- echo ' proxy_pass http://127.0.0.1:5555;' >> /etc/nginx/sites-available/mcp-proxy && \
66
- echo ' proxy_http_version 1.1;' >> /etc/nginx/sites-available/mcp-proxy && \
67
- echo ' proxy_set_header Upgrade $http_upgrade;' >> /etc/nginx/sites-available/mcp-proxy && \
68
- echo ' proxy_set_header Connection "upgrade";' >> /etc/nginx/sites-available/mcp-proxy && \
69
- echo ' proxy_set_header Host $host;' >> /etc/nginx/sites-available/mcp-proxy && \
70
- echo ' proxy_set_header X-Real-IP $remote_addr;' >> /etc/nginx/sites-available/mcp-proxy && \
71
- echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> /etc/nginx/sites-available/mcp-proxy && \
72
- echo ' proxy_set_header X-Forwarded-Proto $scheme;' >> /etc/nginx/sites-available/mcp-proxy && \
73
- echo ' proxy_cache_bypass $http_upgrade;' >> /etc/nginx/sites-available/mcp-proxy && \
74
- echo ' proxy_buffering off;' >> /etc/nginx/sites-available/mcp-proxy && \
75
- echo ' proxy_read_timeout 86400;' >> /etc/nginx/sites-available/mcp-proxy && \
76
- echo ' }' >> /etc/nginx/sites-available/mcp-proxy && \
77
- echo '}' >> /etc/nginx/sites-available/mcp-proxy && \
78
- ln -s /etc/nginx/sites-available/mcp-proxy /etc/nginx/sites-enabled/ && \
79
- rm /etc/nginx/sites-enabled/default
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
  # Create startup script
82
  RUN echo '#!/bin/bash' > /app/start.sh && \
83
  echo 'set -e' >> /app/start.sh && \
84
  echo '' >> /app/start.sh && \
85
- echo '# Start nginx in background' >> /app/start.sh && \
86
- echo 'nginx -g "daemon off;" &' >> /app/start.sh && \
87
- echo 'NGINX_PID=$!' >> /app/start.sh && \
88
  echo '' >> /app/start.sh && \
89
- echo '# Start Coral Server on port 5555' >> /app/start.sh && \
90
- echo 'java -Xmx1g -Xms512m -jar app.jar --sse-server 5555 &' >> /app/start.sh && \
91
  echo 'CORAL_PID=$!' >> /app/start.sh && \
92
  echo '' >> /app/start.sh && \
93
- echo '# Wait for either process to exit' >> /app/start.sh && \
94
- echo 'wait -n $NGINX_PID $CORAL_PID' >> /app/start.sh && \
 
 
 
 
 
 
95
  echo '' >> /app/start.sh && \
96
- echo '# Kill both processes if one exits' >> /app/start.sh && \
97
- echo 'kill $NGINX_PID $CORAL_PID 2>/dev/null || true' >> /app/start.sh && \
98
  chmod +x /app/start.sh
99
 
100
- # Create non-root user (required by HF Spaces)
101
- RUN useradd -m -u 1000 user && \
102
- chown -R user:user /app && \
103
- chown -R user:user /var/log/nginx && \
104
- chown -R user:user /var/lib/nginx && \
105
- touch /var/run/nginx.pid && \
106
- chown user:user /var/run/nginx.pid
107
 
108
  USER user
109
 
110
- # Set environment variables for HF Spaces
111
  ENV HOME=/home/user \
112
  PATH=/home/user/.local/bin:$PATH \
113
- GRADIO_SERVER_NAME=0.0.0.0 \
114
- GRADIO_SERVER_PORT=7860 \
115
- CONFIG_PATH=/app/coral-config/
116
 
117
  # HF Spaces expects port 7860
118
  EXPOSE 7860
119
 
120
- # Health check for the proxied service
121
  HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
122
- CMD curl -f http://localhost:7860/devmode/exampleApplication/privkey/session1/sse || exit 1
123
 
124
  # Start both nginx proxy and Coral Server
125
  CMD ["/app/start.sh"]
 
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
 
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 \
 
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"]