#!/bin/bash set -e # Wait for database to be ready echo "Waiting for PostgreSQL..." while ! nc -z $POSTGRES_HOST ${POSTGRES_PORT:-5432}; do sleep 1 done echo "PostgreSQL is ready!" # Run migrations echo "Running migrations..." python manage.py migrate --noinput # Collect static files (skip in DEBUG mode - Django serves them directly) if [ "$DEBUG" != "True" ]; then echo "Collecting static files..." python manage.py collectstatic --noinput else echo "DEBUG mode - skipping collectstatic" fi # Create superuser if not exists if [ -n "$DJANGO_SUPERUSER_USERNAME" ] && [ -n "$DJANGO_SUPERUSER_PASSWORD" ] && [ -n "$DJANGO_SUPERUSER_EMAIL" ]; then echo "Creating superuser..." python manage.py shell << EOF from django.contrib.auth import get_user_model User = get_user_model() if not User.objects.filter(username='$DJANGO_SUPERUSER_USERNAME').exists(): User.objects.create_superuser('$DJANGO_SUPERUSER_USERNAME', '$DJANGO_SUPERUSER_EMAIL', '$DJANGO_SUPERUSER_PASSWORD') print('Superuser created successfully') else: print('Superuser already exists') EOF fi # Import initial data if flag is set if [ "$IMPORT_INITIAL_DATA" = "true" ]; then echo "Importing initial data..." python manage.py import_data --data-dir=/app --output-dir=/app/output || true fi # Start the server echo "Starting server..." exec "$@"