|
1 | | -# Use Python 3.11 slim image as base |
2 | | -FROM python:3.11-slim |
| 1 | +# Multi-stage Dockerfile for MCP Server |
| 2 | +FROM python:3.11-slim as base |
3 | 3 |
|
4 | | -# Set working directory to parent directory |
| 4 | +# Install system dependencies |
| 5 | +RUN apt-get update && apt-get install -y \ |
| 6 | + curl \ |
| 7 | + && rm -rf /var/lib/apt/lists/* |
| 8 | + |
| 9 | +# Set working directory |
5 | 10 | WORKDIR /workspace |
6 | 11 |
|
| 12 | +# Copy pyproject.toml first for better caching |
| 13 | +COPY pyproject.toml ./mcp_server/ |
| 14 | + |
| 15 | +# Development stage - includes test dependencies |
| 16 | +FROM base as development |
| 17 | + |
| 18 | +# Install the package with all dependencies (including test and dev dependencies) |
| 19 | +RUN cd mcp_server && pip install --no-cache-dir -e .[test,dev] |
| 20 | + |
7 | 21 | # Copy the entire mcp_server directory |
8 | 22 | COPY . ./mcp_server/ |
9 | 23 |
|
10 | | -# Install dependencies |
11 | | -RUN pip install --no-cache-dir fastapi==0.104.1 uvicorn==0.24.0 httpx==0.25.1 sqlalchemy==2.0.23 pydantic==2.8.2 pydantic-settings==2.4.0 python-dotenv==1.0.0 |
| 24 | +# Set PYTHONPATH to ensure imports work correctly |
| 25 | +ENV PYTHONPATH=/workspace |
12 | 26 |
|
13 | 27 | # Expose port 8001 |
14 | 28 | EXPOSE 8001 |
15 | 29 |
|
16 | | -# Set PYTHONPATH and run the application |
| 30 | +# Health check |
| 31 | +HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ |
| 32 | + CMD curl -f http://localhost:8001/health || exit 1 |
| 33 | + |
| 34 | +# Default command to run the application |
| 35 | +CMD ["uvicorn", "mcp_server.main:app", "--host", "0.0.0.0", "--port", "8001"] |
| 36 | + |
| 37 | +# Production stage - minimal dependencies |
| 38 | +FROM base as production |
| 39 | + |
| 40 | +# Install only production dependencies |
| 41 | +RUN cd mcp_server && pip install --no-cache-dir -e . |
| 42 | + |
| 43 | +# Copy the entire mcp_server directory |
| 44 | +COPY . ./mcp_server/ |
| 45 | + |
| 46 | +# Set PYTHONPATH to ensure imports work correctly |
17 | 47 | ENV PYTHONPATH=/workspace |
| 48 | + |
| 49 | +# Create a non-root user for security |
| 50 | +RUN useradd --create-home --shell /bin/bash app |
| 51 | +USER app |
| 52 | + |
| 53 | +# Expose port 8001 |
| 54 | +EXPOSE 8001 |
| 55 | + |
| 56 | +# Health check |
| 57 | +HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ |
| 58 | + CMD curl -f http://localhost:8001/health || exit 1 |
| 59 | + |
| 60 | +# Default command to run the application |
18 | 61 | CMD ["uvicorn", "mcp_server.main:app", "--host", "0.0.0.0", "--port", "8001"] |
| 62 | + |
| 63 | +# Test stage - for running tests in CI |
| 64 | +FROM development as test |
| 65 | + |
| 66 | +# Run tests by default |
| 67 | +CMD ["python", "-m", "pytest", "mcp_server/tests/", "-v"] |
0 commit comments