diff --git a/Dockerfile b/Dockerfile index 74fbcdb..1f9b2d7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,24 @@ FROM python:3.12-slim + WORKDIR /app + +# Окружение +ENV PYTHONDONTWRITEBYTECODE=1 +ENV PYTHONUNBUFFERED=1 + +# Ставим системные либы для Postgres +RUN apt-get update && apt-get install -y --no-install-recommends \ + gcc libpq-dev && rm -rf /var/lib/apt/lists/* + COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt + COPY . . -CMD ["gunicorn", "--bind", "0.0.0.0:8000", "core.wsgi:application"] \ No newline at end of file + +# Обязательно даем права скрипту +RUN chmod +x /app/entrypoint.sh + +# Используем путь из рабочей папки +ENTRYPOINT ["/app/entrypoint.sh"] + +CMD ["gunicorn", "core.wsgi:application", "--bind", "0.0.0.0:8000", "--workers", "3"] \ No newline at end of file diff --git a/core/settings.py b/core/settings.py index 26cdab9..287dbac 100644 --- a/core/settings.py +++ b/core/settings.py @@ -179,4 +179,7 @@ SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') CSRF_TRUSTED_ORIGINS = env.list('CSRF_ORIGINS', default=['http://localhost']) -print(f"--- РАБОТАЕМ НА БАЗЕ: {DATABASES['default']['NAME']} (HOST: {DATABASES['default'].get('HOST', 'localhost')}) ---") \ No newline at end of file +print(f"--- РАБОТАЕМ НА БАЗЕ: {DATABASES['default']['NAME']} (HOST: {DATABASES['default'].get('HOST', 'localhost')}) ---") + +print (env) + diff --git a/core/urls.py b/core/urls.py index 3667112..9789db1 100644 --- a/core/urls.py +++ b/core/urls.py @@ -16,7 +16,14 @@ Including another URLconf """ from django.contrib import admin from django.urls import path +from django.conf.urls.static import static # <--- Добавьте эту строку +from core import settings urlpatterns = [ path('admin/', admin.site.urls), ] + +# Вместо if settings.DEBUG: не забываем from django.conf.urls.static import static # <--- Добавьте эту строку +if settings.ENV_TYPE in ['local', 'dev']: + urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 2ac8e52..b196e2e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -27,6 +27,7 @@ services: image: nginx:1.25-alpine restart: unless-stopped volumes: + # Подключаем конфиг и статику с медиа в режиме только для чтения (т.к. nginx смотрит в интернет и может быть подвергнута атаке) - ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro - staticfiles:/app/staticfiles:ro - mediafiles:/app/media:ro diff --git a/entrypoint.sh b/entrypoint.sh index 96bc549..c9c5a38 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,8 +1,15 @@ #!/bin/sh + +# Собираем статику в папку, которую увидит Nginx echo "Collecting static files..." python manage.py collectstatic --noinput + +# Миграции echo "Applying database migrations..." python manage.py migrate --noinput + +# Создаем админа (из переменных .env) python manage.py createsuperuser --no-input || true + echo "Starting Gunicorn..." exec "$@" \ No newline at end of file