Skip to content

Commit

Permalink
Merge pull request #35 from thisisankit27/pre-deploy
Browse files Browse the repository at this point in the history
.env accessing & dockerfile & docker-compose.yml [optimised] + Readme.md
  • Loading branch information
thisisankit27 authored Feb 24, 2024
2 parents 7c114ae + 94b1bf4 commit 59081f5
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 41 deletions.
164 changes: 163 additions & 1 deletion MasterProject/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,164 @@
.env
staticfiles/
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/
21 changes: 21 additions & 0 deletions MasterProject/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
6 changes: 2 additions & 4 deletions MasterProject/MasterProject/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = []

Expand Down
9 changes: 9 additions & 0 deletions MasterProject/MasterProject/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 14 additions & 7 deletions MasterProject/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
- 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"
4 changes: 4 additions & 0 deletions MasterProject/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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}"
19 changes: 0 additions & 19 deletions MasterProject/dockerfile

This file was deleted.

2 changes: 2 additions & 0 deletions MasterProject/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import sys
import webbrowser
import environ

flag_file = "flag.txt"

Expand Down Expand Up @@ -36,4 +37,5 @@ def main():


if __name__ == '__main__':
environ.Env.read_env('.env')
main()
2 changes: 1 addition & 1 deletion MasterProject/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
36 changes: 27 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand All @@ -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=<your-secret-key>
```

Expand All @@ -49,15 +51,20 @@ 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:

```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:
Expand All @@ -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=<your-secret-key>
```

Replace `<your-secret-key>` 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.

0 comments on commit 59081f5

Please sign in to comment.