Skip to content

Commit

Permalink
Deploy using docker #69 (#70)
Browse files Browse the repository at this point in the history
* Added configuration to deploy using docker

* Fixed docker production file

* Fixed docker production file

* Fixed docker production file

* Fixed docker production file

* Updated dockerfile

* Updated dockerfile

* Updated dockerfile

* Fixed nginx config

* Fixed nginx config

* Fixed nginx config

* Updated nginx dockerfile

* Updated nginx dockerfile

* Updated nginx dockerfile

* Added env keys

* Updated settings

* Updated dockerfile

* Updated dockerfile

* Updated dockerfile

* Updated dockerfile

* Updated dockerfile

* Updated dockerfile

* Updated dockerfile

* Updated dockerfile

* Updated dockerfile

* Updated dockerfile

* Updated settings

* Updated settings

* Updated dockerfile

* Updated dockerfile

* Updated dockerfile

* Updated settings

* Updated settings

* Updated settings

* Updated settings

* Updated settings

* Updated settings

* Dockerfile updated

* Dockerfile updated for production

* Dockerfile updated for production

* Dockerfile updated for production

* Fixed Dockerfile env variable
  • Loading branch information
manjurulhoque authored Jan 28, 2023
1 parent 5c8ee27 commit 0eab365
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 26 deletions.
18 changes: 16 additions & 2 deletions .env.dev.sample
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
# Third-party login keys
SOCIAL_AUTH_GITHUB_KEY=githubt-key
SOCIAL_AUTH_GITHUB_KEY=github-key
SOCIAL_AUTH_GITHUB_SECRET=github-secret

SOCIAL_AUTH_FACEBOOK_KEY=key
SOCIAL_AUTH_FACEBOOK_SECRET=secret

SOCIAL_AUTH_LINKEDIN_OAUTH2_KEY=key
SOCIAL_AUTH_LINKEDIN_OAUTH2_SECRET=secret

SOCIAL_AUTH_GOOGLE_OAUTH2_KEY=key
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET=secret

RECAPTCHA_PUBLIC_KEY=key
RECAPTCHA_PRIVATE_KEY=secret

# To enable prometheus monitoring
ENABLE_PROMETHEUS=1
ENABLE_PROMETHEUS=0

DEBUG=False
19 changes: 14 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
FROM python:3.8-buster

WORKDIR /app
WORKDIR /usr/src/app

COPY requirements.txt /app/
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

COPY requirements.txt /usr/src/app

RUN python -m pip install --upgrade pip
RUN pip install -r requirements.txt

COPY . /app/
COPY . /usr/src/app

RUN cp .env.dev.sample .env

EXPOSE 8000
#EXPOSE 8000

RUN chmod +x entrypoint.sh

CMD ["sh", "entrypoint.sh"]
ENV APP_HOME=/usr/src/app
ENV DEBUG=1
RUN mkdir $APP_HOME/staticfiles
RUN mkdir $APP_HOME/mediafiles

CMD ["sh", "entrypoint.sh"]
36 changes: 36 additions & 0 deletions Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
FROM python:3.8-buster

WORKDIR /usr/src/app

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

COPY requirements.txt /usr/src/app

RUN python -m pip install --upgrade pip
RUN pip install -r requirements.txt

COPY . /usr/src/app

RUN cp .env.dev.sample .env

RUN apt-get update
RUN apt-get -y install nano

#EXPOSE 8000

RUN chmod +x entrypoint.prod.sh

ENV APP_HOME=/usr/src/app
RUN mkdir $APP_HOME/staticfiles
RUN mkdir $APP_HOME/mediafiles

RUN mkdir -p staticfiles
RUN mkdir -p mediafiles

RUN echo "Running from production dockerfile"

# Collect static files
RUN python manage.py collectstatic --noinput

CMD ["sh", "entrypoint.prod.sh"]
File renamed without changes.
11 changes: 11 additions & 0 deletions deployment/nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#FROM tutum/nginx
##FROM nginx:1.21-alpine
#
#RUN rm /etc/nginx/sites-enabled/default || true
#
#COPY sites-enabled/ /etc/nginx/sites-enabled

FROM nginx:1.21-alpine

RUN rm /etc/nginx/conf.d/default.conf || true
COPY nginx.conf /etc/nginx/conf.d
43 changes: 43 additions & 0 deletions deployment/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# server {
#
# listen 80 default_server;
# server_name default_server;
# charset utf-8;
#
# location /static {
# alias /usr/src/app/jobs/staticfiles;
# }
#
# location / {
# proxy_pass http://web:8000;
# proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# }
#
# }

upstream hello_django {
server web:8000;
}

