├── app
├── app
│ ├── __init__.py
│ ├── wsgi.py
│ ├── urls.py
│ └── settings.py
├── hello_world
│ ├── __init__.py
│ ├── apps.py
│ ├── static
│ │ └── coconut_bars.jpg
│ ├── views.py
│ └── templates
│ │ └── index.html
└── manage.py
├── requirements.txt
├── scripts
└── entrypoint.sh
├── proxy
├── default.conf
├── Dockerfile
└── uwsgi_params
├── docker-compose.yml
├── docker-compose-deploy.yml
├── Dockerfile
├── README.md
├── LICENSE
└── .gitignore
/app/app/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/app/hello_world/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | Django>=3.0.6,<3.1
2 | uWSGI>=2.0.18,<2.1
3 |
--------------------------------------------------------------------------------
/app/hello_world/apps.py:
--------------------------------------------------------------------------------
1 | from django.apps import AppConfig
2 |
3 |
4 | class HelloWorldConfig(AppConfig):
5 | name = 'hello_world'
6 |
--------------------------------------------------------------------------------
/app/hello_world/static/coconut_bars.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LondonAppDeveloper/demo-django-docker-nginx-prod/HEAD/app/hello_world/static/coconut_bars.jpg
--------------------------------------------------------------------------------
/scripts/entrypoint.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | set -e
4 |
5 | python manage.py collectstatic --noinput
6 |
7 | uwsgi --socket :8000 --master --enable-threads --module app.wsgi
8 |
--------------------------------------------------------------------------------
/app/hello_world/views.py:
--------------------------------------------------------------------------------
1 | from django.shortcuts import render
2 |
3 |
4 | def index(request):
5 | """Placeholder index view"""
6 | return render(request, 'index.html')
7 |
--------------------------------------------------------------------------------
/proxy/default.conf:
--------------------------------------------------------------------------------
1 | server {
2 | listen 8080;
3 |
4 | location /static {
5 | alias /vol/static;
6 | }
7 |
8 | location / {
9 | uwsgi_pass app:8000;
10 | include /etc/nginx/uwsgi_params;
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/app/hello_world/templates/index.html:
--------------------------------------------------------------------------------
1 | {% load static %}
2 |
3 |
4 | Hello World Django App
5 |
6 | Some delicious coconut bars
7 |
8 |
9 |
--------------------------------------------------------------------------------
/proxy/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM nginxinc/nginx-unprivileged:1-alpine
2 |
3 | COPY ./default.conf /etc/nginx/conf.d/default.conf
4 | COPY ./uwsgi_params /etc/nginx/uwsgi_params
5 |
6 | USER root
7 |
8 | RUN mkdir -p /vol/static
9 | RUN chmod 755 /vol/static
10 |
11 | USER nginx
12 |
--------------------------------------------------------------------------------
/docker-compose.yml:
--------------------------------------------------------------------------------
1 | version: '3.7'
2 |
3 | services:
4 | app:
5 | build:
6 | context: .
7 | ports:
8 | - "8000:8000"
9 | volumes:
10 | - ./app:/app
11 | command: sh -c "python manage.py runserver 0.0.0.0:8000"
12 | environment:
13 | - DEBUG=1
14 |
--------------------------------------------------------------------------------
/app/app/wsgi.py:
--------------------------------------------------------------------------------
1 | """
2 | WSGI config for app project.
3 |
4 | It exposes the WSGI callable as a module-level variable named ``application``.
5 |
6 | For more information on this file, see
7 | https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/
8 | """
9 |
10 | import os
11 |
12 | from django.core.wsgi import get_wsgi_application
13 |
14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
15 |
16 | application = get_wsgi_application()
17 |
--------------------------------------------------------------------------------
/docker-compose-deploy.yml:
--------------------------------------------------------------------------------
1 | version: '3.7'
2 |
3 | services:
4 | app:
5 | build:
6 | context: .
7 | volumes:
8 | - static_data:/vol/web
9 | environment:
10 | - SECRET_KEY=samplesecret123
11 | - ALLOWED_HOSTS=127.0.0.1,localhost
12 |
13 | proxy:
14 | build:
15 | context: ./proxy
16 | volumes:
17 | - static_data:/vol/static
18 | ports:
19 | - "8080:8080"
20 | depends_on:
21 | - app
22 |
23 | volumes:
24 | static_data:
25 |
--------------------------------------------------------------------------------
/proxy/uwsgi_params:
--------------------------------------------------------------------------------
1 | uwsgi_param QUERY_STRING $query_string;
2 | uwsgi_param REQUEST_METHOD $request_method;
3 | uwsgi_param CONTENT_TYPE $content_type;
4 | uwsgi_param CONTENT_LENGTH $content_length;
5 | uwsgi_param REQUEST_URI $request_uri;
6 | uwsgi_param PATH_INFO $document_uri;
7 | uwsgi_param DOCUMENT_ROOT $document_root;
8 | uwsgi_param SERVER_PROTOCOL $server_protocol;
9 | uwsgi_param REMOTE_ADDR $remote_addr;
10 | uwsgi_param REMOTE_PORT $remote_port;
11 | uwsgi_param SERVER_ADDR $server_addr;
12 | uwsgi_param SERVER_PORT $server_port;
13 | uwsgi_param SERVER_NAME $server_name;
14 |
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM python:3.8-alpine
2 |
3 | ENV PATH="/scripts:${PATH}"
4 |
5 | COPY ./requirements.txt /requirements.txt
6 | RUN apk add --update --no-cache --virtual .tmp gcc libc-dev linux-headers
7 | RUN pip install -r /requirements.txt
8 | RUN apk del .tmp
9 |
10 | RUN mkdir /app
11 | COPY ./app /app
12 | WORKDIR /app
13 | COPY ./scripts /scripts
14 |
15 | RUN chmod +x /scripts/*
16 |
17 | RUN mkdir -p /vol/web/media
18 | RUN mkdir -p /vol/web/static
19 | RUN adduser -D user
20 | RUN chown -R user:user /vol
21 | RUN chmod -R 755 /vol/web
22 | USER user
23 |
24 | CMD ["entrypoint.sh"]
25 |
--------------------------------------------------------------------------------
/app/manage.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | """Django's command-line utility for administrative tasks."""
3 | import os
4 | import sys
5 |
6 |
7 | def main():
8 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
9 | try:
10 | from django.core.management import execute_from_command_line
11 | except ImportError as exc:
12 | raise ImportError(
13 | "Couldn't import Django. Are you sure it's installed and "
14 | "available on your PYTHONPATH environment variable? Did you "
15 | "forget to activate a virtual environment?"
16 | ) from exc
17 | execute_from_command_line(sys.argv)
18 |
19 |
20 | if __name__ == '__main__':
21 | main()
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
6 |
7 |
14 |
15 |
16 |
17 | # Demo Django Docker NGINX Production Deployment
18 |
19 | Demo of a Django deployment setup using Docker.
20 |
--------------------------------------------------------------------------------
/app/app/urls.py:
--------------------------------------------------------------------------------
1 | """app URL Configuration
2 |
3 | The `urlpatterns` list routes URLs to views. For more information please see:
4 | https://docs.djangoproject.com/en/2.2/topics/http/urls/
5 | Examples:
6 | Function views
7 | 1. Add an import: from my_app import views
8 | 2. Add a URL to urlpatterns: path('', views.home, name='home')
9 | Class-based views
10 | 1. Add an import: from other_app.views import Home
11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
12 | Including another URLconf
13 | 1. Import the include() function: from django.urls import include, path
14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
15 | """
16 | from django.contrib import admin
17 | from django.urls import path
18 |
19 | from hello_world import views
20 |
21 | urlpatterns = [
22 | path('admin/', admin.site.urls),
23 | path('', views.index)
24 | ]
25 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 LondonAppDeveloper
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | .eggs/
17 | lib/
18 | lib64/
19 | parts/
20 | sdist/
21 | var/
22 | wheels/
23 | pip-wheel-metadata/
24 | share/python-wheels/
25 | *.egg-info/
26 | .installed.cfg
27 | *.egg
28 | MANIFEST
29 |
30 | # PyInstaller
31 | # Usually these files are written by a python script from a template
32 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
33 | *.manifest
34 | *.spec
35 |
36 | # Installer logs
37 | pip-log.txt
38 | pip-delete-this-directory.txt
39 |
40 | # Unit test / coverage reports
41 | htmlcov/
42 | .tox/
43 | .nox/
44 | .coverage
45 | .coverage.*
46 | .cache
47 | nosetests.xml
48 | coverage.xml
49 | *.cover
50 | *.py,cover
51 | .hypothesis/
52 | .pytest_cache/
53 |
54 | # Translations
55 | *.mo
56 | *.pot
57 |
58 | # Django stuff:
59 | *.log
60 | local_settings.py
61 | db.sqlite3
62 | db.sqlite3-journal
63 |
64 | # Flask stuff:
65 | instance/
66 | .webassets-cache
67 |
68 | # Scrapy stuff:
69 | .scrapy
70 |
71 | # Sphinx documentation
72 | docs/_build/
73 |
74 | # PyBuilder
75 | target/
76 |
77 | # Jupyter Notebook
78 | .ipynb_checkpoints
79 |
80 | # IPython
81 | profile_default/
82 | ipython_config.py
83 |
84 | # pyenv
85 | .python-version
86 |
87 | # pipenv
88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies
90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not
91 | # install all needed dependencies.
92 | #Pipfile.lock
93 |
94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow
95 | __pypackages__/
96 |
97 | # Celery stuff
98 | celerybeat-schedule
99 | celerybeat.pid
100 |
101 | # SageMath parsed files
102 | *.sage.py
103 |
104 | # Environments
105 | .env
106 | .venv
107 | env/
108 | venv/
109 | ENV/
110 | env.bak/
111 | venv.bak/
112 |
113 | # Spyder project settings
114 | .spyderproject
115 | .spyproject
116 |
117 | # Rope project settings
118 | .ropeproject
119 |
120 | # mkdocs documentation
121 | /site
122 |
123 | # mypy
124 | .mypy_cache/
125 | .dmypy.json
126 | dmypy.json
127 |
128 | # Pyre type checker
129 | .pyre/
130 |
--------------------------------------------------------------------------------
/app/app/settings.py:
--------------------------------------------------------------------------------
1 | """
2 | Django settings for app project.
3 |
4 | Generated by 'django-admin startproject' using Django 2.2.
5 |
6 | For more information on this file, see
7 | https://docs.djangoproject.com/en/2.2/topics/settings/
8 |
9 | For the full list of settings and their values, see
10 | https://docs.djangoproject.com/en/2.2/ref/settings/
11 | """
12 |
13 | import os
14 |
15 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17 |
18 |
19 | # Quick-start development settings - unsuitable for production
20 | # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
21 |
22 | # SECURITY WARNING: keep the secret key used in production secret!
23 | SECRET_KEY = os.environ.get('SECRET_KEY', 'changeme')
24 |
25 | # SECURITY WARNING: don't run with debug turned on in production!
26 | DEBUG = bool(int(os.environ.get('DEBUG', 0)))
27 |
28 | ALLOWED_HOSTS = []
29 | ALLOWED_HOSTS_ENV = os.environ.get('ALLOWED_HOSTS')
30 | if ALLOWED_HOSTS_ENV:
31 | ALLOWED_HOSTS.extend(ALLOWED_HOSTS_ENV.split(','))
32 |
33 | # Application definition
34 |
35 | INSTALLED_APPS = [
36 | 'django.contrib.admin',
37 | 'django.contrib.auth',
38 | 'django.contrib.contenttypes',
39 | 'django.contrib.sessions',
40 | 'django.contrib.messages',
41 | 'django.contrib.staticfiles',
42 | 'hello_world',
43 | ]
44 |
45 | MIDDLEWARE = [
46 | 'django.middleware.security.SecurityMiddleware',
47 | 'django.contrib.sessions.middleware.SessionMiddleware',
48 | 'django.middleware.common.CommonMiddleware',
49 | 'django.middleware.csrf.CsrfViewMiddleware',
50 | 'django.contrib.auth.middleware.AuthenticationMiddleware',
51 | 'django.contrib.messages.middleware.MessageMiddleware',
52 | 'django.middleware.clickjacking.XFrameOptionsMiddleware',
53 | ]
54 |
55 | ROOT_URLCONF = 'app.urls'
56 |
57 | TEMPLATES = [
58 | {
59 | 'BACKEND': 'django.template.backends.django.DjangoTemplates',
60 | 'DIRS': [],
61 | 'APP_DIRS': True,
62 | 'OPTIONS': {
63 | 'context_processors': [
64 | 'django.template.context_processors.debug',
65 | 'django.template.context_processors.request',
66 | 'django.contrib.auth.context_processors.auth',
67 | 'django.contrib.messages.context_processors.messages',
68 | ],
69 | },
70 | },
71 | ]
72 |
73 | WSGI_APPLICATION = 'app.wsgi.application'
74 |
75 |
76 | # Database
77 | # https://docs.djangoproject.com/en/2.2/ref/settings/#databases
78 |
79 | DATABASES = {
80 | 'default': {
81 | 'ENGINE': 'django.db.backends.sqlite3',
82 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
83 | }
84 | }
85 |
86 |
87 | # Password validation
88 | # https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
89 |
90 | AUTH_PASSWORD_VALIDATORS = [
91 | {
92 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
93 | },
94 | {
95 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
96 | },
97 | {
98 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
99 | },
100 | {
101 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
102 | },
103 | ]
104 |
105 |
106 | # Internationalization
107 | # https://docs.djangoproject.com/en/2.2/topics/i18n/
108 |
109 | LANGUAGE_CODE = 'en-us'
110 |
111 | TIME_ZONE = 'UTC'
112 |
113 | USE_I18N = True
114 |
115 | USE_L10N = True
116 |
117 | USE_TZ = True
118 |
119 |
120 | # Static files (CSS, JavaScript, Images)
121 | # https://docs.djangoproject.com/en/2.2/howto/static-files/
122 |
123 | STATIC_URL = '/static/static/'
124 | MEDIA_URL = '/static/media/'
125 |
126 | STATIC_ROOT = '/vol/web/static'
127 | MEDIA_ROOT = '/vol/web/media'
128 |
--------------------------------------------------------------------------------