-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-compose.yml
More file actions
217 lines (209 loc) · 6.98 KB
/
Copy pathdocker-compose.yml
File metadata and controls
217 lines (209 loc) · 6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
services:
# --- 1. PostgreSQL Database ---
postgres-db:
image: postgres:15-alpine
container_name: postgres-db
restart: always
# Not published to the host (fix-list 2.7) -- only other containers on
# this Compose network need to reach it; `expose` documents the port
# without binding it to the host interface the way `ports` does.
expose:
- "5432"
env_file:
- .env
volumes:
- postgres_data:/var/lib/postgresql/data
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
# --- THIS IS THE FIX (PART 1) ---
# This tells Docker how to check if Postgres is ready.
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
interval: 5s
timeout: 5s
retries: 5
# --- 2. Qdrant Database ---
qdrant-db:
image: qdrant/qdrant:latest
container_name: qdrant-db
restart: always
# Not published to the host -- see postgres-db above.
expose:
- "6333"
- "6334"
volumes:
- qdrant_data:/qdrant/storage
# (Qdrant also has a healthcheck, but we don't need it yet)
# --- 3. The API (FastAPI backend) ---
# One process per container (fix-list 2.1): previously run.sh backgrounded
# uvicorn and foregrounded Streamlit in a single container, so a dead
# backend didn't stop the container from looking "up". api and ui are now
# independent services with their own healthchecks -- see src/Dockerfile.api
# and src/Dockerfile.ui.
api:
build:
context: .
dockerfile: src/Dockerfile.api
container_name: api
restart: always
ports:
- "8000:8000"
env_file:
- .env
depends_on:
postgres-db:
condition: service_healthy
qdrant-db:
condition: service_started # Qdrant starts fast, this is fine
volumes:
- ./data:/app/data
# Not baked into the image (it's an occasional operational script,
# not app code) -- mounted read-only instead, matching how the
# Airflow containers below mount ./src:ro.
- ./scripts:/app/scripts:ro
healthcheck:
# No curl in python:3.10-slim -- urllib is stdlib, so this needs no
# extra package. start_period is long because lifespan loads
# YOLO + EasyOCR + SentenceTransformer and builds the LangChain agent
# before /health can answer at all (cold start, ~20-40s).
test: ["CMD", "python", "-c", "import urllib.request as u; u.urlopen('http://localhost:8000/health', timeout=3)"]
interval: 15s
timeout: 5s
retries: 5
start_period: 60s
# --- 3b. The UI (Streamlit frontend) ---
ui:
build:
context: .
dockerfile: src/Dockerfile.ui
container_name: ui
restart: always
ports:
- "8501:8501"
environment:
API_BASE_URL: http://api:8000
API_KEY: ${API_KEY}
depends_on:
api:
condition: service_healthy
healthcheck:
test: ["CMD", "python", "-c", "import urllib.request as u; u.urlopen('http://localhost:8501/_stcore/health', timeout=3)"]
interval: 15s
timeout: 5s
retries: 5
start_period: 30s
# --- 4. Airflow metadata DB -- deliberately separate from postgres-db
# above. Airflow's own migrations/restarts shouldn't touch the app's
# database, and vice versa. ---
airflow-postgres:
image: postgres:15-alpine
container_name: airflow-postgres
restart: always
env_file:
- .env
environment:
POSTGRES_USER: ${AIRFLOW_POSTGRES_USER}
POSTGRES_PASSWORD: ${AIRFLOW_POSTGRES_PASSWORD}
POSTGRES_DB: ${AIRFLOW_POSTGRES_DB}
volumes:
- airflow_postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${AIRFLOW_POSTGRES_USER} -d ${AIRFLOW_POSTGRES_DB}"]
interval: 5s
timeout: 5s
retries: 5
# --- 5. Airflow: one-shot metadata DB migration + admin user creation.
# Runs once per `docker compose up`; webserver/scheduler wait for it to
# exit 0 (service_completed_successfully) before starting. ---
airflow-init:
build:
context: .
dockerfile: airflow/Dockerfile
container_name: airflow-init
depends_on:
airflow-postgres:
condition: service_healthy
env_file:
- .env
environment: &airflow-common-env
AIRFLOW__CORE__EXECUTOR: LocalExecutor
AIRFLOW__DATABASE__SQL_ALCHEMY_CONN: postgresql+psycopg2://${AIRFLOW_POSTGRES_USER}:${AIRFLOW_POSTGRES_PASSWORD}@airflow-postgres/${AIRFLOW_POSTGRES_DB}
AIRFLOW__CORE__FERNET_KEY: ${AIRFLOW_FERNET_KEY}
AIRFLOW__CORE__LOAD_EXAMPLES: "false"
AIRFLOW__API__AUTH_BACKENDS: airflow.api.auth.backend.basic_auth
# Airflow Connections -- read via BaseHook.get_connection() in
# dags/support.py, never os.environ directly in DAG code. This is
# the framework's own documented mechanism for seeding Connections
# in a container, not the thing that instruction is steering away
# from.
AIRFLOW_CONN_SMARTSENSE_POSTGRES: postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres-db:5432/${POSTGRES_DB}
AIRFLOW_CONN_SMARTSENSE_QDRANT: http://qdrant-db:6333
AIRFLOW_CONN_SMARTSENSE_INTERNAL_API: >-
{"conn_type": "http", "host": "api", "port": 8000, "extra": {"api_key": "${INTERNAL_API_KEY}"}}
volumes:
- ./data:/app/data
- ./dags:/opt/airflow/dags
- ./src:/opt/airflow/src:ro
entrypoint: /bin/bash
command:
- -c
- |
airflow db migrate
airflow users create \
--username "${AIRFLOW_ADMIN_USER}" \
--password "${AIRFLOW_ADMIN_PASSWORD}" \
--firstname Admin \
--lastname User \
--role Admin \
--email admin@example.com \
|| true
# --- 6. Airflow webserver (UI + REST API) ---
airflow-webserver:
build:
context: .
dockerfile: airflow/Dockerfile
container_name: airflow-webserver
restart: always
depends_on:
airflow-postgres:
condition: service_healthy
airflow-init:
condition: service_completed_successfully
env_file:
- .env
environment: *airflow-common-env
ports:
- "8080:8080"
volumes:
- ./data:/app/data
- ./dags:/opt/airflow/dags
- ./src:/opt/airflow/src:ro
command: webserver
# --- 7. Airflow scheduler (LocalExecutor -- runs tasks as its own
# subprocesses, no separate worker service needed at this scale) ---
airflow-scheduler:
build:
context: .
dockerfile: airflow/Dockerfile
container_name: airflow-scheduler
restart: always
depends_on:
airflow-postgres:
condition: service_healthy
airflow-init:
condition: service_completed_successfully
env_file:
- .env
environment: *airflow-common-env
volumes:
- ./data:/app/data
- ./dags:/opt/airflow/dags
- ./src:/opt/airflow/src:ro
command: scheduler
# --- Define the persistent volumes ---
volumes:
postgres_data:
qdrant_data:
airflow_postgres_data: