# Multi-stage production Dockerfile for MCP Server
FROM python:3.12-slim as builder
# Install system dependencies for building
RUN apt-get update && apt-get install -y \
build-essential \
gcc \
g++ \
git \
&& rm -rf /var/lib/apt/lists/*
# Create build directory
WORKDIR /build
# Copy requirements first for better caching
COPY requirements-production.txt .
# Install Python dependencies
RUN pip install --no-cache-dir --user -r requirements-production.txt
# Production stage
FROM python:3.12-slim as production
# Install runtime dependencies including tree-sitter requirements
RUN apt-get update && apt-get install -y \
curl \
libstdc++6 \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean
# Create non-root user for security
RUN groupadd -r mcp && useradd -r -g mcp -d /app -s /bin/bash mcp
# Set up application directory
WORKDIR /app
# Copy Python dependencies from builder
COPY --from=builder /root/.local /home/mcp/.local
# Make sure scripts in .local are usable
ENV PATH=/home/mcp/.local/bin:$PATH
# Copy application code
COPY mcp_server/ ./mcp_server/
COPY pyproject.toml .
COPY README.md .
# Create necessary directories
RUN mkdir -p /var/log/mcp-server /app/uploads /app/data \
&& chown -R mcp:mcp /app /var/log/mcp-server
# Switch to non-root user
USER mcp
# Set environment variables
ENV PYTHONPATH=/app
ENV PYTHONUNBUFFERED=1
ENV MCP_ENVIRONMENT=production
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
CMD curl -f http://localhost:8000/health || exit 1
# Expose ports
EXPOSE 8000 8001
# Production startup command with Gunicorn
CMD ["gunicorn", "mcp_server.gateway:app", \
"--worker-class", "uvicorn.workers.UvicornWorker", \
"--bind", "0.0.0.0:8000", \
"--workers", "4", \
"--worker-connections", "1000", \
"--max-requests", "1000", \
"--max-requests-jitter", "100", \
"--timeout", "30", \
"--keepalive", "2", \
"--access-logfile", "-", \
"--error-logfile", "-", \
"--log-level", "info"]