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..e8f83e6e --- /dev/null +++ b/cinema/management/commands/wait_for_db.py @@ -0,0 +1,19 @@ +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() + break + 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..ecac9e5b --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,35 @@ +services: + app: + 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 collectstatic --noinput && + 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