├── .env.dev ├── Dockerfile ├── README.md ├── app ├── app │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-37.pyc │ │ ├── celery.cpython-37.pyc │ │ ├── settings.cpython-37.pyc │ │ ├── urls.cpython-37.pyc │ │ └── wsgi.cpython-37.pyc │ ├── asgi.py │ ├── celery.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── core │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-37.pyc │ │ ├── admin.cpython-37.pyc │ │ └── models.cpython-37.pyc │ ├── admin.py │ ├── apps.py │ ├── management │ │ ├── __init__.py │ │ ├── __pycache__ │ │ │ └── __init__.cpython-37.pyc │ │ └── commands │ │ │ ├── __init__.py │ │ │ ├── __pycache__ │ │ │ ├── __init__.cpython-37.pyc │ │ │ └── wait_for_db.cpython-37.pyc │ │ │ └── wait_for_db.py │ ├── migrations │ │ ├── __init__.py │ │ └── __pycache__ │ │ │ └── __init__.cpython-37.pyc │ ├── models.py │ ├── tests.py │ └── views.py └── manage.py ├── docker-compose.yml └── requirements.txt /.env.dev: -------------------------------------------------------------------------------- 1 | DB_HOST=db 2 | DB_NAME=app 3 | DB_USER=postgres 4 | DB_PASS=supersecretpassword 5 | POSTGRES_DB=app 6 | POSTGRES_USER=postgres 7 | POSTGRES_PASSWORD=supersecretpassword -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.7-alpine 2 | 3 | ENV PYTHONUNBUFFERED 1 4 | COPY ./requirements.txt /requirements.txt 5 | RUN apk add --update --no-cache postgresql-client jpeg-dev 6 | RUN apk add --update --no-cache --virtual .tmp-build-deps \ 7 | gcc libc-dev linux-headers postgresql-dev musl-dev zlib zlib-dev 8 | RUN pip install -r /requirements.txt 9 | RUN apk del .tmp-build-deps 10 | 11 | RUN mkdir /app 12 | COPY ./app /app 13 | WORKDIR /app 14 | 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # docker-django-redis-celery 2 | Initial configuration of Docker Django Redis Celery 3 | 4 | ## Getting Started 5 | This project works on Python 3+ and Django 2+. 6 | Simply, run the following command: 7 | ``` 8 | docker-compose up --build 9 | ``` 10 | 11 | Follow me on IG for more: https://www.instagram.com/coderasha 12 | -------------------------------------------------------------------------------- /app/app/__init__.py: -------------------------------------------------------------------------------- 1 | from .celery import app as celery_app 2 | 3 | __all__ = ['celery_app'] -------------------------------------------------------------------------------- /app/app/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thepylot/docker-django-redis-celery/8be483c051daff7eee87ddf99bfece88e3a7f639/app/app/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /app/app/__pycache__/celery.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thepylot/docker-django-redis-celery/8be483c051daff7eee87ddf99bfece88e3a7f639/app/app/__pycache__/celery.cpython-37.pyc -------------------------------------------------------------------------------- /app/app/__pycache__/settings.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thepylot/docker-django-redis-celery/8be483c051daff7eee87ddf99bfece88e3a7f639/app/app/__pycache__/settings.cpython-37.pyc -------------------------------------------------------------------------------- /app/app/__pycache__/urls.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thepylot/docker-django-redis-celery/8be483c051daff7eee87ddf99bfece88e3a7f639/app/app/__pycache__/urls.cpython-37.pyc -------------------------------------------------------------------------------- /app/app/__pycache__/wsgi.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thepylot/docker-django-redis-celery/8be483c051daff7eee87ddf99bfece88e3a7f639/app/app/__pycache__/wsgi.cpython-37.pyc -------------------------------------------------------------------------------- /app/app/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for app project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /app/app/celery.py: -------------------------------------------------------------------------------- 1 | import os 2 | from celery import Celery 3 | 4 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings') 5 | 6 | app = Celery('app') 7 | app.config_from_object('django.conf:settings', namespace='CELERY') 8 | app.autodiscover_tasks() -------------------------------------------------------------------------------- /app/app/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for app project. 3 | 4 | Generated by 'django-admin startproject' using Django 3.1.4. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/3.1/ref/settings/ 11 | """ 12 | 13 | from pathlib import Path 14 | import os 15 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 16 | BASE_DIR = Path(__file__).resolve().parent.parent 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = 'l^)6&tgav8q$#g^aj7au^b=^*l98%c#9_v^85gu!%-s$bq7%(2' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'core', 41 | 42 | ] 43 | 44 | MIDDLEWARE = [ 45 | 'django.middleware.security.SecurityMiddleware', 46 | 'django.contrib.sessions.middleware.SessionMiddleware', 47 | 'django.middleware.common.CommonMiddleware', 48 | 'django.middleware.csrf.CsrfViewMiddleware', 49 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 50 | 'django.contrib.messages.middleware.MessageMiddleware', 51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 52 | ] 53 | 54 | ROOT_URLCONF = 'app.urls' 55 | 56 | TEMPLATES = [ 57 | { 58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 59 | 'DIRS': [], 60 | 'APP_DIRS': True, 61 | 'OPTIONS': { 62 | 'context_processors': [ 63 | 'django.template.context_processors.debug', 64 | 'django.template.context_processors.request', 65 | 'django.contrib.auth.context_processors.auth', 66 | 'django.contrib.messages.context_processors.messages', 67 | ], 68 | }, 69 | }, 70 | ] 71 | 72 | WSGI_APPLICATION = 'app.wsgi.application' 73 | 74 | 75 | # Database 76 | # https://docs.djangoproject.com/en/3.1/ref/settings/#databases 77 | 78 | DATABASES = { 79 | 'default': { 80 | 'ENGINE': 'django.db.backends.postgresql', 81 | 'HOST': os.environ.get('DB_HOST'), 82 | 'NAME': os.environ.get('DB_NAME'), 83 | 'USER': os.environ.get('DB_USER'), 84 | 'PASSWORD': os.environ.get('DB_PASS'), 85 | } 86 | } 87 | 88 | 89 | # Password validation 90 | # https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators 91 | 92 | AUTH_PASSWORD_VALIDATORS = [ 93 | { 94 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 95 | }, 96 | { 97 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 98 | }, 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 101 | }, 102 | { 103 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 104 | }, 105 | ] 106 | 107 | 108 | # Internationalization 109 | # https://docs.djangoproject.com/en/3.1/topics/i18n/ 110 | 111 | LANGUAGE_CODE = 'en-us' 112 | 113 | TIME_ZONE = 'UTC' 114 | 115 | USE_I18N = True 116 | 117 | USE_L10N = True 118 | 119 | USE_TZ = True 120 | 121 | 122 | # Static files (CSS, JavaScript, Images) 123 | # https://docs.djangoproject.com/en/3.1/howto/static-files/ 124 | 125 | STATIC_URL = '/static/' 126 | 127 | # Redis and Celery Conf 128 | CELERY_BROKER_URL = "redis://redis:6379" 129 | CELERY_RESULT_BACKEND = "redis://redis:6379" -------------------------------------------------------------------------------- /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/3.1/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 | urlpatterns = [ 20 | path('admin/', admin.site.urls), 21 | ] 22 | -------------------------------------------------------------------------------- /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/3.1/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 | -------------------------------------------------------------------------------- /app/core/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thepylot/docker-django-redis-celery/8be483c051daff7eee87ddf99bfece88e3a7f639/app/core/__init__.py -------------------------------------------------------------------------------- /app/core/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thepylot/docker-django-redis-celery/8be483c051daff7eee87ddf99bfece88e3a7f639/app/core/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /app/core/__pycache__/admin.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thepylot/docker-django-redis-celery/8be483c051daff7eee87ddf99bfece88e3a7f639/app/core/__pycache__/admin.cpython-37.pyc -------------------------------------------------------------------------------- /app/core/__pycache__/models.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thepylot/docker-django-redis-celery/8be483c051daff7eee87ddf99bfece88e3a7f639/app/core/__pycache__/models.cpython-37.pyc -------------------------------------------------------------------------------- /app/core/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | -------------------------------------------------------------------------------- /app/core/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class CoreConfig(AppConfig): 5 | name = 'core' 6 | -------------------------------------------------------------------------------- /app/core/management/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thepylot/docker-django-redis-celery/8be483c051daff7eee87ddf99bfece88e3a7f639/app/core/management/__init__.py -------------------------------------------------------------------------------- /app/core/management/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thepylot/docker-django-redis-celery/8be483c051daff7eee87ddf99bfece88e3a7f639/app/core/management/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /app/core/management/commands/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thepylot/docker-django-redis-celery/8be483c051daff7eee87ddf99bfece88e3a7f639/app/core/management/commands/__init__.py -------------------------------------------------------------------------------- /app/core/management/commands/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thepylot/docker-django-redis-celery/8be483c051daff7eee87ddf99bfece88e3a7f639/app/core/management/commands/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /app/core/management/commands/__pycache__/wait_for_db.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thepylot/docker-django-redis-celery/8be483c051daff7eee87ddf99bfece88e3a7f639/app/core/management/commands/__pycache__/wait_for_db.cpython-37.pyc -------------------------------------------------------------------------------- /app/core/management/commands/wait_for_db.py: -------------------------------------------------------------------------------- 1 | import time 2 | from django.db import connections 3 | from django.db.utils import OperationalError 4 | from django.core.management import BaseCommand 5 | 6 | class Command(BaseCommand): 7 | """Django command to pause execution until db is available""" 8 | 9 | def handle(self, *args, **options): 10 | self.stdout.write('Waiting for database...') 11 | db_conn = None 12 | while not db_conn: 13 | try: 14 | db_conn = connections['default'] 15 | except OperationalError: 16 | self.stdout.write('Database unavailable, waititng 1 second...') 17 | time.sleep(1) 18 | 19 | self.stdout.write(self.style.SUCCESS('Database available!')) 20 | -------------------------------------------------------------------------------- /app/core/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thepylot/docker-django-redis-celery/8be483c051daff7eee87ddf99bfece88e3a7f639/app/core/migrations/__init__.py -------------------------------------------------------------------------------- /app/core/migrations/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thepylot/docker-django-redis-celery/8be483c051daff7eee87ddf99bfece88e3a7f639/app/core/migrations/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /app/core/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | -------------------------------------------------------------------------------- /app/core/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /app/core/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render 2 | 3 | # Create your views here. 4 | -------------------------------------------------------------------------------- /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 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | 3 | services: 4 | app: 5 | build: 6 | context: . 7 | ports: 8 | - "8000:8000" 9 | volumes: 10 | - ./app:/app 11 | command: > 12 | sh -c "python3 manage.py migrate && 13 | python3 manage.py wait_for_db && 14 | python3 manage.py runserver 0.0.0.0:8000" 15 | 16 | env_file: 17 | - ./.env.dev 18 | depends_on: 19 | - db 20 | 21 | db: 22 | image: postgres:10-alpine 23 | env_file: 24 | - ./.env.dev 25 | volumes: 26 | - pgdata:/var/lib/postgresql/data 27 | redis: 28 | image: redis:alpine 29 | celery: 30 | restart: always 31 | build: 32 | context: . 33 | command: celery -A app worker -l info 34 | volumes: 35 | - ./app:/app 36 | env_file: 37 | - ./.env.dev 38 | depends_on: 39 | - db 40 | - redis 41 | - app 42 | volumes: 43 | pgdata: -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Django>=2.2 2 | celery==4.4.1 3 | redis==3.4.1 4 | psycopg2>=2.7.5,<2.8.0 --------------------------------------------------------------------------------