130 lines
2.7 KiB
Docker
130 lines
2.7 KiB
Docker
# Multi-stage Dockerfile for optimized production builds
|
|
FROM python:3.11-slim as base
|
|
|
|
# Set environment variables
|
|
ENV PYTHONUNBUFFERED=1 \
|
|
PYTHONDONTWRITEBYTECODE=1 \
|
|
PIP_NO_CACHE_DIR=1 \
|
|
PIP_DISABLE_PIP_VERSION_CHECK=1 \
|
|
POETRY_VERSION=1.6.1
|
|
|
|
# Install system dependencies
|
|
RUN apt-get update && apt-get install -y \
|
|
build-essential \
|
|
curl \
|
|
ffmpeg \
|
|
libmagic1 \
|
|
libpq-dev \
|
|
pkg-config \
|
|
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
# Install Poetry
|
|
RUN pip install poetry==$POETRY_VERSION
|
|
|
|
# Configure Poetry
|
|
ENV POETRY_NO_INTERACTION=1 \
|
|
POETRY_VENV_IN_PROJECT=1 \
|
|
POETRY_CACHE_DIR=/tmp/poetry_cache
|
|
|
|
WORKDIR /app
|
|
|
|
# Copy dependency files
|
|
COPY pyproject.toml poetry.lock ./
|
|
|
|
# Development stage
|
|
FROM base as development
|
|
|
|
# Install dependencies including dev dependencies
|
|
RUN poetry install --with dev && rm -rf $POETRY_CACHE_DIR
|
|
|
|
# Copy source code
|
|
COPY . .
|
|
|
|
# Set development environment
|
|
ENV PYTHONPATH=/app
|
|
ENV DEBUG=true
|
|
|
|
# Expose ports
|
|
EXPOSE 15100 9090
|
|
|
|
# Default command for development
|
|
CMD ["poetry", "run", "python", "-m", "app"]
|
|
|
|
# Production dependencies stage
|
|
FROM base as deps
|
|
|
|
# Install only production dependencies
|
|
RUN poetry install --only=main && rm -rf $POETRY_CACHE_DIR
|
|
|
|
# Production stage
|
|
FROM python:3.11-slim as production
|
|
|
|
# Install runtime dependencies only
|
|
RUN apt-get update && apt-get install -y \
|
|
curl \
|
|
ffmpeg \
|
|
libmagic1 \
|
|
libpq5 \
|
|
&& rm -rf /var/lib/apt/lists/* \
|
|
&& apt-get clean
|
|
|
|
# Create non-root user
|
|
RUN groupadd -r appuser && useradd -r -g appuser appuser
|
|
|
|
# Set working directory
|
|
WORKDIR /app
|
|
|
|
# Copy virtual environment from deps stage
|
|
COPY --from=deps /app/.venv /app/.venv
|
|
|
|
# Add virtual environment to PATH
|
|
ENV PATH="/app/.venv/bin:$PATH"
|
|
|
|
# Copy application code
|
|
COPY --chown=appuser:appuser . .
|
|
|
|
# Create necessary directories
|
|
RUN mkdir -p /app/data /app/logs && \
|
|
chown -R appuser:appuser /app/data /app/logs
|
|
|
|
# Set production environment
|
|
ENV PYTHONPATH=/app
|
|
ENV DEBUG=false
|
|
|
|
# Health check
|
|
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
|
|
CMD curl -f http://localhost:15100/health || exit 1
|
|
|
|
# Switch to non-root user
|
|
USER appuser
|
|
|
|
# Expose ports
|
|
EXPOSE 15100 9090
|
|
|
|
# Default command
|
|
CMD ["python", "-m", "app"]
|
|
|
|
# Testing stage
|
|
FROM development as testing
|
|
|
|
# Install test dependencies
|
|
RUN poetry install --with dev,test
|
|
|
|
# Run tests
|
|
RUN poetry run pytest tests/ --cov=app --cov-report=term-missing
|
|
|
|
# Security scanning stage
|
|
FROM production as security
|
|
|
|
# Switch back to root for security scanning
|
|
USER root
|
|
|
|
# Install security tools
|
|
RUN pip install safety bandit
|
|
|
|
# Run security checks
|
|
RUN safety check
|
|
RUN bandit -r app/ -f json -o security-report.json || true
|
|
|
|
# Switch back to app user
|
|
USER appuser |