-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdocker-entrypoint.sh
More file actions
70 lines (61 loc) · 1.86 KB
/
docker-entrypoint.sh
File metadata and controls
70 lines (61 loc) · 1.86 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
#!/bin/bash
set -e
# Wait for database to be ready
echo "Waiting for database..."
python << END
import sys
import time
import psycopg2
import os
# Get database connection details from environment variables
host = os.environ.get("DB_HOST", "localhost")
port = os.environ.get("DB_PORT", "5432")
dbname = os.environ.get("DB_NAME", "postgres")
user = os.environ.get("DB_USER", "postgres")
password = os.environ.get("DB_PASSWORD", "postgres")
# Try to connect to the database
start_time = time.time()
timeout = 30
while True:
try:
conn = psycopg2.connect(
host=host,
port=port,
dbname=dbname,
user=user,
password=password
)
conn.close()
print("Database is ready!")
break
except psycopg2.OperationalError as e:
if time.time() - start_time > timeout:
print(f"Could not connect to database after {timeout} seconds: {e}")
sys.exit(1)
print("Waiting for database to be ready...")
time.sleep(2)
END
# Ensure migrations directory exists with proper permissions
echo "Ensuring migrations directory exists..."
mkdir -p /code/api/migrations
chmod -R 777 /code/api/migrations
touch /code/api/migrations/__init__.py
# Run makemigrations first to ensure migration files are created
echo "Running makemigrations..."
python manage.py makemigrations --noinput
# Run migrations
echo "Running migrations..."
python manage.py migrate --noinput
# Create superuser if needed
if [ "$DJANGO_SUPERUSER_USERNAME" ] && [ "$DJANGO_SUPERUSER_PASSWORD" ] && [ "$DJANGO_SUPERUSER_EMAIL" ]; then
echo "Creating superuser..."
python manage.py createsuperuser --noinput
fi
# Collect static files
if [ "$COLLECT_STATIC" = "true" ]; then
echo "Collecting static files..."
python manage.py collectstatic --noinput
fi
# Start server
echo "Starting server..."
exec "$@"