From 84f296ef02c685b967711e156d48633bfa355600 Mon Sep 17 00:00:00 2001 From: hridesh Date: Tue, 6 Aug 2024 22:41:54 +0530 Subject: [PATCH] feat: dockerised backend --- .dockerignore | 89 +++++++++++++++++++++++++++++++++ attendence_tracker/celery.py | 2 - docker-compose.yml | 50 ++++++++++++++++++ docker/celery/Dockerfile | 6 +++ docker/web/Dockerfile | 8 +++ docker/web/docker-entrypoint.sh | 6 +++ manage.py | 3 -- 7 files changed, 159 insertions(+), 5 deletions(-) create mode 100644 .dockerignore create mode 100644 docker-compose.yml create mode 100644 docker/celery/Dockerfile create mode 100644 docker/web/Dockerfile create mode 100755 docker/web/docker-entrypoint.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..0a53e44 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,89 @@ +# Git +.git +.gitignore +.gitattributes + + +# CI +.codeclimate.yml +.travis.yml +.taskcluster.yml + +# Docker +docker-compose.yml +Dockerfile +.docker +.dockerignore + +# Byte-compiled / optimized / DLL files +**/__pycache__/ +**/*.py[cod] + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.cache +nosetests.xml +coverage.xml + +# Translations +*.mo +*.pot + +# Django stuff: +*.log + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Virtual environment +.env +.venv/ +venv/ + +# PyCharm +.idea + +# Python mode for VIM +.ropeproject +**/.ropeproject + +# Vim swap files +**/*.swp + +# VS Code +.vscode/ diff --git a/attendence_tracker/celery.py b/attendence_tracker/celery.py index 5250ddf..55fa9c2 100644 --- a/attendence_tracker/celery.py +++ b/attendence_tracker/celery.py @@ -3,13 +3,11 @@ import django from celery import Celery -from dotenv import load_dotenv if "WEBSITE_HOSTNAME" in os.environ: os.environ.setdefault("DJANGO_SETTINGS_MODULE", "attendence_tracker.production") broker_url = os.getenv("REDIS_BROKER_CONNECTIONSTRING") else: - load_dotenv("./.creds") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "attendence_tracker.settings") broker_url = os.getenv("BROKERLOCATION") diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..65d570e --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,50 @@ +# This is an Extension: https://github.com/compose-spec/compose-spec/blob/main/spec.md#extension +x-environment: &env + environment: + DBNAME: development + DBUSER: development + DBPASS: development + DBHOST: postgres + CACHELOCATION: redis://redis:6379/0 + BROKERLOCATION: redis://redis:6379/1 + SECRET_KEY: django-insecure-ys)is-uls_$yaa(f%iyy^^7pe4a@ql)3thr9loszz#!8l4m4fk + +services: + django: + build: + context: . + dockerfile: ./docker/web/Dockerfile + entrypoint: ./docker/web/docker-entrypoint.sh + volumes: + - ./:/app # Mount the current directory to /app within the container + ports: + - "8000:8000" + depends_on: + - redis + - postgres + <<: *env # This is a YAML merge + + celery: + build: + context: . + dockerfile: ./docker/celery/Dockerfile + volumes: + - ./:/app + depends_on: + - django # Migrations need to be run before celery can start because of django-celery-results + <<: *env + + redis: + image: redis:alpine + + postgres: + image: postgres:alpine + volumes: + - db-vol:/var/lib/postgresql/data + environment: + - POSTGRES_PASSWORD=development + - POSTGRES_USER=development + - POSTGRES_DB=development + +volumes: + db-vol: diff --git a/docker/celery/Dockerfile b/docker/celery/Dockerfile new file mode 100644 index 0000000..ac61a83 --- /dev/null +++ b/docker/celery/Dockerfile @@ -0,0 +1,6 @@ +FROM python:alpine +WORKDIR /app +COPY requirements.txt . +RUN pip install -r requirements.txt +COPY . . +CMD celery -A attendence_tracker worker -l info diff --git a/docker/web/Dockerfile b/docker/web/Dockerfile new file mode 100644 index 0000000..8d532ba --- /dev/null +++ b/docker/web/Dockerfile @@ -0,0 +1,8 @@ +FROM python:alpine +WORKDIR /app +COPY requirements.txt . +RUN pip install -r requirements.txt +COPY . . +CMD python manage.py runserver 0.0.0.0:8000 +EXPOSE 3000 +ENTRYPOINT [ "./docker-entrypoint.sh" ] diff --git a/docker/web/docker-entrypoint.sh b/docker/web/docker-entrypoint.sh new file mode 100755 index 0000000..00c084d --- /dev/null +++ b/docker/web/docker-entrypoint.sh @@ -0,0 +1,6 @@ +#!/bin/sh +echo "Running Migrations" +python manage.py migrate + +echo "Starting Server" +python manage.py runserver 0.0.0.0:8000 diff --git a/manage.py b/manage.py index 2ce64bc..474a5fe 100755 --- a/manage.py +++ b/manage.py @@ -3,8 +3,6 @@ import os import sys -from dotenv import load_dotenv - def main(): """Run administrative tasks.""" @@ -14,7 +12,6 @@ def main(): os.environ.setdefault("DJANGO_SETTINGS_MODULE", "attendence_tracker.production") else: print("Loading environment variables for .env file") - load_dotenv("./.creds") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "attendence_tracker.settings") try: