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
12 changes: 12 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
venv/
venv
.venv
__pycache__
__pycache__/
*.pyc
.git
.git/
.env
db.sqlite3
.db.sqlite3
.flake8
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Configuration files like .flake8 should typically be included in the Docker image. This ensures that anyone using the image has the correct configuration for tools like linters, even if they don't mount the local source code. While the current setup works for docker-compose run because of the volume mount, it's a good practice to make the image as self-contained as possible.

26 changes: 26 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM python:3.10-alpine

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

WORKDIR /app

RUN apk add --update --no-cache postgresql-client jpeg-dev \
&& apk add --update --no-cache --virtual .tmp-build-deps \
build-base postgresql-dev musl-dev zlib zlib-dev

COPY requirements.txt /app/

RUN pip install --no-cache-dir -r requirements.txt \
&& apk del .tmp-build-deps

COPY . /app/

RUN mkdir -p /vol/web/media \
&& mkdir -p /vol/web/static \
&& adduser -D django-user \
&& chown -R django-user:django-user /vol/ \
&& chown -R django-user:django-user /app/ \
&& chmod -R 755 /vol

USER django-user
Empty file added cinema/management/__init__.py
Empty file.
Empty file.
22 changes: 22 additions & 0 deletions cinema/management/commands/wait_for_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import time
from psycopg2 import OperationalError as Psycopg2OpError

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


class Command(BaseCommand):
"""Django command to pause execution until database is available"""

def handle(self, *args, **options):
self.stdout.write('Waiting for database...')
db_up = False
while not db_up:
try:
self.check(databases=['default'])
db_up = True
except (OperationalError, Psycopg2OpError):
self.stdout.write('Database unavailable, waiting 1 second...')
time.sleep(1)

self.stdout.write(self.style.SUCCESS('Database available!'))
14 changes: 10 additions & 4 deletions cinema_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
https://docs.djangoproject.com/en/4.0/ref/settings/
"""
from datetime import timedelta
import os
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
Expand All @@ -27,7 +28,7 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []
ALLOWED_HOSTS = ["127.0.0.1", "localhost", "0.0.0.0"]

INTERNAL_IPS = [
"127.0.0.1",
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.get("POSTGRES_DB", "postgres"),
"USER": os.environ.get("POSTGRES_USER", "postgres"),
"PASSWORD": os.environ.get("POSTGRES_PASSWORD", "postgres"),
"HOST": os.environ.get("POSTGRES_HOST", "db"),
"PORT": os.environ.get("POSTGRES_PORT", "5432"),
}
}

Expand Down Expand Up @@ -132,9 +137,10 @@
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = "static/"
STATIC_ROOT = os.environ.get("STATIC_ROOT", BASE_DIR / "static")

MEDIA_URL = "/media/"
MEDIA_ROOT = BASE_DIR / "media"
MEDIA_ROOT = os.environ.get("MEDIA_ROOT", BASE_DIR / "media")

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
Expand Down
4 changes: 4 additions & 0 deletions cmd_out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
time="2026-03-20T15:38:47+02:00" level=warning msg="D:\\pythonProject\\py-dockerize-cinema\\docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion"
Image py-dockerize-cinema-app Building
failed to connect to the docker API at npipe:////./pipe/dockerDesktopLinuxEngine; check if the path is correct and if the daemon is running: open //./pipe/dockerDesktopLinuxEngine: The system cannot find the file specified.

40 changes: 40 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
version: "3.9"

services:
app:
build:
context: .
ports:
- "8000:8000"
volumes:
- .:/app
- media_data:/vol/web/media
- static_data:/vol/web/static
command: >
sh -c "python manage.py wait_for_db &&
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
environment:
- POSTGRES_DB=cinema
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_HOST=db
- POSTGRES_PORT=5432
- MEDIA_ROOT=/vol/web/media
- STATIC_ROOT=/vol/web/static
depends_on:
- db

db:
image: postgres:14-alpine
environment:
- POSTGRES_DB=cinema
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
volumes:
- pgdata:/var/lib/postgresql/data

volumes:
pgdata:
media_data:
static_data:
Binary file added docker_build_out.txt
Binary file not shown.
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
Loading