# Production Dockerfile Template # Customize for your application # ============================================================================== # Build Stage # ============================================================================== FROM python:3.11-slim AS builder WORKDIR /app # Install build dependencies (if needed) RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ && rm -rf /var/lib/apt/lists/* # Create virtual environment RUN python -m venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" # Install Python dependencies COPY requirements.txt . RUN pip install --no-cache-dir --upgrade pip \ && pip install --no-cache-dir -r requirements.txt # ============================================================================== # Production Stage # ============================================================================== FROM python:3.11-slim WORKDIR /app # Install runtime dependencies only RUN apt-get update && apt-get install -y --no-install-recommends \ # Add runtime dependencies here (e.g., libpq5 for postgres) curl \ && rm -rf /var/lib/apt/lists/* # Copy virtual environment from builder COPY --from=builder /opt/venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" # Create non-root user RUN groupadd --gid 1000 appgroup \ && useradd --uid 1000 --gid appgroup --shell /bin/bash --create-home appuser # Set ownership RUN chown -R appuser:appgroup /app # Switch to non-root user USER appuser # Copy application code COPY --chown=appuser:appgroup src/ ./src/ # Environment variables ENV PYTHONUNBUFFERED=1 \ PYTHONDONTWRITEBYTECODE=1 \ PORT=8000 # Health check HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \ CMD curl -f http://localhost:${PORT}/health || exit 1 # Expose port EXPOSE ${PORT} # Run application CMD ["python", "-m", "uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8000"]