# Multi-stage build for optimized image size
FROM python:3.11-slim as builder
WORKDIR /app
# Install build dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
gcc \
&& rm -rf /var/lib/apt/lists/*
# Copy requirements and install
COPY requirements.txt .
RUN pip install --user --no-cache-dir --no-warn-script-location -r requirements.txt
# Final stage
FROM python:3.11-slim
# Add metadata
LABEL maintainer="ProfRandom92 <159939812+ProfRandom92@users.noreply.github.com>"
LABEL description="CompText MCP Server"
LABEL version="1.0.0"
WORKDIR /app
# Create non-root user for security
RUN useradd -m -u 1000 appuser && \
chown -R appuser:appuser /app
# Copy only necessary files from builder
COPY --from=builder --chown=appuser:appuser /root/.local /home/appuser/.local
COPY --chown=appuser:appuser . .
# Make sure scripts in .local are usable
ENV PATH=/home/appuser/.local/bin:$PATH
# Switch to non-root user
USER appuser
# Expose port
EXPOSE 8000
# Health check (using Python instead of requests for smaller footprint)
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health').read()"
# Run the application
CMD ["python", "-m", "uvicorn", "mcp_server:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "1"]