jharsh1202 / jyn_pathshala

A Django-based backend application developed for a non-profit organization dedicated to conducting weekly Jain Pathshala classes. This system aims to streamline the management of Pathshala classes, including student registration, class scheduling, attendance tracking, and resource management.
MIT License
0 stars 0 forks source link

Investigate and Implement Lighter Docker Image for Production Deployment (DockerFile Optimization) #5

Closed jharsh1202 closed 7 months ago

jharsh1202 commented 7 months ago

Description As part of our ongoing efforts to optimize resource usage and streamline our production environment, we need to explore and adopt a lighter Docker image for our Python Django application.

Objective:

Pratham271 commented 7 months ago

I can work on this.

jharsh1202 commented 7 months ago

Great @Pratham271 , let me know if you face any problem while working on this issue! please create a new branch python-docker-image-optimization from master to work on this

Pratham271 commented 7 months ago

On it, and sure I'll let you know if I face any problem thanks for the heads up

Pratham271 commented 7 months ago

Whenever I am trying to run docker compose up -d with your setup it is giving me the error ERROR [celery 6/6] RUN python manage.py migrate 0.3s

[celery 6/6] RUN python manage.py migrate: 0.271 Traceback (most recent call last): 0.271 File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 412, in run_from_argv 0.271 self.execute(*args, *cmd_options) 0.271 File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 458, in execute 0.271 output = self.handle(args, **options) 0.271 File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 103, in wrapper 0.271 saved_locale = translation.get_language() 0.271 File "/usr/local/lib/python3.9/site-packages/django/utils/translation/init.py", line 210, in get_language 0.272 return _trans.get_language() 0.272 File "/usr/local/lib/python3.9/site-packages/django/utils/translation/init.py", line 65, in getattr 0.272 if settings.USE_I18N: 0.272 File "/usr/local/lib/python3.9/site-packages/django/conf/init.py", line 102, in getattr 0.272 self._setup(name) 0.272 File "/usr/local/lib/python3.9/site-packages/django/conf/init.py", line 89, in _setup 0.272 self._wrapped = Settings(settings_module) 0.272 File "/usr/local/lib/python3.9/site-packages/django/conf/init.py", line 217, in init 0.272 mod = importlib.import_module(self.SETTINGS_MODULE) 0.272 File "/usr/local/lib/python3.9/importlib/init.py", line 127, in import_module 0.272 return _bootstrap._gcd_import(name[level:], package, level) 0.272 File "", line 1030, in _gcd_import 0.272 File "", line 1007, in _find_and_load 0.272 File "", line 984, in _find_and_load_unlocked 0.272 ModuleNotFoundError: No module named 'jyn_pathshala.settings' 0.272 0.272 During handling of the above exception, another exception occurred: 0.272 0.272 Traceback (most recent call last): 0.272 File "/usr/src/app/manage.py", line 22, in 0.272 main() 0.272 File "/usr/src/app/manage.py", line 18, in main 0.272 execute_from_command_line(sys.argv) 0.272 File "/usr/local/lib/python3.9/site-packages/django/core/management/init.py", line 442, in execute_from_command_line 0.273 utility.execute() 0.273 File "/usr/local/lib/python3.9/site-packages/django/core/management/init.py", line 436, in execute 0.273 self.fetch_command(subcommand).run_from_argv(self.argv) 0.273 File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 425, in run_from_argv 0.273 connections.close_all() 0.273 File "/usr/local/lib/python3.9/site-packages/django/utils/connection.py", line 84, in close_all 0.273 for conn in self.all(initialized_only=True): 0.273 File "/usr/local/lib/python3.9/site-packages/django/utils/connection.py", line 76, in all 0.273 return [ 0.273 File "/usr/local/lib/python3.9/site-packages/django/utils/connection.py", line 73, in iter 0.273 return iter(self.settings) 0.273 File "/usr/local/lib/python3.9/site-packages/django/utils/functional.py", line 57, in get 0.274 res = instance.dict[self.name] = self.func(instance) 0.274 File "/usr/local/lib/python3.9/site-packages/django/utils/connection.py", line 45, in settings 0.274 self._settings = self.configure_settings(self._settings) 0.274 File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 148, in configure_settings 0.275 databases = super().configure_settings(databases) 0.275 File "/usr/local/lib/python3.9/site-packages/django/utils/connection.py", line 50, in configure_settings 0.275 settings = getattr(django_settings, self.settings_name) 0.275 File "/usr/local/lib/python3.9/site-packages/django/conf/init.py", line 102, in getattr 0.275 self._setup(name) 0.275 File "/usr/local/lib/python3.9/site-packages/django/conf/init.py", line 89, in _setup 0.275 self._wrapped = Settings(settings_module) 0.275 File "/usr/local/lib/python3.9/site-packages/django/conf/init.py", line 217, in init 0.275 mod = importlib.import_module(self.SETTINGS_MODULE) 0.275 File "/usr/local/lib/python3.9/importlib/init.py", line 127, in import_module 0.275 return _bootstrap._gcd_import(name[level:], package, level) 0.275 File "", line 1030, in _gcd_import 0.275 File "", line 1007, in _find_and_load 0.275 File "", line 984, in _find_and_load_unlocked 0.275 ModuleNotFoundError: No module named 'jyn_pathshala.settings'

failed to solve: process "/bin/sh -c python manage.py migrate" did not complete successfully: exit code: 1

jharsh1202 commented 7 months ago

basically you'll need a settings.py file let me share one with you :)

jharsh1202 commented 7 months ago

@Pratham271 let me know if that works for you.

"""
Django settings for jyn_pathshala project.

Generated by 'django-admin startproject' using Django 4.2.7.

For more information on this file, see
https://docs.djangoproject.com/en/4.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.2/ref/settings/
"""

from pathlib import Path
from datetime import timedelta
from celery.schedules import crontab
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/4.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ''

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ["127.0.0.1", "localhost"]
CORS_ALLOWED_ORIGINS = [
    "",  # Your trusted origin
]
CORS_ALLOW_HEADERS = [
    'csrftoken',
]

# CREDENTIALS
AZURE_ACCOUNT_NAME = ''
AZURE_ACCOUNT_KEY = ''
AZURE_ACCOUNT_CONTAINER_NAME = ''

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'rest_framework_simplejwt',
    'corsheaders',
    'pathshala',
    'django_celery_beat',
    'rest_framework_simplejwt.token_blacklist',
]