server {

listen 80;

location / {
proxy_pass http://web:8000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}

location /static/ {
alias /usr/src/app/staticfiles/;
}

location /media/ {
alias /usr/src/app/mediafiles/;
}

}
17 changes: 17 additions & 0 deletions deployment/nginx/sites-enabled/django_project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
server {

listen 80;
charset utf-8;

location /static {
alias /usr/src/app/jobs/static;
}

location / {
proxy_pass http://web:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

}
30 changes: 30 additions & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: '3'

services:
web:
restart: always
build:
context: .
dockerfile: Dockerfile.prod
expose:
- "8000"
volumes:
- static_volume:/usr/src/app/staticfiles
- media_volume:/usr/src/app/mediafiles
# env_file: .env
command: gunicorn jobs.wsgi:application --bind 0.0.0.0:8000

nginx:
restart: always
build: ./deployment/nginx/
ports:
- "80:80"
volumes:
- static_volume:/usr/src/app/staticfiles
- media_volume:/usr/src/app/mediafiles
depends_on:
- web

volumes:
static_volume:
media_volume:
9 changes: 7 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
version: '3'

services:
portal:
web:
restart: always
build: .
command: ./entrypoint.sh
volumes:
- .:/app
- .db_data/db.sqlite3:/db.sqlite3
- static_volume:/usr/src/app/jobs/staticfiles
ports:
- "8000:8000"
- "8000:8000"

volumes:
static_volume:
5 changes: 5 additions & 0 deletions entrypoint.prod.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

#/usr/local/bin/gunicorn jobs.wsgi:application -w 2 -b :8000

#gunicorn jobs.wsgi:application --bind 0.0.0.0:8000
3 changes: 2 additions & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash

python manage.py migrate
python manage.py runserver 0.0.0.0:8000
python manage.py runserver 0.0.0.0:8000
#/usr/local/bin/gunicorn jobs.wsgi:application -w 2 -b :8000
14 changes: 8 additions & 6 deletions jobs/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

SECRET_KEY = "@pzqp#x^+#(olu#wy(6=mi9&a8n+g&x#af#apn07@j=5oin=xb"

DEBUG = env("DEBUG", default=False)
print("DEBUG: ", DEBUG)

# DEBUG = True
SITE_ID = 1
INSTALLED_APPS = [
Expand Down Expand Up @@ -134,7 +137,7 @@

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

STATIC_ROOT = os.path.join(PROJECT_ROOT, "staticfiles")
# STATIC_ROOT = os.path.join(PROJECT_ROOT, "staticfiles")
# ALLOWED_HOSTS = ['django-portal.herokuapp.com', 'localhost', 'jobs.manjurulhoque.com', '127.0.0.1', 'localhost:3000']
# cors config
CORS_ORIGIN_ALLOW_ALL = True
Expand All @@ -155,11 +158,12 @@
)

STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles") # for production
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
STATICFILES_STORAGE = "whitenoise.storage.CompressedStaticFilesStorage"
# STATICFILES_STORAGE = "whitenoise.storage.CompressedStaticFilesStorage"

MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_ROOT = os.path.join(BASE_DIR, "mediafiles")

AUTH_USER_MODEL = "accounts.user"

Expand Down Expand Up @@ -246,8 +250,6 @@
"USE_SESSION_AUTH": False,
}

DEBUG = True

LOGIN_URL = reverse_lazy("accounts:login")
LOGIN_REDIRECT_URL = "/"
LOGOUT_REDIRECT_URL = "/"
Expand Down Expand Up @@ -314,5 +316,5 @@
if ENABLE_PROMETHEUS:
INSTALLED_APPS += ["django_prometheus"]
MIDDLEWARE = ['django_prometheus.middleware.PrometheusBeforeMiddleware'] + MIDDLEWARE + \
['django_prometheus.middleware.PrometheusAfterMiddleware']
['django_prometheus.middleware.PrometheusAfterMiddleware']
MIDDLEWARE.append("jobs.middlewares.CustomMiddleware")
5 changes: 5 additions & 0 deletions jobs/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.contrib.flatpages import views as flatpages_views
from django.contrib.sitemaps.views import sitemap
from django.urls import include, path
from django.conf.urls.static import static
from drf_yasg import openapi
from drf_yasg.views import get_schema_view
from rest_framework import permissions
Expand Down Expand Up @@ -61,3 +62,7 @@

if settings.ENABLE_PROMETHEUS:
urlpatterns.append(path("", include('django_prometheus.urls')))

if bool(settings.DEBUG):
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
11 changes: 1 addition & 10 deletions jobs/wsgi.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
"""
WSGI config for jobs project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application
Expand All @@ -15,4 +6,4 @@
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "jobs.settings")

application = get_wsgi_application()
application = WhiteNoise(application)
# application = WhiteNoise(application)

0 comments on commit 0eab365

Please sign in to comment.