Severian commited on
Commit
d2e32da
·
verified ·
1 Parent(s): 65d2943

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +156 -91
Dockerfile CHANGED
@@ -48,7 +48,17 @@ http {
48
  include /etc/nginx/mime.types;
49
  default_type application/octet-stream;
50
  sendfile on;
51
- keepalive_timeout 65;
 
 
 
 
 
 
 
 
 
 
52
 
53
  upstream frontend {
54
  server 127.0.0.1:3000;
@@ -63,36 +73,43 @@ http {
63
  server_name _;
64
  client_max_body_size 100M;
65
 
 
 
 
 
 
66
  location / {
67
  proxy_pass http://frontend;
68
  proxy_http_version 1.1;
69
- proxy_set_header Upgrade "\$http_upgrade";
70
  proxy_set_header Connection "upgrade";
71
- proxy_set_header Host "\$host";
72
- proxy_cache_bypass "\$http_upgrade";
73
- proxy_set_header X-Real-IP "\$remote_addr";
74
- proxy_set_header X-Forwarded-For "\$proxy_add_x_forwarded_for";
75
- proxy_set_header X-Forwarded-Proto "\$scheme";
76
  proxy_read_timeout 300s;
77
- proxy_connect_timeout 75s;
 
78
  }
79
 
80
  location /api {
81
  proxy_pass http://backend;
82
  proxy_http_version 1.1;
83
- proxy_set_header Upgrade "\$http_upgrade";
84
  proxy_set_header Connection "upgrade";
85
- proxy_set_header Host "\$host";
86
- proxy_cache_bypass "\$http_upgrade";
87
- proxy_set_header X-Real-IP "\$remote_addr";
88
- proxy_set_header X-Forwarded-For "\$proxy_add_x_forwarded_for";
89
- proxy_set_header X-Forwarded-Proto "\$scheme";
90
  proxy_read_timeout 300s;
91
- proxy_connect_timeout 75s;
 
92
  }
93
 
94
  access_log /var/log/nginx/access.log;
95
- error_log /var/log/nginx/error.log;
96
  }
97
  }
98
  EOF
@@ -173,81 +190,129 @@ ENV FLASK_APP=app.py \
173
  EXPOSE 7860
174
 
175
  # Create startup script
176
- RUN echo '#!/bin/bash\n\
177
- set -e\n\
178
- echo "===== Application Startup at $(date "+%Y-%m-%d %H:%M:%S") ====="\n\
179
- \n\
180
- # Initialize PostgreSQL database if not already initialized\n\
181
- if [ ! -f "$PGDATA/PG_VERSION" ]; then\n\
182
- echo "Initializing PostgreSQL database..."\n\
183
- initdb --username=user --pwfile=<(echo "$DB_PASSWORD") --auth=md5 --encoding=UTF8\n\
184
- \n\
185
- # Configure PostgreSQL\n\
186
- echo "local all all trust" > "$PGDATA/pg_hba.conf"\n\
187
- echo "host all all 127.0.0.1/32 md5" >> "$PGDATA/pg_hba.conf"\n\
188
- echo "host all all ::1/128 md5" >> "$PGDATA/pg_hba.conf"\n\
189
- echo "host all all 0.0.0.0/0 md5" >> "$PGDATA/pg_hba.conf"\n\
190
- \n\
191
- echo "listen_addresses = '\''*'\''" >> "$PGDATA/postgresql.conf"\n\
192
- echo "max_connections = 100" >> "$PGDATA/postgresql.conf"\n\
193
- echo "shared_buffers = 128MB" >> "$PGDATA/postgresql.conf"\n\
194
- fi\n\
195
- \n\
196
- # Start PostgreSQL with detailed logging\n\
197
- echo "Starting PostgreSQL server..."\n\
198
- pg_ctl start -D "$PGDATA" -l /var/log/postgresql/postgresql.log -o "-c logging_collector=on -c log_directory='\''/var/log/postgresql'\'' -c log_filename='\''postgresql-%Y-%m-%d_%H%M%S.log'\'' -c log_statement='\''all'\''" -w\n\
199
- \n\
200
- # Wait for PostgreSQL to start and show logs if there are issues\n\
201
- max_tries=30\n\
202
- count=0\n\
203
- echo "Checking database connection..."\n\
204
- until pg_isready -h localhost -p 5432; do\n\
205
- if [ $count -eq 0 ]; then\n\
206
- echo "PostgreSQL logs:"\n\
207
- tail -n 50 /var/log/postgresql/postgresql.log\n\
208
- fi\n\
209
- echo "Waiting for database connection... (${count}/${max_tries})"\n\
210
- sleep 2\n\
211
- count=$((count+1))\n\
212
- if [ $count -gt $max_tries ]; then\n\
213
- echo "Failed to connect to database after ${max_tries} attempts"\n\
214
- echo "Last 100 lines of PostgreSQL logs:"\n\
215
- tail -n 100 /var/log/postgresql/postgresql.log\n\
216
- exit 1\n\
217
- fi\n\
218
- done\n\
219
- \n\
220
- # Create database if it doesn'\''t exist\n\
221
- if ! psql -lqt | cut -d \| -f 1 | grep -qw dify; then\n\
222
- echo "Creating database dify..."\n\
223
- createdb -U user dify\n\
224
- fi\n\
225
- \n\
226
- echo "Database connection successful"\n\
227
- \n\
228
- # Start application services\n\
229
- cd /app/api && poetry run python -m flask db upgrade\n\
230
- \n\
231
- # Start API server\n\
232
- cd /app/api && poetry run python -m gunicorn app:app \\\n\
233
- --bind ${DIFY_BIND_ADDRESS:-127.0.0.1}:${DIFY_PORT:-5001} \\\n\
234
- --worker-class gevent \\\n\
235
- --workers 1 \\\n\
236
- --timeout 300 \\\n\
237
- --preload &\n\
238
- \n\
239
- # Start frontend server\n\
240
- cd /app/web && PORT=3000 node server.js &\n\
241
- \n\
242
- # Wait for services to be ready\n\
243
- echo "Waiting for services to be ready..."\n\
244
- sleep 10\n\
245
- \n\
246
- # Start nginx with debug logging\n\
247
- nginx -g "daemon off; error_log /var/log/nginx/error.log debug;" &\n\
248
- \n\
249
- wait' > /app/entrypoint.sh && \
250
- chmod +x /app/entrypoint.sh
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
251
 
252
  WORKDIR /app
253
 
 
48
  include /etc/nginx/mime.types;
49
  default_type application/octet-stream;
50
  sendfile on;
51
+ keepalive_timeout 120;
52
+
53
+ # Added buffer size configurations
54
+ proxy_buffer_size 128k;
55
+ proxy_buffers 4 256k;
56
+ proxy_busy_buffers_size 256k;
57
+
58
+ # Added timeout configurations
59
+ proxy_connect_timeout 120s;
60
+ proxy_read_timeout 120s;
61
+ proxy_send_timeout 120s;
62
 
63
  upstream frontend {
64
  server 127.0.0.1:3000;
 
73
  server_name _;
74
  client_max_body_size 100M;
75
 
76
+ # Increased buffer sizes
77
+ proxy_buffer_size 128k;
78
+ proxy_buffers 4 256k;
79
+ proxy_busy_buffers_size 256k;
80
+
81
  location / {
82
  proxy_pass http://frontend;
83
  proxy_http_version 1.1;
84
+ proxy_set_header Upgrade "$http_upgrade";
85
  proxy_set_header Connection "upgrade";
86
+ proxy_set_header Host "$host";
87
+ proxy_cache_bypass "$http_upgrade";
88
+ proxy_set_header X-Real-IP "$remote_addr";
89
+ proxy_set_header X-Forwarded-For "$proxy_add_x_forwarded_for";
90
+ proxy_set_header X-Forwarded-Proto "$scheme";
91
  proxy_read_timeout 300s;
92
+ proxy_connect_timeout 120s;
93
+ proxy_send_timeout 120s;
94
  }
95
 
96
  location /api {
97
  proxy_pass http://backend;
98
  proxy_http_version 1.1;
99
+ proxy_set_header Upgrade "$http_upgrade";
100
  proxy_set_header Connection "upgrade";
101
+ proxy_set_header Host "$host";
102
+ proxy_cache_bypass "$http_upgrade";
103
+ proxy_set_header X-Real-IP "$remote_addr";
104
+ proxy_set_header X-Forwarded-For "$proxy_add_x_forwarded_for";
105
+ proxy_set_header X-Forwarded-Proto "$scheme";
106
  proxy_read_timeout 300s;
107
+ proxy_connect_timeout 120s;
108
+ proxy_send_timeout 120s;
109
  }
110
 
111
  access_log /var/log/nginx/access.log;
112
+ error_log /var/log/nginx/error.log debug;
113
  }
114
  }
