diegobeyl/backtesting
2
1# =============================================================================
2# Docker Compose Configuration - Backtesting App V2
3# =============================================================================
4# Services:
5# - api: FastAPI backend (port 8000)
6# - streamlit: Streamlit UI (port 8501)
7# - redis: Cache and message broker (optional)
8# - postgres: Database for persistence (optional)
9# - nginx: Reverse proxy (optional, for production)
10# =============================================================================
11
12
13services:
14 # ---------------------------------------------------------------------------
15 # FastAPI Backend
16 # ---------------------------------------------------------------------------
17 api:
18 build:
19 context: .
20 dockerfile: Dockerfile
21 target: builder
22 args:
23 - BUILDKIT_INLINE_CACHE=1
24 image: backtesting-app-v2:latest
25 container_name: backtesting_api_v2
26 restart: unless-stopped
27 ports:
28 - "${API_PORT:-8000}:8000"
29 volumes:
30 - ./logs:/app/logs
31 - ./cache:/app/cache
32 - ./config:/app/config
33 - api_data:/app/data
34 environment:
35 - SERVICE_TYPE=api
36 - DEBUG=${DEBUG:-False}
37 - ENVIRONMENT=${ENVIRONMENT:-production}
38 - LOG_LEVEL=${LOG_LEVEL:-info}
39 - API_HOST=0.0.0.0
40 - API_PORT=8000
41 - ENABLE_CACHE=${ENABLE_CACHE:-True}
42 - CACHE_EXPIRY_HOURS=${CACHE_EXPIRY_HOURS:-24}
43 # Redis configuration (if enabled)
44 - REDIS_HOST=redis
45 - REDIS_PORT=6379
46 - REDIS_DB=0
47 # Database configuration (if enabled)
48 - DATABASE_URL=${DATABASE_URL:-sqlite:///./data/backtest_results.db}
49 env_file:
50 - .env
51 networks:
52 - backtesting_network
53 depends_on:
54 redis:
55 condition: service_healthy
56 healthcheck:
57 test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
58 interval: 30s
59 timeout: 10s
60 retries: 3
61 start_period: 40s
62 deploy:
63 resources:
64 limits:
65 cpus: '2'
66 memory: 2G
67 reservations:
68 cpus: '0.5'
69 memory: 512M
70
71 # ---------------------------------------------------------------------------
72 # Streamlit Frontend
73 # ---------------------------------------------------------------------------
74 streamlit:
75 image: backtesting-app-v2:latest
76 container_name: backtesting_streamlit_v2
77 restart: unless-stopped
78 ports:
79 - "${STREAMLIT_PORT:-8501}:8501"
80 volumes:
81 - ./logs:/app/logs
82 - ./cache:/app/cache
83 - ./config:/app/config
84 environment:
85 - SERVICE_TYPE=streamlit
86 - STREAMLIT_SERVER_PORT=8501
87 - STREAMLIT_SERVER_ADDRESS=0.0.0.0
88 - STREAMLIT_BROWSER_GATHER_USAGE_STATS=false
89 - API_HOST=api
90 - API_PORT=8000
91 env_file:
92 - .env
93 networks:
94 - backtesting_network
95 depends_on:
96 api:
97 condition: service_healthy
98 healthcheck:
99 test: ["CMD", "curl", "-f", "http://localhost:8501/_stcore/health"]
100 interval: 30s
101 timeout: 10s
102 retries: 3
103 start_period: 40s
104 deploy:
105 resources:
106 limits:
107 cpus: '1'
108 memory: 1G
109 reservations:
110 cpus: '0.25'
111 memory: 256M
112
113 # ---------------------------------------------------------------------------
114 # Redis - Cache & Message Broker
115 # ---------------------------------------------------------------------------
116 redis:
117 image: redis:7-alpine
118 container_name: backtesting_redis
119 restart: unless-stopped
120 ports:
121 - "6379:6379"
122 volumes:
123 - redis_data:/data
124 command: >
125 redis-server
126 --appendonly yes
127 --appendfsync everysec
128 --maxmemory 512mb
129 --maxmemory-policy allkeys-lru
130 networks:
131 - backtesting_network
132 healthcheck:
133 test: ["CMD", "redis-cli", "ping"]
134 interval: 10s
135 timeout: 3s
136 retries: 3
137 start_period: 10s
138 deploy:
139 resources:
140 limits:
141 cpus: '0.5'
142 memory: 512M
143 reservations:
144 cpus: '0.1'
145 memory: 128M
146
147 # ---------------------------------------------------------------------------
148 # PostgreSQL Database (Optional - uncomment to enable)
149 # ---------------------------------------------------------------------------
150 # postgres:
151 # image: postgres:15-alpine
152 # container_name: backtesting_postgres
153 # restart: unless-stopped
154 # ports:
155 # - "5432:5432"
156 # volumes:
157 # - postgres_data:/var/lib/postgresql/data
158 # - ./init.sql:/docker-entrypoint-initdb.d/init.sql
159 # environment:
160 # - POSTGRES_USER=${POSTGRES_USER:-backtesting}
161 # - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-backtesting123}
162 # - POSTGRES_DB=${POSTGRES_DB:-backtesting_db}
163 # - PGDATA=/var/lib/postgresql/data/pgdata
164 # networks:
165 # - backtesting_network
166 # healthcheck:
167 # test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-backtesting}"]
168 # interval: 10s
169 # timeout: 5s
170 # retries: 5
171 # start_period: 10s
172 # deploy:
173 # resources:
174 # limits:
175 # cpus: '1'
176 # memory: 1G
177
178 # ---------------------------------------------------------------------------
179 # Celery Worker (Optional - for async tasks)
180 # ---------------------------------------------------------------------------
181 # worker:
182 # image: backtesting-app-v2:latest
183 # container_name: backtesting_worker
184 # restart: unless-stopped
185 # volumes:
186 # - ./logs:/app/logs
187 # - ./cache:/app/cache
188 # - ./config:/app/config
189 # environment:
190 # - SERVICE_TYPE=worker
191 # - REDIS_HOST=redis
192 # - REDIS_PORT=6379
193 # - LOG_LEVEL=${LOG_LEVEL:-INFO}
194 # - WORKER_CONCURRENCY=4
195 # env_file:
196 # - .env
197 # networks:
198 # - backtesting_network
199 # depends_on:
200 # redis:
201 # condition: service_healthy
202 # deploy:
203 # resources:
204 # limits:
205 # cpus: '2'
206 # memory: 2G
207
208 # ---------------------------------------------------------------------------
209 # Nginx Reverse Proxy (Optional - for production)
210 # ---------------------------------------------------------------------------
211 # nginx:
212 # image: nginx:alpine
213 # container_name: backtesting_nginx
214 # restart: unless-stopped
215 # ports:
216 # - "80:80"
217 # - "443:443"
218 # volumes:
219 # - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
220 # - ./nginx/ssl:/etc/nginx/ssl:ro
221 # - ./static:/usr/share/nginx/html/static:ro
222 # networks:
223 # - backtesting_network
224 # depends_on:
225 # - api
226 # - streamlit
227 # healthcheck:
228 # test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost/health"]
229 # interval: 30s
230 # timeout: 10s
231 # retries: 3
232 # deploy:
233 # resources:
234 # limits:
235 # cpus: '0.5'
236 # memory: 256M
237
238# =============================================================================
239# Networks
240# =============================================================================
241networks:
242 backtesting_network:
243 driver: bridge
244 ipam:
245 config:
246 - subnet: 172.28.0.0/16
247
248# =============================================================================
249# Volumes
250# =============================================================================
251volumes:
252 redis_data:
253 driver: local
254 # postgres_data:
255 # driver: local
256 api_data:
257 driver: local
258 