From dcebcc63e96071a53fe2b68f80b9deee6f7ad1d3 Mon Sep 17 00:00:00 2001 From: Seroga Date: Tue, 12 May 2026 12:44:56 +0300 Subject: [PATCH 1/2] solution --- .dockerignore | 7 +++++ Dockerfile | 26 +++++++++++++++++ cinema/management/__init__.py | 0 cinema/management/commands/__init__.py | 0 cinema/management/commands/wait_for_db.py | 18 ++++++++++++ cinema_service/settings.py | 14 +++++++--- docker-compose.yaml | 34 +++++++++++++++++++++++ requirements.txt | 1 + 8 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 cinema/management/__init__.py create mode 100644 cinema/management/commands/__init__.py create mode 100644 cinema/management/commands/wait_for_db.py create mode 100644 docker-compose.yaml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..d74f5af3 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,7 @@ +.venv +__pycache__ +*.pyc +.git +.idea +db.sqlite3 +docker-compose.yaml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..5bfbb140 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +FROM python:3.13-slim +LABEL maintainer="ilchuk.cergey@gmail.com" + +ENV PYTHONUNBUFFERED=1 + +WORKDIR /app + +COPY requirements.txt requirements.txt +RUN pip install -r requirements.txt + +COPY . . + +RUN mkdir -p /upload/media +RUN mkdir -p /upload/static + +RUN adduser \ + --disabled-password \ + --no-create-home \ + django_user + +RUN chown -R django_user /upload/media +RUN chmod -R 755 /upload/media +RUN chown -R django_user /upload/static +RUN chmod -R 755 /upload/static + +USER django_user diff --git a/cinema/management/__init__.py b/cinema/management/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/cinema/management/commands/__init__.py b/cinema/management/commands/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/cinema/management/commands/wait_for_db.py b/cinema/management/commands/wait_for_db.py new file mode 100644 index 00000000..1db0f3a5 --- /dev/null +++ b/cinema/management/commands/wait_for_db.py @@ -0,0 +1,18 @@ +import time + +from django.core.management import BaseCommand +from django.db import connections + +from django.db.utils import OperationalError + + +class Command(BaseCommand): + def handle(self, *args, **options): + db_conn = None + + while not db_conn: + try: + db_conn = connections['default'] + db_conn.cursor() + except OperationalError: + time.sleep(1) diff --git a/cinema_service/settings.py b/cinema_service/settings.py index 90dde772..d69f15e5 100644 --- a/cinema_service/settings.py +++ b/cinema_service/settings.py @@ -9,6 +9,7 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/4.0/ref/settings/ """ +import os from datetime import timedelta from pathlib import Path @@ -86,8 +87,12 @@ DATABASES = { "default": { - "ENGINE": "django.db.backends.sqlite3", - "NAME": BASE_DIR / "db.sqlite3", + "ENGINE": "django.db.backends.postgresql", + "NAME": os.environ["POSTGRES_DB"], + "USER": os.environ["POSTGRES_USER"], + "PASSWORD": os.environ["POSTGRES_PASSWORD"], + "HOST": os.environ["POSTGRES_HOST"], + "PORT": os.environ["POSTGRES_PORT"], } } @@ -131,10 +136,11 @@ # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/4.0/howto/static-files/ -STATIC_URL = "static/" +STATIC_URL = "/static/" +STATIC_ROOT = "/upload/static" MEDIA_URL = "/media/" -MEDIA_ROOT = BASE_DIR / "media" +MEDIA_ROOT = "/upload/media" # Default primary key field type # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 00000000..8f2d748a --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,34 @@ +services: + cinema: + build: + context: . + env_file: + - .env + ports: + - "8000:8000" + volumes: + - ./:/app + - cinema_media:/upload/media + - cinema_static:/upload/static + command: > + sh -c "python manage.py wait_for_db && + python manage.py migrate && + python manage.py runserver 0.0.0.0:8000" + depends_on: + - db + + + db: + image: postgres:18-alpine + restart: always + env_file: + - .env + ports: + - "5433:5432" + volumes: + - cinema_db:$PGDATA + +volumes: + cinema_db: + cinema_media: + cinema_static: diff --git a/requirements.txt b/requirements.txt index 2dc12c67..622155ff 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,3 +8,4 @@ djangorestframework djangorestframework-simplejwt drf-spectacular Pillow +psycopg2-binary From d2f5af4e455dddbce84b06773a13df0efcbcafb7 Mon Sep 17 00:00:00 2001 From: Seroga Date: Tue, 12 May 2026 13:44:36 +0300 Subject: [PATCH 2/2] solution2 --- cinema/management/commands/wait_for_db.py | 3 ++- docker-compose.yaml | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/cinema/management/commands/wait_for_db.py b/cinema/management/commands/wait_for_db.py index 1db0f3a5..e8f83e6e 100644 --- a/cinema/management/commands/wait_for_db.py +++ b/cinema/management/commands/wait_for_db.py @@ -12,7 +12,8 @@ def handle(self, *args, **options): while not db_conn: try: - db_conn = connections['default'] + db_conn = connections["default"] db_conn.cursor() + break except OperationalError: time.sleep(1) diff --git a/docker-compose.yaml b/docker-compose.yaml index 8f2d748a..ecac9e5b 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,5 +1,5 @@ services: - cinema: + app: build: context: . env_file: @@ -13,6 +13,7 @@ services: command: > sh -c "python manage.py wait_for_db && python manage.py migrate && + python manage.py collectstatic --noinput && python manage.py runserver 0.0.0.0:8000" depends_on: - db