115
  EOF
 
190
  EXPOSE 7860
191
 
192
  # Create startup script
193
+ COPY <<-'EOT' /app/entrypoint.sh
194
+ #!/bin/bash
195
+ set -e
196
+ echo "===== Application Startup at $(date "+%Y-%m-%d %H:%M:%S") ====="
197
+
198
+ # Function to check if a service is ready
199
+ check_service() {
200
+ local service=$1
201
+ local url=$2
202
+ local max_attempts=$3
203
+ local wait_time=$4
204
+
205
+ echo "Checking $service..."
206
+ for i in $(seq 1 $max_attempts); do
207
+ if curl -s "$url" >/dev/null; then
208
+ echo "$service is ready"
209
+ return 0
210
+ fi
211
+ echo "Waiting for $service (attempt $i/$max_attempts)..."
212
+ sleep $wait_time
213
+ done
214
+ echo "$service failed to start"
215
+ return 1
216
+ }
217
+
218
+ # Initialize PostgreSQL database if not already initialized
219
+ if [ ! -f "$PGDATA/PG_VERSION" ]; then
220
+ echo "Initializing PostgreSQL database..."
221
+ initdb --username=user --pwfile=<(echo "$DB_PASSWORD") --auth=md5 --encoding=UTF8
222
+
223
+ # Configure PostgreSQL
224
+ echo "local all all trust" > "$PGDATA/pg_hba.conf"
225
+ echo "host all all 127.0.0.1/32 md5" >> "$PGDATA/pg_hba.conf"
226
+ echo "host all all ::1/128 md5" >> "$PGDATA/pg_hba.conf"
227
+ echo "host all all 0.0.0.0/0 md5" >> "$PGDATA/pg_hba.conf"
228
+
229
+ echo "listen_addresses = '*'" >> "$PGDATA/postgresql.conf"
230
+ echo "max_connections = 100" >> "$PGDATA/postgresql.conf"
231
+ echo "shared_buffers = 128MB" >> "$PGDATA/postgresql.conf"
232
+ echo "work_mem = 16MB" >> "$PGDATA/postgresql.conf"
233
+ echo "maintenance_work_mem = 128MB" >> "$PGDATA/postgresql.conf"
234
+ echo "effective_cache_size = 512MB" >> "$PGDATA/postgresql.conf"
235
+ fi
236
+
237
+ # Start PostgreSQL with detailed logging
238
+ echo "Starting PostgreSQL server..."
239
+ pg_ctl start -D "$PGDATA" -l /var/log/postgresql/postgresql.log -o "-c logging_collector=on -c log_directory='/var/log/postgresql' -c log_filename='postgresql-%Y-%m-%d_%H%M%S.log' -c log_statement='all'" -w
240
+
241
+ # Wait for PostgreSQL to start and show logs if there are issues
242
+ max_tries=30
243
+ count=0
244
+ echo "Checking database connection..."
245
+ until pg_isready -h localhost -p 5432; do
246
+ if [ $count -eq 0 ]; then
247
+ echo "PostgreSQL logs:"
248
+ tail -n 50 /var/log/postgresql/postgresql.log
249
+ fi
250
+ echo "Waiting for database connection... (${count}/${max_tries})"
251
+ sleep 2
252
+ count=$((count+1))
253
+ if [ $count -gt $max_tries ]; then
254
+ echo "Failed to connect to database after ${max_tries} attempts"
255
+ echo "Last 100 lines of PostgreSQL logs:"
256
+ tail -n 100 /var/log/postgresql/postgresql.log
257
+ exit 1
258
+ fi
259
+ done
260
+
261
+ # Create database if it doesn't exist
262
+ if ! psql -lqt | cut -d \| -f 1 | grep -qw dify; then
263
+ echo "Creating database dify..."
264
+ createdb -U user dify
265
+ fi
266
+
267
+ echo "Database connection successful"
268
+
269
+ # Start application services
270
+ cd /app/api && poetry run python -m flask db upgrade
271
+
272
+ # Start API server
273
+ cd /app/api && poetry run python -m gunicorn app:app \
274
+ --bind ${DIFY_BIND_ADDRESS:-127.0.0.1}:${DIFY_PORT:-5001} \
275
+ --worker-class gevent \
276
+ --workers 1 \
277
+ --timeout 300 \
278
+ --preload \
279
+ --access-logfile - \
280
+ --error-logfile - &
281
+
282
+ # Wait for API to be ready
283
+ echo "Waiting for API server to be ready..."
284
+ check_service "API" "http://127.0.0.1:5001/api/health" 30 2
285
+
286
+ # Start frontend server
287
+ cd /app/web && PORT=3000 node server.js &
288
+
289
+ # Wait for frontend to be ready
290
+ echo "Waiting for frontend server to be ready..."
291
+ check_service "Frontend" "http://127.0.0.1:3000" 30 2
292
+
293
+ # Start nginx with debug logging
294
+ echo "Starting nginx..."
295
+ nginx -g "daemon off; error_log /var/log/nginx/error.log debug;" &
296
+
297
+ # Monitor all processes
298
+ while true; do
299
+ if ! pgrep -f "gunicorn" > /dev/null; then
300
+ echo "API server died"
301
+ exit 1
302
+ fi
303
+ if ! pgrep -f "node server.js" > /dev/null; then
304
+ echo "Frontend server died"
305
+ exit 1
306
+ fi
307
+ if ! pgrep -f "nginx" > /dev/null; then
308
+ echo "Nginx died"
309
+ exit 1
310
+ fi
311
+ sleep 30
312
+ done
313
+ EOT
314
+
315
+ RUN chmod +x /app/entrypoint.sh
316
 
317
  WORKDIR /app
318