# Multi-stage build for minimal image size FROM python:3.11-alpine AS builder # Install build dependencies RUN apk add --no-cache --virtual .build-deps \ gcc \ musl-dev \ libffi-dev \ && rm -rf /var/cache/apk/* # Set working directory WORKDIR /app # Copy requirements first for better caching COPY requirements.txt . # Install Python dependencies RUN pip install --no-cache-dir --user -r requirements.txt # Final stage - use Alpine without Python to copy only what's needed FROM alpine:3.18 # Install runtime dependencies only RUN apk add --no-cache \ python3 \ py3-pip \ && rm -rf /var/cache/apk/* # Create non-root user for security RUN addgroup -g 1000 appuser && \ adduser -D -s /bin/sh -u 1000 -G appuser appuser # Set working directory WORKDIR /app # Copy Python packages from builder stage COPY --from=builder /root/.local /home/appuser/.local # Copy application code COPY --chown=appuser:appuser . . # Switch to non-root user USER appuser # Add local bin to PATH ENV PATH=/home/appuser/.local/bin:$PATH # Expose port if needed (adjust as required) # EXPOSE 8000 # Run the application CMD ["python3", "main.py"]