Update Dockerfile
Browse files- 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 "
|
70 |
proxy_set_header Connection "upgrade";
|
71 |
-
proxy_set_header Host "
|
72 |
-
proxy_cache_bypass "
|
73 |
-
proxy_set_header X-Real-IP "
|
74 |
-
proxy_set_header X-Forwarded-For "
|
75 |
-
proxy_set_header X-Forwarded-Proto "
|
76 |
proxy_read_timeout 300s;
|
77 |
-
proxy_connect_timeout
|
|
|
78 |
}
|
79 |
|
80 |
location /api {
|
81 |
proxy_pass http://backend;
|
82 |
proxy_http_version 1.1;
|
83 |
-
proxy_set_header Upgrade "
|
84 |
proxy_set_header Connection "upgrade";
|
85 |
-
proxy_set_header Host "
|
86 |
-
proxy_cache_bypass "
|
87 |
-
proxy_set_header X-Real-IP "
|
88 |
-
proxy_set_header X-Forwarded-For "
|
89 |
-
proxy_set_header X-Forwarded-Proto "
|
90 |
proxy_read_timeout 300s;
|
91 |
-
proxy_connect_timeout
|
|
|
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 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
echo "
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
echo "
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
echo "
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
echo "
|
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 |
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 |
|