diff --git a/Dockerfile.backend b/Dockerfile.backend index 55487ec..af73c33 100644 --- a/Dockerfile.backend +++ b/Dockerfile.backend @@ -6,17 +6,26 @@ ENV PYTHONUNBUFFERED=1 \ WORKDIR /app -# Install system dependencies +# Copy requirements file first +COPY requirements.txt . + +# Install build dependencies for psycopg2, install pip requirements, then clean up build tools RUN apt-get update && apt-get install -y --no-install-recommends \ - build-essential \ + gcc \ + python3-dev \ libpq-dev \ + libpq5 \ + && pip install --no-cache-dir -r requirements.txt \ + && apt-get purge -y --auto-remove gcc python3-dev libpq-dev \ && rm -rf /var/lib/apt/lists/* -# Copy requirements and install -COPY requirements.txt . -RUN pip install --no-cache-dir -r requirements.txt +# Copy entrypoint script and set executable permissions +COPY entrypoint.sh /app/entrypoint.sh +RUN chmod +x /app/entrypoint.sh -# Copy source code +# Copy backend source code COPY backend /app/backend EXPOSE 8000 + +ENTRYPOINT ["/app/entrypoint.sh"] diff --git a/docker-compose.yml b/docker-compose.yml index 74ad8cc..a0d7615 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -33,7 +33,7 @@ services: context: . dockerfile: Dockerfile.backend container_name: divar_backend - command: sh -c "python backend/manage.py migrate && python backend/manage.py runserver 0.0.0.0:8000" + command: python backend/manage.py runserver 0.0.0.0:8000 ports: - "8000:8000" environment: @@ -54,7 +54,7 @@ services: context: . dockerfile: Dockerfile.backend container_name: divar_celery - command: sh -c "celery -A config worker --loglevel=info -P gevent -c 20" + command: celery -A config worker --loglevel=info -P gevent -c 20 environment: - REDIS_URL=redis://redis:6379/0 - POSTGRES_HOST=db diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..27bf202 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,34 @@ +#!/bin/sh +set -e + +# Wait for PostgreSQL database connection if POSTGRES_HOST is specified +if [ -n "$POSTGRES_HOST" ]; then + echo "Waiting for PostgreSQL database ($POSTGRES_HOST)..." + while ! python -c " +import os, sys, psycopg2 +try: + conn = psycopg2.connect( + dbname=os.getenv('POSTGRES_DB', 'divar_crawler'), + user=os.getenv('POSTGRES_USER', 'postgres'), + password=os.getenv('POSTGRES_PASSWORD', 'postgres'), + host=os.getenv('POSTGRES_HOST', 'db'), + port=os.getenv('POSTGRES_PORT', '5432'), + connect_timeout=2 + ) + conn.close() + sys.exit(0) +except Exception: + sys.exit(1) +" 2>/dev/null; do + sleep 1 + done + echo "PostgreSQL database is ready!" +fi + +# Automatically run Django migrations before starting backend server +if [ "$1" = "python" ] && [ "$2" = "backend/manage.py" ] && [ "$3" = "runserver" ]; then + echo "Running Django migrations..." + python backend/manage.py migrate --noinput +fi + +exec "$@" diff --git a/requirements.txt b/requirements.txt index 193325c..33a8f19 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,8 @@ djangorestframework>=3.18.0,<3.19 django-cors-headers>=4.9.0,<4.10 celery>=5.6.3,<5.7 redis>=8.1.0,<8.2 -psycopg2-binary>=2.9.12,<2.10 +psycopg2>=2.9.9,<2.10 + django-celery-beat>=2.9.0,<2.10 requests>=2.34.2,<2.35 openai>=2.53.0,<2.54