Skip to content

Commit 4ad18c7

Browse files
authored
Update Dockerfile
1 parent d240c46 commit 4ad18c7

File tree

1 file changed

+113
-2
lines changed

1 file changed

+113
-2
lines changed

Dockerfile

Lines changed: 113 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,121 @@
11
FROM node:22-alpine
2+
23
ARG VERSION=latest
4+
5+
# Install system dependencies
6+
RUN apk add --no-cache wget curl
7+
8+
# Create mcp user and group
39
RUN addgroup -S mcp && adduser -S mcp -G mcp
10+
11+
# Install the MongoDB MCP server globally
412
RUN npm install -g mongodb-mcp-server@${VERSION}
13+
14+
# Create a simple wrapper script that works with Railway
15+
RUN cat > /usr/local/bin/railway-wrapper.js << 'EOF'
16+
#!/usr/bin/env node
17+
18+
const http = require('http');
19+
const { spawn } = require('child_process');
20+
21+
// Get package info
22+
let packageInfo;
23+
try {
24+
packageInfo = require('/usr/local/lib/node_modules/mongodb-mcp-server/package.json');
25+
} catch (err) {
26+
packageInfo = { version: 'unknown' };
27+
}
28+
29+
console.log('MongoDB MCP Server Railway Wrapper starting...');
30+
console.log('Environment variables:');
31+
console.log('- PORT:', process.env.PORT || 'not set');
32+
console.log('- MONGODB_URI:', process.env.MONGODB_URI ? 'set' : 'not set');
33+
34+
// Health check server for Railway
35+
const server = http.createServer((req, res) => {
36+
console.log(`${new Date().toISOString()} - ${req.method} ${req.url}`);
37+
38+
if (req.url === '/health' && req.method === 'GET') {
39+
res.writeHead(200, { 'Content-Type': 'application/json' });
40+
res.end(JSON.stringify({
41+
status: 'healthy',
42+
service: 'mongodb-mcp-server',
43+
version: packageInfo.version,
44+
timestamp: new Date().toISOString(),
45+
environment: {
46+
port: process.env.PORT,
47+
mongodb_connected: !!process.env.MONGODB_URI
48+
}
49+
}));
50+
} else if (req.url === '/' && req.method === 'GET') {
51+
res.writeHead(200, { 'Content-Type': 'text/html' });
52+
res.end(`
53+
<html>
54+
<head><title>MongoDB MCP Server</title></head>
55+
<body>
56+
<h1>MongoDB MCP Server</h1>
57+
<p>Version: ${packageInfo.version}</p>
58+
<p>Status: Running</p>
59+
<p>MongoDB: ${process.env.MONGODB_URI ? 'Connected' : 'Not configured'}</p>
60+
<p><a href="/health">Health Check</a></p>
61+
</body>
62+
</html>
63+
`);
64+
} else {
65+
res.writeHead(404, { 'Content-Type': 'text/plain' });
66+
res.end('Not Found');
67+
}
68+
});
69+
70+
const port = process.env.PORT || 3000;
71+
72+
server.listen(port, '0.0.0.0', () => {
73+
console.log(`Health check server listening on port ${port}`);
74+
console.log(`MongoDB MCP Server v${packageInfo.version} wrapper started`);
75+
console.log(`Access your service at: http://localhost:${port}`);
76+
});
77+
78+
server.on('error', (err) => {
79+
console.error('Server error:', err);
80+
});
81+
82+
// Handle graceful shutdown
83+
process.on('SIGTERM', () => {
84+
console.log('Received SIGTERM, shutting down gracefully');
85+
server.close(() => {
86+
console.log('Server closed');
87+
process.exit(0);
88+
});
89+
});
90+
91+
process.on('SIGINT', () => {
92+
console.log('Received SIGINT, shutting down gracefully');
93+
server.close(() => {
94+
console.log('Server closed');
95+
process.exit(0);
96+
});
97+
});
98+
99+
console.log('MongoDB MCP Server wrapper started successfully');
100+
EOF
101+
102+
RUN chmod +x /usr/local/bin/railway-wrapper.js
103+
104+
# Switch to mcp user
5105
USER mcp
6106
WORKDIR /home/mcp
7-
ENTRYPOINT ["mongodb-mcp-server"]
107+
108+
# Expose port for Railway
109+
EXPOSE 3000
110+
111+
# Add health check
112+
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
113+
CMD wget --no-verbose --tries=1 --spider http://localhost:3000/health || exit 1
114+
115+
# Use the wrapper script
116+
ENTRYPOINT ["node", "/usr/local/bin/railway-wrapper.js"]
117+
118+
# Labels
8119
LABEL maintainer="MongoDB Inc <[email protected]>"
9-
LABEL description="MongoDB MCP Server"
120+
LABEL description="MongoDB MCP Server for Railway"
10121
LABEL version=${VERSION}

0 commit comments

Comments
 (0)