diegobeyl/backtesting
2
1# =============================================================================
2# Multi-Stage Build for Backtesting App V2
3# =============================================================================
4# Stage 1: Build dependencies and compile packages
5# Stage 2: Production runtime with minimal footprint
6# =============================================================================
7
8# -----------------------------------------------------------------------------
9# STAGE 1: Builder - Install and compile dependencies
10# -----------------------------------------------------------------------------
11FROM python:3.11-slim AS builder
12
13# Set working directory
14WORKDIR /build
15
16# Install build dependencies
17RUN apt-get update && apt-get install -y --no-install-recommends \
18 gcc \
19 g++ \
20 make \
21 curl \
22 && rm -rf /var/lib/apt/lists/*
23
24# Copy only requirements for better layer caching
25COPY requirements.docker.txt requirements.txt
26
27# Create virtual environment and install dependencies
28RUN python -m venv /opt/venv
29ENV PATH="/opt/venv/bin:$PATH"
30
31# Install Python packages
32RUN pip install --no-cache-dir --upgrade pip setuptools wheel && \
33 pip install --no-cache-dir -r requirements.txt
34
35# -----------------------------------------------------------------------------
36# STAGE 2: Runtime - Minimal production image
37# -----------------------------------------------------------------------------
38FROM python:3.11-slim
39
40# Metadata
41LABEL maintainer="backtesting-team"
42LABEL version="2.0"
43LABEL description="Backtesting Application V2 - Production Ready"
44
45# Set working directory
46WORKDIR /app
47
48# Set environment variables
49ENV PYTHONDONTWRITEBYTECODE=1 \
50 PYTHONUNBUFFERED=1 \
51 PATH="/opt/venv/bin:$PATH" \
52 TZ=UTC \
53 LANG=C.UTF-8 \
54 LC_ALL=C.UTF-8
55
56# Install minimal runtime dependencies
57RUN apt-get update && apt-get install -y --no-install-recommends \
58 curl \
59 tini \
60 && rm -rf /var/lib/apt/lists/* \
61 && apt-get clean
62
63# Copy virtual environment from builder
64COPY --from=builder /opt/venv /opt/venv
65
66# Create non-root user for security
67RUN groupadd -r -g 1000 appuser && \
68 useradd -r -u 1000 -g appuser -m -s /sbin/nologin appuser
69
70# Create necessary directories with proper permissions
71RUN mkdir -p logs cache config && \
72 chown -R appuser:appuser logs cache config
73
74# Copy application code
75COPY --chown=appuser:appuser api/ ./api/
76COPY --chown=appuser:appuser backtesting_app/ ./backtesting_app/
77COPY --chown=appuser:appuser core/ ./core/
78COPY --chown=appuser:appuser utils/ ./utils/
79COPY --chown=appuser:appuser static/ ./static/
80COPY --chown=appuser:appuser config/ ./config/
81COPY --chown=appuser:appuser config.py ./config.py
82COPY --chown=appuser:appuser .env.example ./.env.example
83
84# Copy startup script
85COPY --chown=appuser:appuser docker-entrypoint.sh /docker-entrypoint.sh
86RUN chmod +x /docker-entrypoint.sh
87
88# Switch to non-root user
89USER appuser
90
91# Expose ports
92EXPOSE 8000 8501
93
94# Health check
95HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
96 CMD curl -f http://localhost:8000/health || exit 1
97
98# Use tini as init system (handles zombie processes)
99ENTRYPOINT ["/usr/bin/tini", "--"]
100
101# Default command (can be overridden in docker-compose)
102CMD ["/docker-entrypoint.sh"]
103 