Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.idea/
.vscode/
*.iml
.env
.DS_Store
.venv/
.pytest_cache/
**__pycache__/
README.md
checklist.md
.gitignore
**db.sqlite3
media
5 changes: 5 additions & 0 deletions .env_example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
POSTGRES_DB=
POSTGRES_USER=
POSTGRES_PASSWORD=
DB_HOST=
DB_PORT=
Comment thread
MarcinDJacz marked this conversation as resolved.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*.iml
.env
.DS_Store
venv/
.venv/
.pytest_cache/
**__pycache__/
**db.sqlite3
Expand Down
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM python:3.12-slim

WORKDIR /app

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

COPY . .

RUN adduser \
--disabled-password \
--no-create-home \
django-user
Comment thread
MarcinDJacz marked this conversation as resolved.

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

RUN chown -R django-user:django-user /vol/
14 changes: 10 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,12 @@

DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
"ENGINE": "django.db.backends.postgresql",
"NAME": os.getenv("POSTGRES_DB"),
"USER": os.getenv("POSTGRES_USER"),
"PASSWORD": os.getenv("POSTGRES_PASSWORD"),
"HOST": "db",
"PORT": "5432",
Comment thread
MarcinDJacz marked this conversation as resolved.
Outdated
}
}

Expand Down Expand Up @@ -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 = "/vol/web/static"

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
33 changes: 33 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
version: "1"
Comment thread
MarcinDJacz marked this conversation as resolved.
Outdated

services:
app:
build:
context: .
ports:
- "8000:8000"
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"
env_file:
- .env
depends_on:
- db
volumes:
- .:/app

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

volumes:
postgres_data:
media_data:
static_data:
Comment thread
MarcinDJacz marked this conversation as resolved.
Outdated
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
Empty file added user/management/__init__.py
Empty file.
Empty file.
21 changes: 21 additions & 0 deletions user/management/commands/wait_for_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import time
from django.db import connections
from django.db.utils import OperationalError
from django.core.management.base import BaseCommand


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

db_conn = None

while not db_conn:
try:
db_conn = connections["default"]
db_conn.cursor()
except OperationalError:
self.stdout.write("Database unavailable, waiting 1 sec...")
time.sleep(1)

self.stdout.write(self.style.SUCCESS("Database available!"))
Loading