diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..5afdff7a --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +.git +.gitignore +.env +__pycache__ +*.pyc +.venv +media/ +static/ \ No newline at end of file diff --git a/.env.example b/.env.example new file mode 100644 index 00000000..41b7ee3d --- /dev/null +++ b/.env.example @@ -0,0 +1,9 @@ +POSTGRES_USER=your_user +POSTGRES_PASSWORD=your_password +POSTGRES_DB=postgres_db +DB_HOST=db +DB_PORT=5432 + +SECRET_KEY=change_me +DEBUG=1 +ALLOWED_HOSTS=127.0.0.1,localhost \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..af6214d7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,23 @@ +FROM python:3.13-slim + +LABEL maintainer="Marek DrzÄ…dzewski" + +ENV PYTHONUNBUFFERED=1 + +WORKDIR /app + +COPY requirements.txt . +RUN pip install --upgrade pip && pip install -r requirements.txt + +COPY . . + +RUN adduser \ + --disabled-password \ + --no-create-home \ + django-user + +RUN mkdir -p /vol/web \ + && chown -R django-user:django-user /vol \ + && chmod -R 755 /vol/web + +USER django-user \ No newline at end of file diff --git a/cinema_service/settings.py b/cinema_service/settings.py index 90dde772..54ab356f 100644 --- a/cinema_service/settings.py +++ b/cinema_service/settings.py @@ -11,6 +11,7 @@ """ from datetime import timedelta from pathlib import Path +import os # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -47,6 +48,7 @@ "debug_toolbar", "cinema", "user", + "starter", ] MIDDLEWARE = [ @@ -86,8 +88,12 @@ DATABASES = { "default": { - "ENGINE": "django.db.backends.sqlite3", - "NAME": BASE_DIR / "db.sqlite3", + "ENGINE": "django.db.backends.postgresql", + "NAME": os.environ.get("POSTGRES_DB"), + "USER": os.environ.get("POSTGRES_USER"), + "PASSWORD": os.environ.get("POSTGRES_PASSWORD"), + "HOST": os.environ.get("DB_HOST"), + "PORT": os.environ.get("DB_PORT"), } } @@ -125,7 +131,7 @@ USE_I18N = True -USE_TZ = False +USE_TZ = True # Static files (CSS, JavaScript, Images) diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..009911c3 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,27 @@ +services: + web: + build: . + container_name: cinema_service + command: > + sh -c "python manage.py wait_for_db && + python manage.py migrate && + python manage.py runserver 0.0.0.0:8000" + ports: + - "8000:8000" + volumes: + - ./:/app + env_file: + - .env + db: + image: postgres:15-alpine + container_name: postgres_container + restart: always + env_file: + - .env + ports: + - "5432:5432" + volumes: + - pgdata:/var/lib/postgresql/data + +volumes: + pgdata: diff --git a/requirements.txt b/requirements.txt index 2dc12c67..61920591 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,10 +1,28 @@ -django -flake8 -flake8-quotes -flake8-variables-names -pep8-naming -django-debug-toolbar -djangorestframework -djangorestframework-simplejwt -drf-spectacular -Pillow +asgiref==3.11.1 +attrs==26.1.0 +Django==5.2.13 +django-debug-toolbar==6.3.0 +djangorestframework==3.17.1 +djangorestframework_simplejwt==5.5.1 +drf-spectacular==0.29.0 +flake8==7.3.0 +flake8-quotes==3.4.0 +flake8-variables-names==0.0.6 +inflection==0.5.1 +jsonschema==4.26.0 +jsonschema-specifications==2025.9.1 +mccabe==0.7.0 +pep8-naming==0.15.1 +pillow==12.2.0 +psycopg2-binary==2.9.11 +pycodestyle==2.14.0 +pyflakes==3.4.0 +PyJWT==2.12.1 +PyYAML==6.0.3 +referencing==0.37.0 +rpds-py==0.30.0 +setuptools==82.0.1 +sqlparse==0.5.5 +typing_extensions==4.15.0 +tzdata==2026.1 +uritemplate==4.2.0 \ No newline at end of file diff --git a/starter/__init__.py b/starter/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/starter/management/__init__.py b/starter/management/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/starter/management/commands/__init__.py b/starter/management/commands/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/starter/management/commands/wait_for_db.py b/starter/management/commands/wait_for_db.py new file mode 100644 index 00000000..748396c2 --- /dev/null +++ b/starter/management/commands/wait_for_db.py @@ -0,0 +1,35 @@ +import time +from django.core.management.base import BaseCommand +from django.db import connections +from django.db.utils import OperationalError + + +class Command(BaseCommand): + + def handle(self, *args, **kwargs): + self.stdout.write("Waiting for database...") + try_counter = 0 + max_retries = 40 + + while True: + try: + db_conn = connections["default"] + db_conn.cursor() + + self.stdout.write(self.style.SUCCESS("Database connected!")) + break + + except OperationalError: + self.stdout.write( + "Database unavailable, waiting 2 seconds..." + ) + time.sleep(2) + try_counter += 1 + + if try_counter >= max_retries: + self.stdout.write( + self.style.ERROR("Cannot connect to db!" + " Check your connection or" + " try again later.!") + ) + raise Exception("Database not available")