Severian commited on
Commit
8f2f82d
1 Parent(s): 0848c81

fix: improve database initialization and connection handling

Browse files
Files changed (2) hide show
  1. docker/docker-compose.yaml +9 -14
  2. 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
- PGUSER: ${PGUSER:-postgres}
331
- POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-difyai123456}
332
- POSTGRES_DB: ${POSTGRES_DB:-dify}
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: ['CMD', 'pg_isready']
344
- interval: 1s
345
- timeout: 3s
346
- retries: 30
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
- # Set database connection variables from environment
7
- export PGHOST="${DB_HOST:-db}"
8
- export PGPORT="${DB_PORT:-5432}"
9
- export PGUSER="${DB_USERNAME:-postgres}"
10
- export PGPASSWORD="${DB_PASSWORD:-difyai123456}"
11
- export PGDATABASE="${DB_DATABASE:-dify}"
12
 
13
- # Wait for PostgreSQL with proper credentials
14
- until pg_isready -h "$PGHOST" -p "$PGPORT" -U "$PGUSER"; do
15
- echo "PostgreSQL is unavailable - sleeping"
16
- sleep 2
 
 
 
 
17
  done
 
18
 
19
- # Wait for Redis with proper credentials
20
- until redis-cli -h "${REDIS_HOST:-redis}" -p "${REDIS_PORT:-6379}" \
21
- -a "${REDIS_PASSWORD:-difyai123456}" ping; do
22
- echo "Redis is unavailable - sleeping"
23
- sleep 2
24
  done
 
25
 
26
  # Initialize database if needed
27
  cd /app/api
28
  if [ ! -f ".db_initialized" ]; then
29
- echo "Initializing database..."
30
  flask db upgrade
31
- touch .db_initialized
 
 
 
 
 
 
32
  fi
33
 
34
- # Start API server in background
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 \