Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.idea/
.vscode/
*.iml
.env
.DS_Store
.gitignore
.git
venv/
.pytest_cache/
**__pycache__/
Dockerfile
docker-compose.yml
22 changes: 22 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM python:3.12.13-slim
LABEL maintainer="illaredka8@gmail.com"

ENV PYTHONUNBUFFERED=1

WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

COPY . .
RUN mkdir -p /files/media

RUN adduser \
--disabled-password \
--no-create-home \
my_user

RUN chown -R my_user /files/media
RUN chmod -R 755 /files/media

USER my_user
Empty file added cinema/management/__init__.py
Empty file.
Empty file.
23 changes: 23 additions & 0 deletions cinema/management/commands/wait_for_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import time
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docker-compose.yml file is MISSING - this is a required file per task requirements. It must define 2 services: app (Django application) and db (PostgreSQL), with proper volumes for my_media and my_db, environment configuration from .env, and depends_on to ensure app waits for db.


from django.core.management.base import BaseCommand
from django.db import connections
from django.db.utils import OperationalError


class Command(BaseCommand):
help = "Wait for database to be available" # noqa: VNE003

def handle(self, *args, **options):
self.stdout.write("Waiting for database...")

db_ready = False
while not db_ready:
try:
connections["default"].ensure_connection()
db_ready = True
except OperationalError:
self.stdout.write("Database unavailable, waiting 1 second...")
time.sleep(1)

self.stdout.write(self.style.SUCCESS("Database available!"))
11 changes: 8 additions & 3 deletions cinema_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"],
}
}

Expand Down Expand Up @@ -134,7 +139,7 @@
STATIC_URL = "static/"

MEDIA_URL = "/media/"
MEDIA_ROOT = BASE_DIR / "media"
MEDIA_ROOT = "/files/media"

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
Expand Down
33 changes: 33 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
services:
app:
build:
context: .
env_file:
- .env
ports:
- "8000:8000"
command: >
sh -c "python manage.py wait_for_db &&
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
volumes:
- ./:/app
- my_media:/files/media
depends_on:
- db


db:
image: postgres:16.0-alpine3.17
restart: always
env_file:
- .env
ports:
- "5432:5432"
volumes:
- my_db:$PGDATA


volumes:
my_db:
my_media:
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ djangorestframework
djangorestframework-simplejwt
drf-spectacular
Pillow
psycopg2-binary
Loading