Files
WealthWise/backend/Dockerfile

72 lines
2.1 KiB
Docker

# Multi-stage Dockerfile for WealthWise Backend
# Stage 1: Builder - Install dependencies and export requirements
# Stage 2: Runtime - Slim production image
# ==========================================
# STAGE 1: Builder
# ==========================================
FROM python:3.11-slim as builder
WORKDIR /build
# Install system dependencies for building Python packages
RUN apt-get update && apt-get install -y --no-install-recommends \
gcc \
libpq-dev \
&& rm -rf /var/lib/apt/lists/*
# Install Poetry
RUN pip install --no-cache-dir poetry==1.7.1
# Copy Poetry configuration
COPY pyproject.toml ./
# Configure Poetry to not create a virtual environment
# and export dependencies to requirements.txt
RUN poetry config virtualenvs.create false && \
poetry export -f requirements.txt --output requirements.txt --without-hashes --only main
# ==========================================
# STAGE 2: Runtime
# ==========================================
FROM python:3.11-slim as runtime
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PYTHONFAULTHANDLER=1 \
PIP_NO_CACHE_DIR=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1
WORKDIR /app
# Install runtime dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
libpq5 \
&& rm -rf /var/lib/apt/lists/*
# Create non-root user for security
RUN useradd --create-home --shell /bin/bash app && \
chown -R app:app /app
USER app
# Copy requirements from builder stage
COPY --from=builder /build/requirements.txt .
# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy application code
COPY --chown=app:app app/ ./app/
# Expose port
EXPOSE 8000
# Health check
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/api/v1/health')" || exit 1
# Run the application with uvicorn
# Using multiple workers for production (adjust based on CPU cores)
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "4"]