fix: improve database initialization and connection handling
Browse files- docker/docker-compose.yaml +9 -14
- docker/entrypoint.sh +27 -18
docker/docker-compose.yaml
CHANGED
@@ -326,24 +326,19 @@ services:
|
|
326 |
db:
|
327 |
image: postgres:15-alpine
|
328 |
restart: always
|
|
|
|
|
329 |
environment:
|
330 |
-
|
331 |
-
POSTGRES_PASSWORD: ${
|
332 |
-
POSTGRES_DB: ${
|
333 |
-
PGDATA: ${PGDATA:-/var/lib/postgresql/data/pgdata}
|
334 |
-
command: >
|
335 |
-
postgres -c 'max_connections=${POSTGRES_MAX_CONNECTIONS:-100}'
|
336 |
-
-c 'shared_buffers=${POSTGRES_SHARED_BUFFERS:-128MB}'
|
337 |
-
-c 'work_mem=${POSTGRES_WORK_MEM:-4MB}'
|
338 |
-
-c 'maintenance_work_mem=${POSTGRES_MAINTENANCE_WORK_MEM:-64MB}'
|
339 |
-
-c 'effective_cache_size=${POSTGRES_EFFECTIVE_CACHE_SIZE:-4096MB}'
|
340 |
volumes:
|
341 |
- ./volumes/db/data:/var/lib/postgresql/data
|
342 |
healthcheck:
|
343 |
-
test: [
|
344 |
-
interval:
|
345 |
-
timeout:
|
346 |
-
retries:
|
347 |
|
348 |
# The redis cache.
|
349 |
redis:
|
|
|
326 |
db:
|
327 |
image: postgres:15-alpine
|
328 |
restart: always
|
329 |
+
networks:
|
330 |
+
- dify_network
|
331 |
environment:
|
332 |
+
POSTGRES_USER: ${DB_USERNAME:-postgres}
|
333 |
+
POSTGRES_PASSWORD: ${DB_PASSWORD:-difyai123456}
|
334 |
+
POSTGRES_DB: ${DB_DATABASE:-dify}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
335 |
volumes:
|
336 |
- ./volumes/db/data:/var/lib/postgresql/data
|
337 |
healthcheck:
|
338 |
+
test: ["CMD-SHELL", "pg_isready -U ${DB_USERNAME:-postgres}"]
|
339 |
+
interval: 5s
|
340 |
+
timeout: 5s
|
341 |
+
retries: 5
|
342 |
|
343 |
# The redis cache.
|
344 |
redis:
|
docker/entrypoint.sh
CHANGED
@@ -3,35 +3,44 @@ set -e
|
|
3 |
|
4 |
echo "Starting Dify services..."
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
export PGPASSWORD="${DB_PASSWORD:-difyai123456}"
|
11 |
-
export PGDATABASE="${DB_DATABASE:-dify}"
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
17 |
done
|
|
|
18 |
|
19 |
-
|
20 |
-
until
|
21 |
-
|
22 |
-
|
23 |
-
sleep 2
|
24 |
done
|
|
|
25 |
|
26 |
# Initialize database if needed
|
27 |
cd /app/api
|
28 |
if [ ! -f ".db_initialized" ]; then
|
29 |
-
echo "
|
30 |
flask db upgrade
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
fi
|
33 |
|
34 |
-
# Start
|
35 |
echo "Starting API server on port 7860..."
|
36 |
gunicorn --bind 0.0.0.0:7860 \
|
37 |
--workers 1 \
|
|
|
3 |
|
4 |
echo "Starting Dify services..."
|
5 |
|
6 |
+
# Enhanced database connection check
|
7 |
+
check_postgres() {
|
8 |
+
pg_isready -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USERNAME}" > /dev/null 2>&1
|
9 |
+
}
|
|
|
|
|
10 |
|
11 |
+
check_redis() {
|
12 |
+
redis-cli -h "${REDIS_HOST}" -p "${REDIS_PORT}" -a "${REDIS_PASSWORD}" ping > /dev/null 2>&1
|
13 |
+
}
|
14 |
+
|
15 |
+
echo "Waiting for PostgreSQL to be ready..."
|
16 |
+
until check_postgres; do
|
17 |
+
echo "PostgreSQL is unavailable (host: ${DB_HOST}, port: ${DB_PORT}) - retrying..."
|
18 |
+
sleep 5
|
19 |
done
|
20 |
+
echo "PostgreSQL is ready!"
|
21 |
|
22 |
+
echo "Waiting for Redis to be ready..."
|
23 |
+
until check_redis; do
|
24 |
+
echo "Redis is unavailable (host: ${REDIS_HOST}, port: ${REDIS_PORT}) - retrying..."
|
25 |
+
sleep 5
|
|
|
26 |
done
|
27 |
+
echo "Redis is ready!"
|
28 |
|
29 |
# Initialize database if needed
|
30 |
cd /app/api
|
31 |
if [ ! -f ".db_initialized" ]; then
|
32 |
+
echo "Running database migrations..."
|
33 |
flask db upgrade
|
34 |
+
if [ $? -eq 0 ]; then
|
35 |
+
touch .db_initialized
|
36 |
+
echo "Database initialization completed successfully"
|
37 |
+
else
|
38 |
+
echo "Database initialization failed"
|
39 |
+
exit 1
|
40 |
+
fi
|
41 |
fi
|
42 |
|
43 |
+
# Start services
|
44 |
echo "Starting API server on port 7860..."
|
45 |
gunicorn --bind 0.0.0.0:7860 \
|
46 |
--workers 1 \
|