From 36f63d8d9e61ad1d8e46f01ce9f109f7b42a2f60 Mon Sep 17 00:00:00 2001 From: Depaton Date: Fri, 20 Mar 2026 15:41:52 +0200 Subject: [PATCH 1/2] solution --- .dockerignore | 12 +++++++ Dockerfile | 26 ++++++++++++++ cinema/management/__init__.py | 0 cinema/management/commands/__init__.py | 0 cinema/management/commands/wait_for_db.py | 22 ++++++++++++ cinema_service/settings.py | 14 +++++--- cmd_out.txt | 4 +++ docker-compose.yml | 40 ++++++++++++++++++++++ docker_build_out.txt | Bin 0 -> 1754 bytes requirements.txt | 1 + 10 files changed, 115 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 cmd_out.txt create mode 100644 docker-compose.yml create mode 100644 docker_build_out.txt diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..372538aa --- /dev/null +++ b/.dockerignore @@ -0,0 +1,12 @@ +venv/ +venv +.venv +__pycache__ +__pycache__/ +*.pyc +.git +.git/ +.env +db.sqlite3 +.db.sqlite3 +.flake8 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..c4c1fd9a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +FROM python:3.10-alpine + +ENV PYTHONDONTWRITEBYTECODE=1 +ENV PYTHONUNBUFFERED=1 + +WORKDIR /app + +RUN apk add --update --no-cache postgresql-client jpeg-dev \ + && apk add --update --no-cache --virtual .tmp-build-deps \ + build-base postgresql-dev musl-dev zlib zlib-dev + +COPY requirements.txt /app/ + +RUN pip install --no-cache-dir -r requirements.txt \ + && apk del .tmp-build-deps + +COPY . /app/ + +RUN mkdir -p /vol/web/media \ + && mkdir -p /vol/web/static \ + && adduser -D django-user \ + && chown -R django-user:django-user /vol/ \ + && chown -R django-user:django-user /app/ \ + && chmod -R 755 /vol + +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..248406aa --- /dev/null +++ b/cinema/management/commands/wait_for_db.py @@ -0,0 +1,22 @@ +import time +from psycopg2 import OperationalError as Psycopg2OpError + +from django.db.utils import OperationalError +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + """Django command to pause execution until database is available""" + + def handle(self, *args, **options): + self.stdout.write('Waiting for database...') + db_up = False + while not db_up: + try: + self.check(databases=['default']) + db_up = True + except (OperationalError, Psycopg2OpError): + self.stdout.write('Database unavailable, waiting 1 second...') + time.sleep(1) + + self.stdout.write(self.style.SUCCESS('Database available!')) diff --git a/cinema_service/settings.py b/cinema_service/settings.py index 90dde772..138f92b7 100644 --- a/cinema_service/settings.py +++ b/cinema_service/settings.py @@ -10,6 +10,7 @@ https://docs.djangoproject.com/en/4.0/ref/settings/ """ from datetime import timedelta +import os from pathlib import Path # Build paths inside the project like this: BASE_DIR / 'subdir'. @@ -27,7 +28,7 @@ # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = ["127.0.0.1", "localhost", "0.0.0.0"] INTERNAL_IPS = [ "127.0.0.1", @@ -86,8 +87,12 @@ DATABASES = { "default": { - "ENGINE": "django.db.backends.sqlite3", - "NAME": BASE_DIR / "db.sqlite3", + "ENGINE": "django.db.backends.postgresql", + "NAME": os.environ.get("POSTGRES_DB", "postgres"), + "USER": os.environ.get("POSTGRES_USER", "postgres"), + "PASSWORD": os.environ.get("POSTGRES_PASSWORD", "postgres"), + "HOST": os.environ.get("POSTGRES_HOST", "db"), + "PORT": os.environ.get("POSTGRES_PORT", "5432"), } } @@ -132,9 +137,10 @@ # https://docs.djangoproject.com/en/4.0/howto/static-files/ STATIC_URL = "static/" +STATIC_ROOT = os.environ.get("STATIC_ROOT", BASE_DIR / "static") MEDIA_URL = "/media/" -MEDIA_ROOT = BASE_DIR / "media" +MEDIA_ROOT = os.environ.get("MEDIA_ROOT", BASE_DIR / "media") # Default primary key field type # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field diff --git a/cmd_out.txt b/cmd_out.txt new file mode 100644 index 00000000..126bd80a --- /dev/null +++ b/cmd_out.txt @@ -0,0 +1,4 @@ +time="2026-03-20T15:38:47+02:00" level=warning msg="D:\\pythonProject\\py-dockerize-cinema\\docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion" + Image py-dockerize-cinema-app Building +failed to connect to the docker API at npipe:////./pipe/dockerDesktopLinuxEngine; check if the path is correct and if the daemon is running: open //./pipe/dockerDesktopLinuxEngine: The system cannot find the file specified. + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..2beeb5c5 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,40 @@ +version: "3.9" + +services: + app: + build: + context: . + ports: + - "8000:8000" + volumes: + - .:/app + - media_data:/vol/web/media + - static_data:/vol/web/static + command: > + sh -c "python manage.py wait_for_db && + python manage.py migrate && + python manage.py runserver 0.0.0.0:8000" + environment: + - POSTGRES_DB=cinema + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + - POSTGRES_HOST=db + - POSTGRES_PORT=5432 + - MEDIA_ROOT=/vol/web/media + - STATIC_ROOT=/vol/web/static + depends_on: + - db + + db: + image: postgres:14-alpine + environment: + - POSTGRES_DB=cinema + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + volumes: + - pgdata:/var/lib/postgresql/data + +volumes: + pgdata: + media_data: + static_data: diff --git a/docker_build_out.txt b/docker_build_out.txt new file mode 100644 index 0000000000000000000000000000000000000000..f8e2e981e5b6382916e7e149e482e7e7c551e221 GIT binary patch literal 1754 zcma)+T~8BH5QgX4#Q(72!f3D`LNumE6NH+O5Te4J8YpdPSGwEH(t`EEpRPXdOt)+s ziR5I@o{xEF-uXIzeh+MFeY>)umDXT2wag0UYfcjD@XajcY-n$7%O2Y!+p#v^E2P`D z%dGvWy|9-^@7il--EO;7#}RGTTWFe~<%Vy9u3PJ2F-Lc9BknRRj#f4f(DId0*aG`w z_~iBx`Kf&e-{-8lZP>tV+@N6E(Idpr<)8cQjQJL+2_r#DdQyDOkuKSVACfJQ7-Bne zpt}qiMNSCmnXwl;*|=eyf);M-_HyQwTiH=YbGw9ja1|n7W^&JUO^{WbI^NBh|=OfJ2BP~z6WTDT$a8|06im&7plMm!e z6}zxctW;CsoBZiwE2$yWpCwUKEy~wpk7w$YyU$&=ht@0T!`rN2AG18*!xvgMl^!wCHJaTv9G!)Iz%0yccnVjs}p+H>p!tQUZO+fR4WM{ z6lhe|JJxbUi+_vEwN^K+HSPzdB*1CqU)K9etd&=b)$G)u~yv0PORc} zDxAaE@AUS_laf(+67dbsD|8?SAmdfZSLl+6=2.9 From 5ba4930f173cb8cffcac95d6b252709bedcdf304 Mon Sep 17 00:00:00 2001 From: Depaton Date: Fri, 20 Mar 2026 15:45:43 +0200 Subject: [PATCH 2/2] fix flake8 styling issues --- cinema/management/commands/wait_for_db.py | 8 ++++---- flake8_report.txt | 4 ++++ 2 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 flake8_report.txt diff --git a/cinema/management/commands/wait_for_db.py b/cinema/management/commands/wait_for_db.py index 248406aa..c63a897e 100644 --- a/cinema/management/commands/wait_for_db.py +++ b/cinema/management/commands/wait_for_db.py @@ -9,14 +9,14 @@ class Command(BaseCommand): """Django command to pause execution until database is available""" def handle(self, *args, **options): - self.stdout.write('Waiting for database...') + self.stdout.write("Waiting for database...") db_up = False while not db_up: try: - self.check(databases=['default']) + self.check(databases=["default"]) db_up = True except (OperationalError, Psycopg2OpError): - self.stdout.write('Database unavailable, waiting 1 second...') + self.stdout.write("Database unavailable, waiting 1 second...") time.sleep(1) - self.stdout.write(self.style.SUCCESS('Database available!')) + self.stdout.write(self.style.SUCCESS("Database available!")) diff --git a/flake8_report.txt b/flake8_report.txt new file mode 100644 index 00000000..2f747b68 --- /dev/null +++ b/flake8_report.txt @@ -0,0 +1,4 @@ +.\cinema\management\commands\wait_for_db.py:12:27: Q000 Single quotes found but double quotes preferred +.\cinema\management\commands\wait_for_db.py:16:39: Q000 Single quotes found but double quotes preferred +.\cinema\management\commands\wait_for_db.py:19:35: Q000 Single quotes found but double quotes preferred +.\cinema\management\commands\wait_for_db.py:22:46: Q000 Single quotes found but double quotes preferred