demo version on how to dockerise a django project
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Set up dockerised django project

Hilke Ros ab886ce9

+340
+5
.env
··· 1 + POSTGRES_DB=dev_database 2 + POSTGRES_USER=dev_user 3 + POSTGRES_PASSWORD=test 4 + DB_HOST=db 5 + DB_PORT=5432
+20
Dockerfile
··· 1 + FROM python:3.13-slim-bookworm 2 + 3 + ENV PYTHONDONTWRITEBYTECODE=1 \ 4 + PYTHONUNBUFFERED=1 5 + 6 + WORKDIR /app 7 + 8 + RUN apt-get update && apt-get install -y curl 9 + 10 + COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/ 11 + 12 + COPY src/requirements.txt . 13 + RUN uv pip install -r requirements.txt --system 14 + 15 + COPY src/ . 16 + 17 + EXPOSE 8000 18 + 19 + # CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"] 20 + CMD ["./entrypoint.sh"]
+33
docker-compose.yml
··· 1 + services: 2 + web: 3 + build: . 4 + container_name: django_app 5 + ports: 6 + - 8000:8000 7 + volumes: 8 + - ./src:/app 9 + depends_on: 10 + db: 11 + condition: service_healthy 12 + restart: true 13 + env_file: 14 + - .env 15 + db: 16 + image: postgres:17 17 + container_name: postgres_db 18 + volumes: 19 + - postgres_db:/var/lib/postgresql/data/ 20 + environment: 21 + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} 22 + POSTGRES_USER: ${POSTGRES_USER} 23 + POSTGRES_DB: ${POSTGRES_DB} 24 + healthcheck: 25 + test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"] 26 + interval: 10s 27 + retries: 5 28 + start_period: 30s 29 + timeout: 10s 30 + 31 + 32 + volumes: 33 + postgres_db:
src/core/__init__.py

This is a binary file and will not be displayed.

+3
src/core/admin.py
··· 1 + from django.contrib import admin 2 + 3 + # Register your models here.
+6
src/core/apps.py
··· 1 + from django.apps import AppConfig 2 + 3 + 4 + class CoreConfig(AppConfig): 5 + default_auto_field = "django.db.models.BigAutoField" 6 + name = "core"
+23
src/core/migrations/0001_initial.py
··· 1 + # Generated by Django 5.2.8 on 2025-12-02 09:45 2 + 3 + from django.db import migrations, models 4 + 5 + 6 + class Migration(migrations.Migration): 7 + 8 + initial = True 9 + 10 + dependencies = [ 11 + ] 12 + 13 + operations = [ 14 + migrations.CreateModel( 15 + name='Actor', 16 + fields=[ 17 + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 18 + ('name', models.CharField(max_length=128)), 19 + ('nationality', models.CharField(max_length=128)), 20 + ('created', models.DateTimeField(auto_now_add=True)), 21 + ], 22 + ), 23 + ]
+18
src/core/migrations/0002_actor_released.py
··· 1 + # Generated by Django 5.2.8 on 2025-12-02 10:11 2 + 3 + from django.db import migrations, models 4 + 5 + 6 + class Migration(migrations.Migration): 7 + 8 + dependencies = [ 9 + ('core', '0001_initial'), 10 + ] 11 + 12 + operations = [ 13 + migrations.AddField( 14 + model_name='actor', 15 + name='released', 16 + field=models.DateField(blank=True, null=True), 17 + ), 18 + ]
src/core/migrations/__init__.py

This is a binary file and will not be displayed.

+11
src/core/models.py
··· 1 + from django.db import models 2 + 3 + 4 + class Actor(models.Model): 5 + name = models.CharField(max_length=128) 6 + nationality = models.CharField(max_length=128) 7 + created = models.DateTimeField(auto_now_add=True) 8 + released = models.DateField(null=True, blank=True) 9 + 10 + def __str__(self): 11 + return self.name
+3
src/core/tests.py
··· 1 + from django.test import TestCase 2 + 3 + # Create your tests here.
+3
src/core/views.py
··· 1 + from django.shortcuts import render 2 + 3 + # Create your views here.
src/db.sqlite3

This is a binary file and will not be displayed.

src/django_docker_demo/__init__.py

This is a binary file and will not be displayed.

src/django_docker_demo/__pycache__/__init__.cpython-310.pyc

This is a binary file and will not be displayed.

src/django_docker_demo/__pycache__/settings.cpython-310.pyc

This is a binary file and will not be displayed.

