Initial commit
Working example of empty django app with gunicorn, nginx, and postgersql. Source: https://blog.bitsacm.in/django-on-docker/
This commit is contained in:
commit
aaa86031e0
|
@ -0,0 +1,4 @@
|
||||||
|
project.env
|
||||||
|
venv
|
||||||
|
__pycache__
|
||||||
|
*.pyc
|
|
@ -0,0 +1,39 @@
|
||||||
|
# Use the official Python image from the Docker Hub
|
||||||
|
FROM python:3
|
||||||
|
|
||||||
|
# These two environment variables prevent __pycache__/ files.
|
||||||
|
ENV PYTHONUNBUFFERED 1
|
||||||
|
ENV PYTHONDONTWRITEBYTECODE 1
|
||||||
|
|
||||||
|
# We need to install netcat (used by entrypoint.sh)
|
||||||
|
# as it is not installed by default
|
||||||
|
RUN apt-get update && apt-get install -y netcat
|
||||||
|
|
||||||
|
# Create an app user in the app group.
|
||||||
|
RUN useradd --user-group --create-home --no-log-init --shell /bin/bash app
|
||||||
|
|
||||||
|
ENV APP_HOME=/home/app/web
|
||||||
|
|
||||||
|
# Create the staticfiles directory. This avoids permission errors.
|
||||||
|
RUN mkdir -p $APP_HOME/staticfiles
|
||||||
|
RUN chown app:app $APP_HOME/staticfiles
|
||||||
|
|
||||||
|
# Change the workdir.
|
||||||
|
WORKDIR $APP_HOME
|
||||||
|
|
||||||
|
# Copy the requirements.txt file.
|
||||||
|
COPY ./requirements.txt $APP_HOME
|
||||||
|
|
||||||
|
# Upgrade pip
|
||||||
|
RUN pip install --upgrade pip
|
||||||
|
|
||||||
|
# Install the requirements.
|
||||||
|
RUN pip install -r requirements.txt
|
||||||
|
|
||||||
|
# Copy the rest of the code.
|
||||||
|
COPY . $APP_HOME
|
||||||
|
|
||||||
|
USER app:app
|
||||||
|
|
||||||
|
ENTRYPOINT ["/home/app/web/entrypoint.sh"]
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
"""
|
||||||
|
ASGI config for djangoproject project.
|
||||||
|
|
||||||
|
It exposes the ASGI callable as a module-level variable named ``application``.
|
||||||
|
|
||||||
|
For more information on this file, see
|
||||||
|
https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
from django.core.asgi import get_asgi_application
|
||||||
|
|
||||||
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djangoproject.settings')
|
||||||
|
|
||||||
|
application = get_asgi_application()
|
|
@ -0,0 +1,13 @@
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
class Database:
|
||||||
|
NAME = os.getenv('POSTGRES_DB')
|
||||||
|
USER = os.getenv('POSTGRES_USER')
|
||||||
|
PASSWORD = os.getenv('POSTGRES_PASSWORD')
|
||||||
|
HOST = os.getenv('DATABASE_HOST')
|
||||||
|
PORT = os.getenv('DATABASE_PORT')
|
||||||
|
|
||||||
|
|
||||||
|
class Secrets:
|
||||||
|
SECRET_KEY = 'ladm3zbrp7y$c9h$-jz+(@0)d*zpppges+to67z-pg$wbzcqm='
|
|
@ -0,0 +1,128 @@
|
||||||
|
"""
|
||||||
|
Django settings for djangoproject project.
|
||||||
|
|
||||||
|
Generated by 'django-admin startproject' using Django 3.1.5.
|
||||||
|
|
||||||
|
For more information on this file, see
|
||||||
|
https://docs.djangoproject.com/en/3.1/topics/settings/
|
||||||
|
|
||||||
|
For the full list of settings and their values, see
|
||||||
|
https://docs.djangoproject.com/en/3.1/ref/settings/
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||||
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||||
|
|
||||||
|
|
||||||
|
from djangoproject.local_settings import Database, Secrets
|
||||||
|
|
||||||
|
|
||||||
|
# Quick-start development settings - unsuitable for production
|
||||||
|
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
|
||||||
|
|
||||||
|
# SECURITY WARNING: keep the secret key used in production secret!
|
||||||
|
SECRET_KEY = Secrets.SECRET_KEY
|
||||||
|
|
||||||
|
# SECURITY WARNING: don't run with debug turned on in production!
|
||||||
|
DEBUG = True
|
||||||
|
|
||||||
|
ALLOWED_HOSTS = ["localhost", "127.0.0.1", "0.0.0.0"]
|
||||||
|
|
||||||
|
# Application definition
|
||||||
|
|
||||||
|
INSTALLED_APPS = [
|
||||||
|
'django.contrib.admin',
|
||||||
|
'django.contrib.auth',
|
||||||
|
'django.contrib.contenttypes',
|
||||||
|
'django.contrib.sessions',
|
||||||
|
'django.contrib.messages',
|
||||||
|
'django.contrib.staticfiles',
|
||||||
|
]
|
||||||
|
|
||||||
|
MIDDLEWARE = [
|
||||||
|
'django.middleware.security.SecurityMiddleware',
|
||||||
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||||
|
'django.middleware.common.CommonMiddleware',
|
||||||
|
'django.middleware.csrf.CsrfViewMiddleware',
|
||||||
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||||
|
'django.contrib.messages.middleware.MessageMiddleware',
|
||||||
|
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||||
|
]
|
||||||
|
|
||||||
|
ROOT_URLCONF = 'djangoproject.urls'
|
||||||
|
|
||||||
|
TEMPLATES = [
|
||||||
|
{
|
||||||
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||||
|
'DIRS': [],
|
||||||
|
'APP_DIRS': True,
|
||||||
|
'OPTIONS': {
|
||||||
|
'context_processors': [
|
||||||
|
'django.template.context_processors.debug',
|
||||||
|
'django.template.context_processors.request',
|
||||||
|
'django.contrib.auth.context_processors.auth',
|
||||||
|
'django.contrib.messages.context_processors.messages',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
WSGI_APPLICATION = 'djangoproject.wsgi.application'
|
||||||
|
|
||||||
|
|
||||||
|
# Database
|
||||||
|
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
|
||||||
|
|
||||||
|
DATABASES = {
|
||||||
|
"default": {
|
||||||
|
"ENGINE": "django.db.backends.postgresql",
|
||||||
|
"NAME": Database.NAME,
|
||||||
|
"USER": Database.USER,
|
||||||
|
"PASSWORD": Database.PASSWORD,
|
||||||
|
"HOST": Database.HOST,
|
||||||
|
"PORT": Database.PORT,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Password validation
|
||||||
|
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
|
||||||
|
|
||||||
|
AUTH_PASSWORD_VALIDATORS = [
|
||||||
|
{
|
||||||
|
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
# Internationalization
|
||||||
|
# https://docs.djangoproject.com/en/3.1/topics/i18n/
|
||||||
|
|
||||||
|
LANGUAGE_CODE = 'en-us'
|
||||||
|
|
||||||
|
TIME_ZONE = 'UTC'
|
||||||
|
|
||||||
|
USE_I18N = True
|
||||||
|
|
||||||
|
USE_L10N = True
|
||||||
|
|
||||||
|
USE_TZ = True
|
||||||
|
|
||||||
|
|
||||||
|
# Static files (CSS, JavaScript, Images)
|
||||||
|
# https://docs.djangoproject.com/en/3.1/howto/static-files/
|
||||||
|
|
||||||
|
STATIC_URL = '/static/'
|
||||||
|
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
|
|
@ -0,0 +1,21 @@
|
||||||
|
"""djangoproject URL Configuration
|
||||||
|
|
||||||
|
The `urlpatterns` list routes URLs to views. For more information please see:
|
||||||
|
https://docs.djangoproject.com/en/3.1/topics/http/urls/
|
||||||
|
Examples:
|
||||||
|
Function views
|
||||||
|
1. Add an import: from my_app import views
|
||||||
|
2. Add a URL to urlpatterns: path('', views.home, name='home')
|
||||||
|
Class-based views
|
||||||
|
1. Add an import: from other_app.views import Home
|
||||||
|
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
|
||||||
|
Including another URLconf
|
||||||
|
1. Import the include() function: from django.urls import include, path
|
||||||
|
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||||
|
"""
|
||||||
|
from django.contrib import admin
|
||||||
|
from django.urls import path
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path('admin/', admin.site.urls),
|
||||||
|
]
|
|
@ -0,0 +1,16 @@
|
||||||
|
"""
|
||||||
|
WSGI config for djangoproject 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/3.1/howto/deployment/wsgi/
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
from django.core.wsgi import get_wsgi_application
|
||||||
|
|
||||||
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djangoproject.settings')
|
||||||
|
|
||||||
|
application = get_wsgi_application()
|
|
@ -0,0 +1,19 @@
|
||||||
|
#!/bin/sh
|
||||||
|
if [ "$DATABASE" = "postgres" ]; then
|
||||||
|
echo "Waiting for postgres..."
|
||||||
|
|
||||||
|
while ! nc -z $DATABASE_HOST $DATABASE_PORT; do
|
||||||
|
sleep 0.1
|
||||||
|
done
|
||||||
|
|
||||||
|
echo "PostgreSQL started"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Make migrations and migrate the database.
|
||||||
|
echo "Making migrations and migrating the database"
|
||||||
|
python manage.py makemigrations main --noinput
|
||||||
|
python manage.py migrate --noinput
|
||||||
|
echo "collectiong static files"
|
||||||
|
python manage.py collectstatic --noinput
|
||||||
|
|
||||||
|
exec "$@"
|
|
@ -0,0 +1,22 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
"""Django's command-line utility for administrative tasks."""
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""Run administrative tasks."""
|
||||||
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djangoproject.settings')
|
||||||
|
try:
|
||||||
|
from django.core.management import execute_from_command_line
|
||||||
|
except ImportError as exc:
|
||||||
|
raise ImportError(
|
||||||
|
"Couldn't import Django. Are you sure it's installed and "
|
||||||
|
"available on your PYTHONPATH environment variable? Did you "
|
||||||
|
"forget to activate a virtual environment?"
|
||||||
|
) from exc
|
||||||
|
execute_from_command_line(sys.argv)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
|
@ -0,0 +1,29 @@
|
||||||
|
appdirs==1.4.3
|
||||||
|
asgiref==3.3.1
|
||||||
|
CacheControl==0.12.6
|
||||||
|
certifi==2019.11.28
|
||||||
|
chardet==3.0.4
|
||||||
|
colorama==0.4.3
|
||||||
|
contextlib2==0.6.0
|
||||||
|
distlib==0.3.0
|
||||||
|
distro==1.4.0
|
||||||
|
Django==3.1.5
|
||||||
|
gunicorn==20.0.4
|
||||||
|
html5lib==1.0.1
|
||||||
|
idna==2.8
|
||||||
|
ipaddr==2.2.0
|
||||||
|
lockfile==0.12.2
|
||||||
|
msgpack==0.6.2
|
||||||
|
packaging==20.3
|
||||||
|
pep517==0.8.2
|
||||||
|
progress==1.5
|
||||||
|
psycopg2-binary==2.8.6
|
||||||
|
pyparsing==2.4.6
|
||||||
|
pytoml==0.1.21
|
||||||
|
pytz==2020.5
|
||||||
|
requests==2.22.0
|
||||||
|
retrying==1.3.3
|
||||||
|
six==1.14.0
|
||||||
|
sqlparse==0.4.1
|
||||||
|
urllib3==1.25.8
|
||||||
|
webencodings==0.5.1
|
|
@ -0,0 +1,41 @@
|
||||||
|
version: '3'
|
||||||
|
|
||||||
|
services:
|
||||||
|
db:
|
||||||
|
container_name: postgresdb
|
||||||
|
image: postgres:latest
|
||||||
|
restart: always
|
||||||
|
env_file:
|
||||||
|
- project.env
|
||||||
|
ports:
|
||||||
|
- 5432:5432
|
||||||
|
volumes:
|
||||||
|
- postgres-data:/var/lib/postgresql/data
|
||||||
|
web:
|
||||||
|
container_name: django
|
||||||
|
build: djangoproject/
|
||||||
|
command: >
|
||||||
|
gunicorn djangoproject.wsgi:application --bind 0.0.0.0:8000 --workers=4
|
||||||
|
env_file:
|
||||||
|
- project.env
|
||||||
|
expose:
|
||||||
|
- 8000
|
||||||
|
depends_on:
|
||||||
|
- db
|
||||||
|
volumes:
|
||||||
|
- staticfiles:/home/app/web/staticfiles
|
||||||
|
nginx:
|
||||||
|
container_name: nginx
|
||||||
|
image: nginx:mainline-alpine
|
||||||
|
restart: always
|
||||||
|
ports:
|
||||||
|
- 8888:80
|
||||||
|
volumes:
|
||||||
|
- ./nginx:/etc/nginx/conf.d
|
||||||
|
- staticfiles:/home/app/web/staticfiles
|
||||||
|
depends_on:
|
||||||
|
- web
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
postgres-data:
|
||||||
|
staticfiles:
|
|
@ -0,0 +1,19 @@
|
||||||
|
upstream djangoapp {
|
||||||
|
server django:8000;
|
||||||
|
}
|
||||||
|
|
||||||
|
server {
|
||||||
|
listen 80;
|
||||||
|
listen [::]:80;
|
||||||
|
|
||||||
|
location /static/ {
|
||||||
|
alias /home/app/web/staticfiles/;
|
||||||
|
}
|
||||||
|
|
||||||
|
location / {
|
||||||
|
proxy_pass http://djangoapp;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_redirect off;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
POSTGRES_USER=dbadmin
|
||||||
|
POSTGRES_PASSWORD=verysecretdbpassword
|
||||||
|
POSTGRES_DB=project_db
|
||||||
|
DATABASE=postgres
|
||||||
|
DATABASE_HOST=postgresdb
|
||||||
|
DATABASE_PORT=5432
|
Loading…
Reference in New Issue