forked from pipeshub-ai/pipeshub-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile.integration
More file actions
340 lines (286 loc) · 10.2 KB
/
Copy pathDockerfile.integration
File metadata and controls
340 lines (286 loc) · 10.2 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# -----------------------------------------------------------------------------
# Stage 1: Build Base - Contains all build tools (NOT in final image)
# -----------------------------------------------------------------------------
FROM python:3.12-slim AS build-base
ENV DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC
RUN apt-get update && apt-get install -y --no-install-recommends \
curl \
build-essential \
gnupg \
wget \
librocksdb-dev \
libgflags-dev \
libsnappy-dev \
zlib1g-dev \
libbz2-dev \
liblz4-dev \
libzstd-dev \
libssl-dev \
libspatialindex-dev \
libmariadb-dev \
git \
&& rm -rf /var/lib/apt/lists/*
# Install Rust with minimal profile (only needed for building certain pip packages)
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y --profile minimal --default-toolchain stable
ENV PATH="/root/.cargo/bin:${PATH}"
# Install uv for faster pip operations
RUN pip install --no-cache-dir uv
# -----------------------------------------------------------------------------
# Stage 2: Python Dependencies Build
# -----------------------------------------------------------------------------
FROM build-base AS python-deps
WORKDIR /app/python
COPY ./backend/python/pyproject.toml ./
# Install Python dependencies
RUN uv pip install --system -e . && \
# Download ML models
python -m spacy download en_core_web_sm && \
python -c "import nltk; nltk.download('punkt', quiet=True)" && \
python -c "from sentence_transformers import CrossEncoder; CrossEncoder(model_name='BAAI/bge-reranker-base')" && \
# Clean up caches to save space
rm -rf /root/.cache/pip /root/.cache/uv /tmp/*
# -----------------------------------------------------------------------------
# Stage 3: Node.js Backend Build
# -----------------------------------------------------------------------------
FROM node:20-slim AS nodejs-backend
WORKDIR /app/backend
COPY backend/nodejs/apps/package*.json ./
COPY backend/nodejs/apps/tsconfig.json ./
# Install dependencies with architecture handling
RUN set -e; \
ARCH=$(uname -m); \
echo "Building for architecture: $ARCH"; \
if [ "$ARCH" = "arm64" ] || [ "$ARCH" = "aarch64" ]; then \
echo "Detected ARM architecture"; \
npm install --ignore-scripts && \
npm uninstall jpeg-recompress-bin mozjpeg imagemin-mozjpeg 2>/dev/null || true && \
npm install sharp --save || true; \
else \
echo "Detected x86 architecture"; \
npm install; \
fi
COPY backend/nodejs/apps/src ./src
RUN npm run build && \
# Prune dev dependencies after build
npm prune --production && \
# Clean npm cache
npm cache clean --force
# -----------------------------------------------------------------------------
# Stage 4: Runtime Base - Minimal runtime dependencies only
# -----------------------------------------------------------------------------
FROM python:3.12-slim AS runtime-base
ENV DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC
# Install ONLY runtime dependencies (no build tools!)
RUN apt-get update && apt-get install -y --no-install-recommends \
# Basic utilities
curl \
ca-certificates \
# Network debugging (optional - remove if not needed)
iputils-ping \
dnsutils \
# Runtime libraries for RocksDB
libsnappy1v5 \
zlib1g \
liblz4-1 \
libzstd1 \
# Other runtime deps
libpq5 \
libmariadb3 \
# OpenGL library (required by Docling for PDF processing)
libgl1 \
libglib2.0-0 \
# OCR tools
ocrmypdf \
tesseract-ocr \
ghostscript \
unpaper \
qpdf \
# LibreOffice - MINIMAL install (only writer and calc, headless)
# Comment out if not needed - this is still ~300-400MB
libreoffice-writer-nogui \
libreoffice-calc-nogui \
libreoffice-impress-nogui \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# Install Node.js runtime only (not full dev environment)
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
apt-get install -y --no-install-recommends nodejs && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Install Neo4j Cypher Shell
RUN apt-get update && \
apt-get install -y --no-install-recommends gnupg && \
curl -fsSL https://debian.neo4j.com/neotechnology.gpg.key | gpg --dearmor -o /usr/share/keyrings/neo4j.gpg && \
echo "deb [signed-by=/usr/share/keyrings/neo4j.gpg] https://debian.neo4j.com stable latest" > /etc/apt/sources.list.d/neo4j.list && \
apt-get update && \
apt-get install -y --no-install-recommends cypher-shell && \
apt-get purge -y --auto-remove gnupg && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# -----------------------------------------------------------------------------
# Stage 5: Final Runtime Image
# -----------------------------------------------------------------------------
FROM runtime-base AS runtime
WORKDIR /app
# Copy Python site-packages from build stage
COPY --from=python-deps /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
COPY --from=python-deps /usr/local/bin /usr/local/bin
# Copy ML model data
COPY --from=python-deps /root/.cache/huggingface /root/.cache/huggingface
COPY --from=python-deps /root/nltk_data /root/nltk_data
# Copy Node.js backend (already pruned)
COPY --from=nodejs-backend /app/backend/dist ./backend/dist
COPY --from=nodejs-backend /app/backend/src/modules/mail ./backend/src/modules/mail
COPY --from=nodejs-backend /app/backend/src/modules/api-docs/pipeshub-openapi.yaml ./backend/src/modules/api-docs/pipeshub-openapi.yaml
COPY --from=nodejs-backend /app/backend/node_modules ./backend/dist/node_modules
# Copy Python application code
COPY backend/python/app/ /app/python/app/
# Copy the process monitor script
COPY <<'EOF' /app/process_monitor.sh
#!/bin/bash
# Process monitor script with parent-child process management
set -e
LOG_FILE="/app/process_monitor.log"
CHECK_INTERVAL=${CHECK_INTERVAL:-20}
NODEJS_PORT=${NODEJS_PORT:-3000}
# PIDs of child processes
NODEJS_PID=""
SLACKBOT_PID=""
DOCLING_PID=""
INDEXING_PID=""
CONNECTOR_PID=""
QUERY_PID=""
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
start_nodejs() {
log "Starting Node.js service..."
cd /app/backend
node dist/index.js &
NODEJS_PID=$!
log "Node.js started with PID: $NODEJS_PID"
log "Waiting for Node.js health check..."
local MAX_RETRIES=30
local RETRY_COUNT=0
local HEALTH_CHECK_URL="http://localhost:${NODEJS_PORT}/api/v1/health"
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
if curl -s -f "$HEALTH_CHECK_URL" > /dev/null 2>&1; then
log "Node.js health check passed!"
break
fi
RETRY_COUNT=$((RETRY_COUNT + 1))
log "Health check attempt $RETRY_COUNT/$MAX_RETRIES failed, retrying in 2 seconds..."
sleep 2
done
if [ $RETRY_COUNT -eq $MAX_RETRIES ]; then
log "ERROR: Node.js health check failed after $MAX_RETRIES attempts"
return 1
fi
}
start_slackbot() {
log "Starting Slack Bot service..."
cd /app/backend
node dist/integrations/slack-bot/src/index.js &
SLACKBOT_PID=$!
log "Slack Bot started with PID: $SLACKBOT_PID"
}
start_docling() {
log "Starting Docling service..."
cd /app/python
python -m app.docling_main &
DOCLING_PID=$!
log "Docling started with PID: $DOCLING_PID"
}
start_indexing() {
log "Starting Indexing service..."
cd /app/python
python -m app.indexing_main &
INDEXING_PID=$!
log "Indexing started with PID: $INDEXING_PID"
}
start_connector() {
log "Starting Connector service..."
cd /app/python
python -m app.connectors_main &
CONNECTOR_PID=$!
log "Connector started with PID: $CONNECTOR_PID"
log "Waiting for Connector health check..."
local MAX_RETRIES=30
local RETRY_COUNT=0
local HEALTH_CHECK_URL="http://localhost:8088/health"
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
if curl -s -f "$HEALTH_CHECK_URL" > /dev/null 2>&1; then
log "Connector health check passed!"
break
fi
RETRY_COUNT=$((RETRY_COUNT + 1))
log "Health check attempt $RETRY_COUNT/$MAX_RETRIES failed, retrying in 2 seconds..."
sleep 2
done
if [ $RETRY_COUNT -eq $MAX_RETRIES ]; then
log "ERROR: Connector health check failed after $MAX_RETRIES attempts"
return 1
fi
}
start_query() {
log "Starting Query service..."
cd /app/python
python -m app.query_main &
QUERY_PID=$!
log "Query started with PID: $QUERY_PID"
}
check_process() {
local pid=$1
local name=$2
if [ -z "$pid" ] || ! kill -0 "$pid" 2>/dev/null; then
log "WARNING: $name (PID: $pid) is not running!"
return 1
fi
return 0
}
cleanup() {
log "Shutting down all services..."
[ -n "$NODEJS_PID" ] && kill "$NODEJS_PID" 2>/dev/null || true
[ -n "$SLACKBOT_PID" ] && kill "$SLACKBOT_PID" 2>/dev/null || true
[ -n "$DOCLING_PID" ] && kill "$DOCLING_PID" 2>/dev/null || true
[ -n "$INDEXING_PID" ] && kill "$INDEXING_PID" 2>/dev/null || true
[ -n "$CONNECTOR_PID" ] && kill "$CONNECTOR_PID" 2>/dev/null || true
[ -n "$QUERY_PID" ] && kill "$QUERY_PID" 2>/dev/null || true
wait
log "All services stopped."
exit 0
}
trap cleanup SIGTERM SIGINT SIGQUIT
log "=== Process Monitor Starting ==="
start_nodejs
start_slackbot
start_connector
start_indexing
start_query
start_docling
log "All services started. Beginning monitoring cycle (checking every ${CHECK_INTERVAL}s)..."
while true; do
sleep "$CHECK_INTERVAL"
if ! check_process "$NODEJS_PID" "Node.js"; then
start_nodejs
fi
if [ -n "$SLACKBOT_PID" ] && ! check_process "$SLACKBOT_PID" "Slack Bot"; then
start_slackbot
fi
if ! check_process "$DOCLING_PID" "Docling"; then
start_docling
fi
if ! check_process "$INDEXING_PID" "Indexing"; then
start_indexing
fi
if ! check_process "$CONNECTOR_PID" "Connector"; then
start_connector
fi
if ! check_process "$QUERY_PID" "Query"; then
start_query
fi
done
EOF
RUN chmod +x /app/process_monitor.sh
EXPOSE 3000
CMD ["/app/process_monitor.sh"]