+16
src/django_docker_demo/asgi.py
··· 1 + """ 2 + ASGI config for django_docker_demo project. 3 + 4 + It exposes the ASGI callable as a module-level variable named ``application``. 5 + 6 + For more information on this file, see 7 + https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/ 8 + """ 9 + 10 + import os 11 + 12 + from django.core.asgi import get_asgi_application 13 + 14 + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_docker_demo.settings") 15 + 16 + application = get_asgi_application()
+129
src/django_docker_demo/settings.py
··· 1 + """ 2 + Django settings for django_docker_demo project. 3 + 4 + Generated by 'django-admin startproject' using Django 5.0.3. 5 + 6 + For more information on this file, see 7 + https://docs.djangoproject.com/en/5.0/topics/settings/ 8 + 9 + For the full list of settings and their values, see 10 + https://docs.djangoproject.com/en/5.0/ref/settings/ 11 + """ 12 + 13 + from pathlib import Path 14 + 15 + # Build paths inside the project like this: BASE_DIR / 'subdir'. 16 + BASE_DIR = Path(__file__).resolve().parent.parent 17 + 18 + 19 + # Quick-start development settings - unsuitable for production 20 + # See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/ 21 + 22 + # SECURITY WARNING: keep the secret key used in production secret! 23 + SECRET_KEY = "django-insecure-@do!$e7x&d_wb!z$_egm2kfi#0j+ndi=!=lh#4=&00kk9n%qvz" 24 + 25 + # SECURITY WARNING: don't run with debug turned on in production! 26 + DEBUG = True 27 + 28 + ALLOWED_HOSTS = [] 29 + 30 + 31 + # Application definition 32 + 33 + INSTALLED_APPS = [ 34 + "django.contrib.admin", 35 + "django.contrib.auth", 36 + "django.contrib.contenttypes", 37 + "django.contrib.sessions", 38 + "django.contrib.messages", 39 + "django.contrib.staticfiles", 40 + "core", 41 + ] 42 + 43 + MIDDLEWARE = [ 44 + "django.middleware.security.SecurityMiddleware", 45 + "django.contrib.sessions.middleware.SessionMiddleware", 46 + "django.middleware.common.CommonMiddleware", 47 + "django.middleware.csrf.CsrfViewMiddleware", 48 + "django.contrib.auth.middleware.AuthenticationMiddleware", 49 + "django.contrib.messages.middleware.MessageMiddleware", 50 + "django.middleware.clickjacking.XFrameOptionsMiddleware", 51 + ] 52 + 53 + ROOT_URLCONF = "django_docker_demo.urls" 54 + 55 + TEMPLATES = [ 56 + { 57 + "BACKEND": "django.template.backends.django.DjangoTemplates", 58 + "DIRS": [], 59 + "APP_DIRS": True, 60 + "OPTIONS": { 61 + "context_processors": [ 62 + "django.template.context_processors.debug", 63 + "django.template.context_processors.request", 64 + "django.contrib.auth.context_processors.auth", 65 + "django.contrib.messages.context_processors.messages", 66 + ], 67 + }, 68 + }, 69 + ] 70 + 71 + WSGI_APPLICATION = "django_docker_demo.wsgi.application" 72 + 73 + 74 + # Database 75 + # https://docs.djangoproject.com/en/5.0/ref/settings/#databases 76 + import os 77 + 78 + DATABASES = { 79 + "default": { 80 + "ENGINE": "django.db.backends.postgresql", 81 + "NAME": os.environ["POSTGRES_DB"], 82 + "USER": os.environ["POSTGRES_USER"], 83 + "PASSWORD": os.environ["POSTGRES_PASSWORD"], 84 + "HOST": os.environ["DB_HOST"], 85 + "PORT": os.environ["DB_PORT"], 86 + } 87 + } 88 + 89 + 90 + # Password validation 91 + # https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators 92 + 93 + AUTH_PASSWORD_VALIDATORS = [ 94 + { 95 + "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", 96 + }, 97 + { 98 + "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", 99 + }, 100 + { 101 + "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", 102 + }, 103 + { 104 + "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", 105 + }, 106 + ] 107 + 108 + 109 + # Internationalization 110 + # https://docs.djangoproject.com/en/5.0/topics/i18n/ 111 + 112 + LANGUAGE_CODE = "en-us" 113 + 114 + TIME_ZONE = "UTC" 115 + 116 + USE_I18N = True 117 + 118 + USE_TZ = True 119 + 120 + 121 + # Static files (CSS, JavaScript, Images) 122 + # https://docs.djangoproject.com/en/5.0/howto/static-files/ 123 + 124 + STATIC_URL = "static/" 125 + 126 + # Default primary key field type 127 + # https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field 128 + 129 + DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
+23
src/django_docker_demo/urls.py
··· 1 + """ 2 + URL configuration for django_docker_demo project. 3 + 4 + The `urlpatterns` list routes URLs to views. For more information please see: 5 + https://docs.djangoproject.com/en/5.0/topics/http/urls/ 6 + Examples: 7 + Function views 8 + 1. Add an import: from my_app import views 9 + 2. Add a URL to urlpatterns: path('', views.home, name='home') 10 + Class-based views 11 + 1. Add an import: from other_app.views import Home 12 + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 13 + Including another URLconf 14 + 1. Import the include() function: from django.urls import include, path 15 + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 16 + """ 17 + 18 + from django.contrib import admin 19 + from django.urls import path 20 + 21 + urlpatterns = [ 22 + path("admin/", admin.site.urls), 23 + ]
+16
src/django_docker_demo/wsgi.py
··· 1 + """ 2 + WSGI config for django_docker_demo project. 3 + 4 + It exposes the WSGI callable as a module-level variable named ``application``. 5 + 6 + For more information on this file, see 7 + https://docs.djangoproject.com/en/5.0/howto/deployment/wsgi/ 8 + """ 9 + 10 + import os 11 + 12 + from django.core.wsgi import get_wsgi_application 13 + 14 + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_docker_demo.settings") 15 + 16 + application = get_wsgi_application()
+7
src/entrypoint.sh
··· 1 + #!/bin/sh 2 + 3 + echo "Running migrations..." 4 + python manage.py migrate 5 + 6 + echo "Starting server..." 7 + python manage.py runserver 0.0.0.0:8000
+22
src/manage.py
··· 1 + #!/usr/bin/env python 2 + """Django's command-line utility for administrative tasks.""" 3 + import os 4 + import sys 5 + 6 + 7 + def main(): 8 + """Run administrative tasks.""" 9 + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_docker_demo.settings") 10 + try: 11 + from django.core.management import execute_from_command_line 12 + except ImportError as exc: 13 + raise ImportError( 14 + "Couldn't import Django. Are you sure it's installed and " 15 + "available on your PYTHONPATH environment variable? Did you " 16 + "forget to activate a virtual environment?" 17 + ) from exc 18 + execute_from_command_line(sys.argv) 19 + 20 + 21 + if __name__ == "__main__": 22 + main()
+2
src/requirements.txt
··· 1 + django 2 + psycopg[binary]