Dockerfile.production•2.66 kB
# Production Dockerfile for MCP Server
# Multi-stage build for optimized production image
# Build stage
FROM python:3.11-slim as builder
# Set build arguments
ARG BUILD_DATE
ARG VERSION=2.0.0
ARG VCS_REF
# Add metadata
LABEL maintainer="MCP System" \
version="${VERSION}" \
description="Production MCP Server - Scalable Agent Network" \
build-date="${BUILD_DATE}" \
vcs-ref="${VCS_REF}"
# Install system dependencies for building
RUN apt-get update && apt-get install -y \
build-essential \
curl \
git \
&& rm -rf /var/lib/apt/lists/*
# Set working directory
WORKDIR /app
# Copy requirements first for better caching
COPY requirements.txt .
COPY requirements-production.txt .
# Install Python dependencies
RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir -r requirements.txt && \
pip install --no-cache-dir -r requirements-production.txt
# Production stage
FROM python:3.11-slim as production
# Install runtime dependencies
RUN apt-get update && apt-get install -y \
curl \
wget \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean
# Create non-root user for security
RUN groupadd -r mcp && useradd -r -g mcp mcp
# Set working directory
WORKDIR /app
# Copy Python packages from builder
COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin
# Copy application code
COPY . .
# Create necessary directories
RUN mkdir -p logs backups config/ssl && \
chown -R mcp:mcp /app
# Set environment variables
ENV PYTHONPATH=/app \
PYTHONUNBUFFERED=1 \
ENVIRONMENT=production \
LOG_LEVEL=INFO \
PORT=8000
# Switch to non-root user
USER mcp
# Expose port
EXPOSE 8000
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
CMD curl -f http://localhost:8000/api/health || exit 1
# Default command
CMD ["python", "production_mcp_server.py"]
# Development stage (for development builds)
FROM production as development
# Switch back to root for development tools
USER root
# Install development dependencies
RUN apt-get update && apt-get install -y \
vim \
htop \
net-tools \
&& rm -rf /var/lib/apt/lists/*
# Install development Python packages
COPY requirements-dev.txt .
RUN pip install --no-cache-dir -r requirements-dev.txt
# Set development environment
ENV ENVIRONMENT=development \
LOG_LEVEL=DEBUG
# Switch back to mcp user
USER mcp
# Development command with auto-reload
CMD ["python", "-m", "uvicorn", "production_mcp_server:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]