Deploy application in container using compose to bring up db

This commit is contained in:
2025-11-19 13:37:34 +01:00
parent d5befdd018
commit ae86a2e5fa
10 changed files with 230 additions and 10 deletions

View File

@@ -11,6 +11,7 @@ import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'cntmanage.settings')
settings = os.environ.get("DJANGO_SETTINGS_MODULE", "cntmanage.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings)
application = get_asgi_application()

View File

@@ -28,7 +28,6 @@ DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
@@ -127,9 +126,9 @@ LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_I18N = False # Disable translation engine
USE_TZ = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)

View File

@@ -0,0 +1,142 @@
"""
Django settings for techdb project.
Generated by 'django-admin startproject' using Django 5.1.2.
For more information on this file, see
https://docs.djangoproject.com/en/5.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.1/ref/settings/
"""
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('SECRET_KEY', 'django-insecure-s%(9^y#!1*ge)7u%$vf3zp0lisgd%=(k@$13&ej13p5(ei71hi')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
# Allowed hosts list is necessary for when in production
ALLOWED_HOSTS = ['*']
# Using a secure-only session cookie makes it more difficult for network traffic sniffers to hijack user sessions.
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'nested_admin',
'flightslot',
'durationwidget',
'colorfield',
'import_export',
'django_admin_action_forms',
]
# Import Export plugin settings
from import_export.formats.base_formats import CSV
IMPORT_EXPORT_USE_TRANSACTIONS = True
IMPORT_EXPORT_SKIP_ADMIN_LOG = True
IMPORT_FORMATS = [CSV]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'cntmanage.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['/var/www/templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'cntmanage.wsgi.application'
# Database
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('DB_NAME','techstorage'),
'USER': os.environ.get('DB_USER','tech'),
'PASSWORD': os.environ.get('DB_PASSWORD','tech'),
'HOST': os.environ.get('DB_HOST','postgresql'),
'PORT': os.environ.get('DB_PORT','5432')
}
}
# Password validation
# https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/5.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = False # Disable translation engine
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/
STATIC_URL = "static/"
STATIC_ROOT = "/var/www/static/"
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
# Default primary key field type
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

View File

@@ -16,8 +16,10 @@ Including another URLconf
"""
from django.contrib import admin
from django.urls import path
from django.views.generic import RedirectView
urlpatterns = [
path('', RedirectView.as_view(url='/admin/', permanent=False)),
path('admin/', admin.site.urls),
]

View File

@@ -11,6 +11,7 @@ import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'cntmanage.settings')
settings = os.environ.get("DJANGO_SETTINGS_MODULE", "cntmanage.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings)
application = get_wsgi_application()

View File

@@ -1,8 +1,5 @@
# Use postgres/example user/password credentials
version: '3.9'
services:
postgresql:
image: postgres:17.0
container_name: tech-postgresql
@@ -19,3 +16,20 @@ services:
POSTGRED_DB: techstorage
PGDATA: /var/lib/postgresql/data
flightslot:
image: flightslot:dev
container_name: tech-flightslot
restart: unless-stopped
ports:
- 8000:8000
depends_on:
- postgresql
environment:
- DJANGO_SETTINGS_MODULE=cntmanage.settings_prod
- SECRET_KEY=6WIjA!+mI+ZOWHaJm6v^8F4o,@-gliDtwkp*QFvpkFe"Oo0quq
- DB_NAME=techstorage
- DB_USER=tech
- DB_PASSWORD=tech
- DB_HOST=postgresql
- DB_PORT=5432

20
cntmanage/docker/entrypoint.sh Executable file
View File

@@ -0,0 +1,20 @@
#!/bin/sh
set -e
echo "📦 Starting Django deploy script..."
echo "🔧 Running migrations..."
django-admin migrate --noinput
echo "📁 Collecting static files..."
django-admin collectstatic --noinput
echo "📁 Manually copying static files..."
cp -v /app/static/* /var/www/static/
echo "📁 Manually copying template files..."
cp -rv /app/templates/ /var/www/templates/
echo "🚀 Launching Flightslot..."
exec "$@"

View File

@@ -0,0 +1,40 @@
### STAGE 1 - Builder image ###
# Builder container
FROM python:3.12 AS builder
# Install Poetry
RUN curl -sSL https://install.python-poetry.org | python3 -
ENV PATH="${PATH}:/root/.local/bin"
RUN env
# Create build directory
WORKDIR /build
# Copy project files
COPY . ./
# Run poetry update to download dependencies
RUN poetry update --no-interaction --no-ansi
# Build project
RUN poetry build
### STAGE 2 — Final image
FROM python:3.12-slim AS deploy
WORKDIR /app
# Copy application custom static files
RUN mkdir -p static
COPY ./static/cantorair.jpg ./static
# Copy application custom templates for admin page
RUN mkdir -p /templates/admin
COPY ./templates/admin/* ./templates/admin/
# Copy and install application wheel package
COPY --from=builder /build/dist/*.whl ./
RUN pip install --no-cache-dir *.whl
RUN pip install gunicorn whitenoise
# Copy entryupoint bash script
COPY ./docker/entrypoint.sh ./
ENTRYPOINT ["/app/entrypoint.sh"]
# Command to be executed after entry point
CMD ["gunicorn", "cntmanage.wsgi:application", "--bind", "0.0.0.0:8000"]

View File

@@ -58,7 +58,7 @@ class StudentAdmin(ImportMixin, AdminActionFormsMixin, admin.ModelAdmin):
def password(self, obj: Student) -> SafeText:
return SafeText(obj.default_password())
@admin.action(description="Disable Students")
@admin.action(description="Deactivate Students")
def disable_students(self, request: HttpRequest, queryset: QuerySet[Student]):
for q in queryset.all():
if q.user:

View File

@@ -1,11 +1,12 @@
[tool.poetry]
name = "cntmanage"
version = "0.1.0"
packages = [{include = "flightslot"}]
packages = [{include = "flightslot"}, {include = "cntmanage"}]
description = "CantorAir Flight Scheduler"
authors = ["Emanuele <ema.trabattoni@gmail.com>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.12"
django = "^5.1.2"