-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDockerfile.unified
More file actions
321 lines (271 loc) · 10.5 KB
/
Copy pathDockerfile.unified
File metadata and controls
321 lines (271 loc) · 10.5 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
# Unified Dockerfile for BrainKB Services
# Deploys: APItokenmanager, query_service, ml_service, and oxigraph
# Note: Oxigraph binary extraction skipped - oxigraph image is distroless
# Oxigraph will be run as a separate service in docker-compose or use the fallback script
# Main build stage
FROM python:3.10-slim
# Set metadata
LABEL project="BrainyPedia" \
maintainer="Tek Raj Chhetri <tekraj@mit.edu>" \
version="1.0" \
description="Unified Docker image for BrainKB services"
# Install system dependencies
RUN apt-get update && apt-get install -y \
gcc \
g++ \
postgresql-client \
libpq-dev \
supervisor \
curl \
wget \
openssl \
apache2-utils \
&& rm -rf /var/lib/apt/lists/*
# Create directories for all services
WORKDIR /app
# Copy APItokenmanager
COPY APItokenmanager/ /app/APItokenmanager/
WORKDIR /app/APItokenmanager
RUN pip install --upgrade pip && \
pip install -r requirements.txt && \
pip install python-decouple gunicorn
# Copy query_service
COPY query_service/ /app/query_service/
WORKDIR /app/query_service
RUN pip install -r requirements.txt
# Copy ml_service
COPY ml_service/ /app/ml_service/
WORKDIR /app/ml_service
RUN pip install --use-deprecated=legacy-resolver "structsense==0.0.4" || \
pip install --use-deprecated=legacy-resolver --no-deps "structsense==0.0.4" && \
pip install -r requirements.txt
# Create supervisor configuration
RUN mkdir -p /etc/supervisor/conf.d
RUN cat > /etc/supervisor/conf.d/supervisord.conf << 'EOF'
[unix_http_server]
file=/var/run/supervisor.sock
chmod=0700
[supervisord]
nodaemon=true
logfile=/var/log/supervisor/supervisord.log
pidfile=/var/run/supervisord.pid
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[program:api_tokenmanager]
command=gunicorn -b 0.0.0.0:8000 APIAuthManager.wsgi:application
directory=/app/APItokenmanager
autostart=true
autorestart=true
startsecs=10
stderr_logfile=/var/log/supervisor/api_tokenmanager.err.log
stdout_logfile=/var/log/supervisor/api_tokenmanager.out.log
environment=PATH="/usr/local/bin:/usr/bin:/bin"
priority=100
[program:query_service]
command=gunicorn core.main:app --bind 0.0.0.0:8010 --workers 6 --worker-class uvicorn.workers.UvicornWorker --threads 2 --timeout 300 --keep-alive 120 --max-requests 1000 --max-requests-jitter 50
directory=/app/query_service
autostart=true
autorestart=true
startsecs=30
stderr_logfile=/var/log/supervisor/query_service.err.log
stdout_logfile=/var/log/supervisor/query_service.out.log
environment=PATH="/usr/local/bin:/usr/bin:/bin",WEB_CONCURRENCY="6"
startretries=10
stopwaitsecs=10
priority=200
[program:ml_service]
command=gunicorn core.main:app --bind 0.0.0.0:8007 --workers 6 --worker-class uvicorn.workers.UvicornWorker --threads 2 --max-requests 1000 --max-requests-jitter 50 --timeout 220 --keep-alive 220 --log-level debug
directory=/app/ml_service
autostart=true
autorestart=true
startsecs=15
stderr_logfile=/var/log/supervisor/ml_service.err.log
stdout_logfile=/var/log/supervisor/ml_service.out.log
environment=PATH="/usr/local/bin:/usr/bin:/bin",WEB_CONCURRENCY="6"
[program:oxigraph]
command=/app/oxigraph/oxigraph_server --bind 0.0.0.0:7878 --storage /data
directory=/app/oxigraph
autostart=false
autorestart=false
stderr_logfile=/var/log/supervisor/oxigraph.err.log
stdout_logfile=/var/log/supervisor/oxigraph.out.log
environment=PATH="/usr/local/bin:/usr/bin:/bin",TMPDIR="/tmp"
startsecs=0
startretries=0
EOF
# Create directories for logs and data
# Ensure supervisor socket directory exists and is writable
RUN mkdir -p /var/log/supervisor /data /app/oxigraph /var/run && \
chmod 777 /var/run /var/log/supervisor
# Create oxigraph fallback script
# Note: Oxigraph binary extraction from distroless image is not possible
# Oxigraph should be run as a separate service in docker-compose
RUN echo '#!/bin/bash' > /app/oxigraph/oxigraph_server && \
echo 'echo "Oxigraph is not available in this container. Please run oxigraph as a separate service."' >> /app/oxigraph/oxigraph_server && \
echo 'echo "You can add it to docker-compose.unified.yml or run it separately."' >> /app/oxigraph/oxigraph_server && \
echo 'exit 1' >> /app/oxigraph/oxigraph_server && \
chmod +x /app/oxigraph/oxigraph_server
# Create healthcheck script
RUN cat > /app/healthcheck.sh << 'EOF'
#!/bin/bash
# Healthcheck script to verify all services are running
# Check if supervisor is running
if ! pgrep supervisord > /dev/null; then
echo "ERROR: supervisord is not running"
exit 1
fi
# Check if supervisor socket is accessible
if ! supervisorctl status > /dev/null 2>&1; then
echo "ERROR: Cannot connect to supervisor"
exit 1
fi
# Check each service status
for service in api_tokenmanager query_service ml_service; do
status=$(supervisorctl status $service 2>/dev/null | awk '{print $2}')
if [ "$status" != "RUNNING" ]; then
echo "ERROR: Service $service is not running (status: $status)"
echo "Check logs at: /var/log/supervisor/${service}.err.log"
exit 1
fi
done
# Check if services are listening on their ports
# Give services time to bind to ports if they just started
sleep 2
# Check API Token Manager (port 8000)
if ! curl -s -f http://localhost:8000/ > /dev/null 2>&1; then
echo "WARNING: API Token Manager (port 8000) is not responding"
fi
# Check Query Service (port 8010)
if ! curl -s -f http://localhost:8010/api/ > /dev/null 2>&1; then
echo "WARNING: Query Service (port 8010) is not responding"
fi
# Check ML Service (port 8007)
if ! curl -s -f http://localhost:8007/api/ > /dev/null 2>&1; then
echo "WARNING: ML Service (port 8007) is not responding"
fi
# If we got here, at least supervisor and services are running
exit 0
EOF
RUN chmod +x /app/healthcheck.sh
# Create startup script
RUN cat > /app/start.sh << 'EOF'
#!/bin/bash
set -e
echo "=========================================="
echo "BrainKB Unified Container Startup"
echo "=========================================="
echo ""
# Check critical environment variables
echo "Checking environment configuration..."
MISSING_VARS=()
# Check JWT/Database configuration
if [ -z "$JWT_POSTGRES_DATABASE_USER" ] && [ -z "$DB_USER" ]; then
MISSING_VARS+=("JWT_POSTGRES_DATABASE_USER or DB_USER")
fi
if [ -z "$JWT_POSTGRES_DATABASE_PASSWORD" ] && [ -z "$DB_PASSWORD" ]; then
MISSING_VARS+=("JWT_POSTGRES_DATABASE_PASSWORD or DB_PASSWORD")
fi
if [ -z "$QUERY_SERVICE_JWT_SECRET_KEY" ]; then
MISSING_VARS+=("QUERY_SERVICE_JWT_SECRET_KEY")
fi
if [ -z "$ML_SERVICE_JWT_SECRET_KEY" ]; then
MISSING_VARS+=("ML_SERVICE_JWT_SECRET_KEY")
fi
if [ ${#MISSING_VARS[@]} -gt 0 ]; then
echo "WARNING: Missing critical environment variables:"
for var in "${MISSING_VARS[@]}"; do
echo " - $var"
done
echo ""
echo "Services may fail to start without these variables."
echo "Please ensure your .env file is properly configured."
echo ""
fi
# Note: Oxigraph runs as a separate service in docker-compose
# Ensure supervisor socket directory exists and is writable
mkdir -p /var/run
chmod 755 /var/run
# Wait for PostgreSQL to be ready
# Use JWT_POSTGRES_DATABASE_USER if available, otherwise fall back to DB_USER
# This ensures we use the same user that PostgreSQL was initialized with
PG_USER="${JWT_POSTGRES_DATABASE_USER:-${DB_USER:-postgres}}"
PG_PASSWORD="${JWT_POSTGRES_DATABASE_PASSWORD:-${DB_PASSWORD}}"
PG_HOST="${JWT_POSTGRES_DATABASE_HOST_URL:-${DB_HOST:-postgres}}"
PG_DB="${JWT_POSTGRES_DATABASE_NAME:-${DB_NAME:-brainkb}}"
echo "Waiting for PostgreSQL to be ready..."
echo "Connecting as user: ${PG_USER} to database: ${PG_DB} on host: ${PG_HOST}"
until PGPASSWORD="$PG_PASSWORD" psql -h "$PG_HOST" -U "$PG_USER" -d "$PG_DB" -c '\q' 2>/dev/null; do
echo "PostgreSQL is unavailable - sleeping"
sleep 2
done
echo "PostgreSQL is ready!"
# Note: Oxigraph is optional - services will handle connection failures gracefully
# No need to wait for it here - services will retry when needed
# Run Django migrations for APItokenmanager if needed
cd /app/APItokenmanager
if [ -f .env ] || [ -n "$DB_NAME" ]; then
echo "Running Django migrations..."
python manage.py makemigrations || true
python manage.py migrate || true
python manage.py collectstatic --noinput || true
# Create superuser if credentials are provided and user doesn't exist
if [ -n "$DJANGO_SUPERUSER_USERNAME" ] && [ -n "$DJANGO_SUPERUSER_EMAIL" ] && [ -n "$DJANGO_SUPERUSER_PASSWORD" ]; then
echo "Creating Django superuser..."
export DJANGO_SUPERUSER_USERNAME DJANGO_SUPERUSER_EMAIL DJANGO_SUPERUSER_PASSWORD
python manage.py shell << 'PYTHON_SCRIPT' || true
import os
from django.contrib.auth import get_user_model
User = get_user_model()
username = os.environ.get('DJANGO_SUPERUSER_USERNAME')
email = os.environ.get('DJANGO_SUPERUSER_EMAIL')
password = os.environ.get('DJANGO_SUPERUSER_PASSWORD')
if username and email and password:
if not User.objects.filter(username=username).exists():
User.objects.create_superuser(username, email, password)
print(f'Superuser {username} created successfully')
else:
print(f'Superuser {username} already exists')
else:
print('Error: Missing superuser credentials in environment')
PYTHON_SCRIPT
else
echo "Warning: DJANGO_SUPERUSER credentials not provided. Superuser not created."
echo "You can create one manually with: python manage.py createsuperuser"
fi
echo "Django migrations completed"
fi
# Ensure supervisor socket directory exists and is writable (in case /var/run is tmpfs)
mkdir -p /var/run
chmod 755 /var/run
# Start supervisor
echo ""
echo "Starting all services via supervisord..."
echo "=========================================="
echo ""
echo "Services being started:"
echo " - API Token Manager (port 8000)"
echo " - Query Service (port 8010)"
echo " - ML Service (port 8007)"
echo ""
echo "Logs are available at:"
echo " - /var/log/supervisor/api_tokenmanager.err.log"
echo " - /var/log/supervisor/query_service.err.log"
echo " - /var/log/supervisor/ml_service.err.log"
echo ""
echo "From host, view logs with:"
echo " docker exec brainkb-unified tail -f /var/log/supervisor/query_service.err.log"
echo ""
echo "=========================================="
echo ""
# Use our config file that includes socket configuration
exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
EOF
RUN chmod +x /app/start.sh
# Expose ports
EXPOSE 8000 8007 8010
# Set working directory
WORKDIR /app
# Default command
CMD ["/app/start.sh"]