Skip to content
Open

sol #1017

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
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
__pycache__/
*.pyc
.env
venv/
.git/
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM python:3.12-slim

ENV PYTHONUNBUFFERED=1
ENV PYTHONPATH=/app

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

RUN adduser \
--disabled-password \
--no-create-home \
django-user

RUN mkdir -p /vol/web/media /vol/web/static

RUN chown -R django-user:django-user /vol/
RUN chmod -R 755 /vol/web/

COPY --chown=django-user:django-user . .

USER django-user

CMD manage.py runserver 0.0.0.0:8000
Comment on lines +1 to +25
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 entirely. This file is required to: define both app and db services, configure the wait_for_db command in the startup sequence, manage volumes for static/media files, and link the services together.

19 changes: 19 additions & 0 deletions cinema/management/commands/wait_for_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.db.utils import OperationalError
from django.core.management.base import BaseCommand
import time


class Command(BaseCommand):
def handle(self, *args, **options):
self.stdout.write("Waiting for database...")
db_up = False

while db_up is False:
try:
self.check(databases=["default"])
db_up = True
except OperationalError:
self.stdout.write("Database unavailable, waiting 1 second...")
time.sleep(1)

self.stdout.write(self.style.SUCCESS("Database available!"))
17 changes: 13 additions & 4 deletions cinema_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -86,8 +87,14 @@

DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
"ENGINE": "django.db.backends.postgresql",
"NAME": os.environ.get("POSTGRES_DB", "cinema_db"),
"USER": os.environ.get("POSTGRES_USER", "cinema_user"),
"PASSWORD": os.environ.get(
"POSTGRES_PASSWORD", "supersecretpassword123"
),
"HOST": os.environ.get("POSTGRES_HOST", "db"),
"PORT": os.environ.get("POSTGRES_PORT", "5432"),
}
}

Expand Down Expand Up @@ -131,10 +138,12 @@
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = "static/"
STATIC_URL = "/static/"
STATIC_ROOT = "/vol/web/static"

# Media files (Uploaded by users)
MEDIA_URL = "/media/"
MEDIA_ROOT = BASE_DIR / "media"
MEDIA_ROOT = "/vol/web/media"

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
Expand Down
31 changes: 31 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
services:
app:
build:
context: .
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"
env_file:
- .env
depends_on:
- db
volumes:
- static_data:/vol/web/static
- media_data:/vol/web/media

db:
image: postgres:14-alpine
ports:
- "5433:5432"
env_file:
- .env
volumes:
- my_db:/var/lib/postgresql/data

volumes:
my_db:
static_data:
media_data:
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>=2.9.0
Loading