Repository navigation
Expand file tree
/
Copy pathdocker-compose.yml
More file actions
executable file
·233 lines (224 loc) · 9.56 KB
/
Copy pathdocker-compose.yml
File metadata and controls
executable file
·233 lines (224 loc) · 9.56 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
x-db-env: &db-env
DB_HOST: ${DB_HOST:-timescaledb}
DB_NAME: ${DB_NAME:-stockdw}
DB_USER: ${DB_USER:-data226}
DB_PASSWORD: ${DB_PASSWORD:?DB_PASSWORD must be set in .env}
x-airflow-env: &airflow-env
<<: *db-env
AIRFLOW__CORE__EXECUTOR: LocalExecutor
# Airflow's own bookkeeping lives in its own database. It used to share
# stockdw, which put 42 Airflow tables in the same schema as the nine
# warehouse tables: a pg_dump of the warehouse carried Airflow's users and
# run history with it, and `airflow db reset` pointed at the schema holding
# the facts. AIRFLOW_CONN_TIMESCALEDB_CONN still points at the warehouse --
# that is the connection the DAGs read and write through.
AIRFLOW__DATABASE__SQL_ALCHEMY_CONN: postgresql+psycopg2://${DB_USER:-data226}:${DB_PASSWORD}@${DB_HOST:-timescaledb}:5432/${AIRFLOW_DB_NAME:-airflow}
AIRFLOW_CONN_TIMESCALEDB_CONN: postgresql://${DB_USER:-data226}:${DB_PASSWORD}@${DB_HOST:-timescaledb}:5432/${DB_NAME:-stockdw}
AIRFLOW__WEBSERVER__SECRET_KEY: ${AIRFLOW__WEBSERVER__SECRET_KEY:?Set AIRFLOW__WEBSERVER__SECRET_KEY in .env}
AIRFLOW__CORE__FERNET_KEY: ${AIRFLOW__CORE__FERNET_KEY:?Set AIRFLOW__CORE__FERNET_KEY in .env}
AIRFLOW__CORE__DAGS_ARE_PAUSED_AT_CREATION: 'true'
AIRFLOW__CORE__LOAD_EXAMPLES: 'false'
FRED_API_KEY: ${FRED_API_KEY:-}
EDGAR_IDENTITY: ${EDGAR_IDENTITY:-StockDataPlatform user@example.com}
# .env documented TICKERS_FILE as the one knob driving both the populate
# scripts and the ETL DAG factory, but compose never forwarded it, so setting
# it had no effect on the containers.
TICKERS_FILE: ${TICKERS_FILE:-/opt/airflow/dags/tickers.txt}
AIRFLOW_ADMIN_USER: ${AIRFLOW_ADMIN_USER:-admin}
AIRFLOW_ADMIN_PASSWORD: ${AIRFLOW_ADMIN_PASSWORD:?Set AIRFLOW_ADMIN_PASSWORD in .env}
AIRFLOW_ADMIN_EMAIL: ${AIRFLOW_ADMIN_EMAIL:-admin@example.com}
# Free Alpaca paper-trading keys: hourly bars back to 2016. Left empty,
# intraday_prices_daily is skipped and the stored hourly bars stop updating.
ALPACA_API_KEY: ${ALPACA_API_KEY:-}
ALPACA_API_SECRET: ${ALPACA_API_SECRET:-}
x-airflow-volumes: &airflow-volumes
- ./Dags:/opt/airflow/dags
- ./scripts:/opt/airflow/scripts
- ./SQL:/opt/airflow/sql
# Task logs on the host. Inside the container they vanished on every
# recreate, which is how the log that would have shown NVDA's missed
# re-adjustment was lost. Gitignored.
- ./logs:/opt/airflow/logs
services:
timescaledb:
# Pinned: latest-pg14 moves the TimescaleDB minor version under an
# existing data directory on every pull.
image: timescale/timescaledb:2.19.3-pg14
container_name: timescaledb
restart: unless-stopped
ports:
- "5434:5432"
environment:
POSTGRES_USER: ${POSTGRES_USER:-data226}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD must be set in .env}
POSTGRES_DB: ${POSTGRES_DB:-stockdw}
volumes:
- ./data/db:/var/lib/postgresql/data
- ./SQL/00-databases.sql:/docker-entrypoint-initdb.d/00-databases.sql:ro
- ./SQL/schema.sql:/docker-entrypoint-initdb.d/01-schema.sql:ro
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-data226} -d ${POSTGRES_DB:-stockdw}"]
interval: 5s
timeout: 5s
retries: 20
airflow-webserver:
build:
context: .
dockerfile: Dockerfile.airflow
container_name: airflow-webserver
# Run as the host user so bind-mounted paths under ./Dags stay writable.
# Airflow images expect an arbitrary uid with GID 0; /opt/airflow is group-writable.
user: "${AIRFLOW_UID:-50000}:0"
restart: unless-stopped
# The scheduler owns `airflow db migrate`. Both services used to run it,
# racing on the same alembic_version row; it survives on luck today and
# deadlocks the first time a version bump carries real migrations.
depends_on:
timescaledb:
condition: service_healthy
airflow-scheduler:
condition: service_healthy
environment:
<<: *airflow-env
volumes: *airflow-volumes
ports:
- "8081:8080"
# One line per command, all at the same indent. In a YAML `>` scalar a
# more-indented line keeps its newline instead of folding to a space, which
# split this create call across several shell commands: argparse saw none of
# the flags, the user was never created, and the old `|| true` swallowed it
# silently -- the UI came up with no account that could log into it.
#
# So: on failure, check the postcondition rather than assuming. A restart
# where the user already exists carries on; a create that genuinely failed
# stops the webserver instead of serving a login nobody can pass.
command: >
bash -c "
(airflow users create --username \"$${AIRFLOW_ADMIN_USER}\" --password \"$${AIRFLOW_ADMIN_PASSWORD}\" --firstname Air --lastname Flow --role Admin --email \"$${AIRFLOW_ADMIN_EMAIL}\" || airflow users list --output plain | awk '{print \$$2}' | grep -qx \"$${AIRFLOW_ADMIN_USER}\") &&
airflow webserver"
healthcheck:
test: ["CMD-SHELL", "curl -fsS http://localhost:8080/health || exit 1"]
interval: 15s
timeout: 10s
retries: 10
start_period: 60s
extra_hosts:
- "host.docker.internal:host-gateway"
airflow-scheduler:
build:
context: .
dockerfile: Dockerfile.airflow
container_name: airflow-scheduler
# Run as the host user so bind-mounted paths under ./Dags stay writable.
# Airflow images expect an arbitrary uid with GID 0; /opt/airflow is group-writable.
user: "${AIRFLOW_UID:-50000}:0"
restart: unless-stopped
depends_on:
timescaledb:
condition: service_healthy
environment:
<<: *airflow-env
volumes: *airflow-volumes
command: >
bash -c "
airflow db migrate &&
airflow scheduler"
healthcheck:
test: ["CMD-SHELL", "airflow jobs check --job-type SchedulerJob --hostname \"$$(hostname)\" || exit 1"]
interval: 15s
timeout: 15s
retries: 10
start_period: 90s
extra_hosts:
- "host.docker.internal:host-gateway"
zookeeper:
image: confluentinc/cp-zookeeper:7.4.0
container_name: stock-data-platform-zookeeper
restart: unless-stopped
ports:
- "2181:2181"
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
healthcheck:
# A port check, not `ruok`: the image's env translation drops
# 4lw.commands.whitelist, so the four-letter commands stay disabled.
test: ["CMD-SHELL", "nc -z localhost 2181"]
interval: 5s
timeout: 5s
retries: 20
kafka:
image: confluentinc/cp-kafka:7.4.0
container_name: stock-data-platform-kafka
# Zookeeper keeps its data in the container layer, so a stop/start replays
# the previous run's ephemeral /brokers/ids/1 znode and holds it until the
# old session times out. A broker starting inside that window dies with
# NodeExistsException, and with the default restart policy it stayed dead
# while the producer and consumer logged NoBrokersAvailable forever.
# Retry until Zookeeper lets the registration through: a hard-killed
# Zookeeper needs three or four broker attempts before the old session
# expires. Bounded, so a genuine misconfiguration still stops.
restart: on-failure:10
depends_on:
zookeeper:
condition: service_healthy
ports:
- "9092:9092"
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://stock-data-platform-kafka:9092
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
healthcheck:
test: ["CMD-SHELL", "kafka-broker-api-versions --bootstrap-server localhost:9092 >/dev/null 2>&1"]
interval: 10s
timeout: 10s
retries: 6
start_period: 30s
kafka-producer:
build:
context: .
dockerfile: Dockerfile.kafka
args:
SCRIPT: live_from_kafka.py
container_name: stock-data-platform-kafka-producer
# service_healthy, not the short-form list: kafka has a healthcheck, and a
# plain `- kafka` only waits for the container to start, not for the broker
# to accept connections. Both services logged NoBrokersAvailable through
# their retry loop on every boot, and with no restart policy an exhausted
# loop left streaming down until someone noticed.
depends_on:
kafka:
condition: service_healthy
timescaledb:
condition: service_healthy
environment:
<<: *db-env
KAFKA_BOOTSTRAP: ${KAFKA_BOOTSTRAP:-stock-data-platform-kafka:9092}
KAFKA_TOPIC: ${KAFKA_TOPIC:-stock-data}
STOCK_TICKERS: ${STOCK_TICKERS:-AAPL,AMZN,DIS,GOOG,JPM,META,MSFT,NFLX,NVDA,TSLA}
restart: unless-stopped
kafka-consumer:
build:
context: .
dockerfile: Dockerfile.kafka
args:
SCRIPT: kafka_to_postgres.py
container_name: stock-data-platform-kafka-consumer
# service_healthy, not the short-form list: kafka has a healthcheck, and a
# plain `- kafka` only waits for the container to start, not for the broker
# to accept connections. Both services logged NoBrokersAvailable through
# their retry loop on every boot, and with no restart policy an exhausted
# loop left streaming down until someone noticed.
depends_on:
kafka:
condition: service_healthy
timescaledb:
condition: service_healthy
environment:
<<: *db-env
KAFKA_BOOTSTRAP: ${KAFKA_BOOTSTRAP:-stock-data-platform-kafka:9092}
KAFKA_TOPIC: ${KAFKA_TOPIC:-stock-data}
restart: unless-stopped