From 94b1bf48341bd7fcc20a7a2fda7be0408146bad4 Mon Sep 17 00:00:00 2001 From: thisisankit27 Date: Sat, 24 Feb 2024 01:39:07 +0530 Subject: [PATCH] .env accessing + dockerfile & docker-compose.yml [optimised] + Readme.md Signed-off-by: thisisankit27 --- MasterProject/.dockerignore | 164 +++++++++++++++++++++++- MasterProject/Dockerfile | 21 +++ MasterProject/MasterProject/settings.py | 6 +- MasterProject/MasterProject/wsgi.py | 9 ++ MasterProject/docker-compose.yml | 21 ++- MasterProject/docker-entrypoint.sh | 4 + MasterProject/dockerfile | 19 --- MasterProject/manage.py | 2 + MasterProject/requirements.txt | 2 +- README.md | 36 ++++-- 10 files changed, 243 insertions(+), 41 deletions(-) create mode 100644 MasterProject/Dockerfile create mode 100644 MasterProject/docker-entrypoint.sh delete mode 100644 MasterProject/dockerfile diff --git a/MasterProject/.dockerignore b/MasterProject/.dockerignore index fa22177..d87811d 100644 --- a/MasterProject/.dockerignore +++ b/MasterProject/.dockerignore @@ -1,2 +1,164 @@ .env -staticfiles/ \ No newline at end of file +staticfiles/ +snapspeak/ + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# 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/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ \ No newline at end of file diff --git a/MasterProject/Dockerfile b/MasterProject/Dockerfile new file mode 100644 index 0000000..91d7eb7 --- /dev/null +++ b/MasterProject/Dockerfile @@ -0,0 +1,21 @@ +FROM python:3.10.13-slim + +ENV PYTHONBUFFERED=1 + +COPY . /django +WORKDIR /django + +RUN python3 -m venv /opt/venv + +RUN /opt/venv/bin/pip install --upgrade pip && \ + /opt/venv/bin/pip install -r requirements.txt && \ + apt-get update && \ + apt-get install -y tesseract-ocr + +RUN /opt/venv/bin/python manage.py collectstatic --noinput + +RUN chmod +x docker-entrypoint.sh + +CMD ["/django/docker-entrypoint.sh"] + +EXPOSE 8000 \ No newline at end of file diff --git a/MasterProject/MasterProject/settings.py b/MasterProject/MasterProject/settings.py index afd83fd..b569949 100644 --- a/MasterProject/MasterProject/settings.py +++ b/MasterProject/MasterProject/settings.py @@ -15,8 +15,6 @@ import os os.environ['CUDA_VISIBLE_DEVICES'] = '' -env = environ.Env() -environ.Env.read_env() # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -26,10 +24,10 @@ # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = env('SECRET_KEY') +SECRET_KEY = os.environ.get('SECRET_KEY') # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = os.environ.get('DEBUG') ALLOWED_HOSTS = [] diff --git a/MasterProject/MasterProject/wsgi.py b/MasterProject/MasterProject/wsgi.py index 019f139..c21f99d 100644 --- a/MasterProject/MasterProject/wsgi.py +++ b/MasterProject/MasterProject/wsgi.py @@ -8,6 +8,15 @@ """ import os +import pathlib +import environ + +CURRENT_DIR = pathlib.Path(__file__).resolve().parent +BASE_DIR = CURRENT_DIR.parent +ENV_FILE_PATH = BASE_DIR / '.env' + +env = environ.Env() +env.read_env(str(ENV_FILE_PATH)) from django.core.wsgi import get_wsgi_application diff --git a/MasterProject/docker-compose.yml b/MasterProject/docker-compose.yml index b663e6b..45a0f93 100644 --- a/MasterProject/docker-compose.yml +++ b/MasterProject/docker-compose.yml @@ -1,11 +1,18 @@ version: "3.8" services: app: - build: . - volumes: - - .:/django + build: + context: . + dockerfile: Dockerfile + image: snapspeak-django:v1 + environment: + - PORT=8020 + env_file: + - .env ports: - - 8000:8000 - image: application:django - container_name: app_container_django - command: gunicorn MasterProject.wsgi:application --bind 0.0.0.0:8000 \ No newline at end of file + - 8001:8020 + # volumes: + # - .:/django + container_name: snapspeak_app_container + # command: gunicorn MasterProject.wsgi:application --bind 0.0.0.0:8000 + command: sh -c "/django/docker-entrypoint.sh" \ No newline at end of file diff --git a/MasterProject/docker-entrypoint.sh b/MasterProject/docker-entrypoint.sh new file mode 100644 index 0000000..3812d22 --- /dev/null +++ b/MasterProject/docker-entrypoint.sh @@ -0,0 +1,4 @@ +#!/bin/bash +APP_PORT=${PORT:-8000} +cd /django/ +/opt/venv/bin/gunicorn --worker-tmp-dir /dev/shm MasterProject.wsgi:application --bind "0.0.0.0:${APP_PORT}" \ No newline at end of file diff --git a/MasterProject/dockerfile b/MasterProject/dockerfile deleted file mode 100644 index 2fa3516..0000000 --- a/MasterProject/dockerfile +++ /dev/null @@ -1,19 +0,0 @@ -FROM python:3.10.13-slim - -ENV PYTHONBUFFERED=1 - -WORKDIR /django - -COPY requirements.txt requirements.txt - -RUN pip install -r requirements.txt - -RUN apt-get update && apt-get install -y tesseract-ocr - -COPY . . - -RUN python manage.py collectstatic --noinput - -CMD gunicorn MasterProject.wsgi:application --bind 0.0.0.0:8000 - -EXPOSE 8000 \ No newline at end of file diff --git a/MasterProject/manage.py b/MasterProject/manage.py index 62dea74..7acb33c 100644 --- a/MasterProject/manage.py +++ b/MasterProject/manage.py @@ -3,6 +3,7 @@ import os import sys import webbrowser +import environ flag_file = "flag.txt" @@ -36,4 +37,5 @@ def main(): if __name__ == '__main__': + environ.Env.read_env('.env') main() diff --git a/MasterProject/requirements.txt b/MasterProject/requirements.txt index 1609788..13a0aed 100644 --- a/MasterProject/requirements.txt +++ b/MasterProject/requirements.txt @@ -1,5 +1,5 @@ Django==5.0.1 -django_environ==0.11.2 +django-environ==0.11.2 Pillow==10.2.0 pytesseract==0.3.10 gunicorn==21.2.0 diff --git a/README.md b/README.md index 145c096..6dd47ee 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ Before you begin, make sure you have the following installed and set up on your 2. Navigate to the project directory: ```bash + cd SnapSpeak cd MasterProject ``` @@ -36,9 +37,10 @@ Before you begin, make sure you have the following installed and set up on your pip install -r requirements.txt ``` -4. Go to the `MasterProject` directory and create a `.env` file in the same folder as `settings.py`. Add the following line to the `.env` file: +4. Create a `.env` file in the same folder as `manage.py`. Add the following line to the `.env` file: ```dotenv + DEBUG=1 SECRET_KEY= ``` @@ -49,7 +51,7 @@ Before you begin, make sure you have the following installed and set up on your ```bash python manage.py collectstatic ``` - + ## Run the Application Run the following command to start the Django development server: @@ -57,7 +59,12 @@ Run the following command to start the Django development server: ```bash python manage.py runserver ``` +or +```bash +gunicorn MasterProject.wsgi:application --bind 127.0.0.1:8000 +``` + ## Docker Installation SnapSpeak can also be deployed using Docker for easier setup and portability. Follow these steps to run the application using Docker: @@ -73,27 +80,38 @@ SnapSpeak can also be deployed using Docker for easier setup and portability. Fo 3. Navigate to the project directory: ```bash + cd SnapSpeak cd MasterProject ``` -4. Build and start the Docker containers using Docker Compose: +4. Create a `.env` file in the same folder as `manage.py`. Add the following line to the `.env` file: + + ```dotenv + DEBUG=1 + SECRET_KEY= + ``` + + Replace `` with a securely generated secret key for your Django application. You can use online tools or Django's `django.core.management.utils.get_random_secret_key()` method to generate a new key. Make sure to keep this key confidential and never share it publicly. + + +5. Build and start the Docker containers using Docker Compose: ```bash - docker-compose up --build + docker compose up --build ``` - This command will build the Docker images and start the SnapSpeak application along with its dependencies. + This command will build the Docker image and start the SnapSpeak application along with its dependencies. -5. Access the SnapSpeak application by visiting [http://localhost:8000/](http://localhost:8000/) in your web browser. +6. Access the SnapSpeak application by visiting [http://localhost:8001/](http://localhost:8001/) in your web browser. The application should now be running in a Docker container, providing a convenient and isolated environment for SnapSpeak. -6. To stop the Docker containers, use the following command: +7. To stop the Docker containers, use the following command: ```bash - docker-compose down + docker compose down ``` This will stop and remove the containers. -**Note**: Ensure that port 8000 on your local machine is available and not occupied by another service before running Docker Compose. Adjust the `docker-compose.yml` file if you need to change the port configuration. +**Note**: Ensure that port 8001 on your local machine is available and not occupied by another service before running Docker Compose. Adjust the `docker-compose.yml` file if you need to change the port configuration.