# ============================================================================= # Multi-Stage Python Dockerfile # Usage: # docker build -t myapp . # docker run -p 8000:8000 myapp # ============================================================================= # --------------------------------------------------------------------------- # Stage 1: Build dependencies # --------------------------------------------------------------------------- # Use slim for building - it has gcc and headers available via apt. FROM python:3.12-slim AS builder # Prevent Python from writing .pyc files and enable unbuffered output. ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 # Install build-time system dependencies (if any compiled packages need them). RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ && rm -rf /var/lib/apt/lists/* WORKDIR /app # Install Python dependencies into a virtual environment. # Copying requirements first enables Docker layer caching -- # dependencies are only reinstalled when requirements.txt changes. COPY requirements.txt . RUN python -m venv /app/.venv \ && /app/.venv/bin/pip install --no-cache-dir --upgrade pip \ && /app/.venv/bin/pip install --no-cache-dir -r requirements.txt # --------------------------------------------------------------------------- # Stage 2: Runtime # --------------------------------------------------------------------------- FROM python:3.12-slim AS runtime # Prevent .pyc files and enable unbuffered output for logging. ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 \ PATH="/app/.venv/bin:$PATH" # Install only runtime system dependencies (no build tools). # Add packages here if your app needs them at runtime (e.g., libpq for psycopg). # RUN apt-get update && apt-get install -y --no-install-recommends \ # libpq5 \ # && rm -rf /var/lib/apt/lists/* # Create a non-root user for security. RUN groupadd -r appuser && useradd -r -g appuser -d /app -s /bin/false appuser WORKDIR /app # Copy the virtual environment from the builder stage. COPY --from=builder /app/.venv /app/.venv # Copy application source code. COPY src/ ./src/ # Switch to non-root user. USER appuser # Expose the application port. EXPOSE 8000 # Health check -- adjust the endpoint to match your app. HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \ CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')" || exit 1 # Run the application. # For FastAPI/Uvicorn: CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8000"] # For Django/Gunicorn: # CMD ["gunicorn", "src.wsgi:application", "--bind", "0.0.0.0:8000", "--workers", "4"] # For a plain script: # CMD ["python", "-m", "src.main"]