# Authentication
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ),
}

# JWT settings
SIMPLE_JWT = {
    'ACCESS_TOKEN_LIFETIME': timedelta(days=100, minutes=15),
    'REFRESH_TOKEN_LIFETIME': timedelta(days=1),
    'ROTATE_REFRESH_TOKENS': True,
    'BLACKLIST_AFTER_ROTATION': True,
    'ALGORITHM': 'HS256',
    'SIGNING_KEY': SECRET_KEY,
    'VERIFYING_KEY': None,
    'AUTH_HEADER_TYPES': ('Bearer',),
}

CELERY_BROKER_URL = 'redis://redis:6379/0'
CELERY_RESULT_BACKEND = 'redis://redis:6379/0'

# settings.py

# Celery Beat Configuration
CELERY_BEAT_SCHEDULE = {
    'create-record-weekly': {
        'task': 'pathshala.tasks.create_session_records',  
        'schedule': crontab(day_of_week='monday'),  # Every Monday at 12 AM
    },
    'backup-db-to-azure-blob-weekly': {
        'task': 'pathshala.tasks.backup_db_to_azure_blob',
        'schedule': crontab(hour=14, minute=0, day_of_week='monday'), # Every Monday at 2 PM
    },
}

# Media 
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    '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',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
]

ROOT_URLCONF = 'jyn_pathshala.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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 = 'jyn_pathshala.wsgi.application'

# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

# Password validation
# https://docs.djangoproject.com/en/4.2/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/4.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Kolkata'

USE_I18N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
jharsh1202 commented 7 months ago

are we still on this? @Pratham271 ?

Pratham271 commented 7 months ago

Yeah I am good to go now there were few more errors that documentation helped me solve got busy with something else too.