{"id":15641624,"url":"https://github.com/masterkale/docker-django","last_synced_at":"2025-10-07T07:37:33.449Z","repository":{"id":10492615,"uuid":"65938183","full_name":"MasterKale/Docker-Django","owner":"MasterKale","description":"A complete Django setup, running in Docker","archived":false,"fork":false,"pushed_at":"2023-06-19T13:27:20.000Z","size":70,"stargazers_count":74,"open_issues_count":3,"forks_count":27,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-03-29T05:23:34.321Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MasterKale.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-08-17T20:05:29.000Z","updated_at":"2024-12-30T21:22:23.000Z","dependencies_parsed_at":"2024-10-22T18:39:06.443Z","dependency_job_id":null,"html_url":"https://github.com/MasterKale/Docker-Django","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MasterKale%2FDocker-Django","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MasterKale%2FDocker-Django/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MasterKale%2FDocker-Django/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MasterKale%2FDocker-Django/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MasterKale","download_url":"https://codeload.github.com/MasterKale/Docker-Django/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249250973,"owners_count":21237965,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-03T11:43:54.001Z","updated_at":"2025-10-07T07:37:28.419Z","avatar_url":"https://github.com/MasterKale.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Docker'd Django\n### Django, Postgres, and Redis, all in Docker\n\nThis is a boilerplate repo intended for quickly starting a new **Django** project with **PostgreSQL** and **Redis** support, all running within Docker containers.\n\nMultiple production hosting options are also included. See the **Production Hosting** section below for more information.\n\n- [Prerequisites](#prerequisites)\n- [Getting started](#getting-started)\n- [Components](#components)\n- [Production Hosting](#production-hosting)\n- [Troubleshooting](#troubleshooting)\n\n## Prerequisites\n\n- Docker\n- Pipenv\n  - Make sure Python3 is available\n  - Enables `pipenv install` to set up libraries locally for the editor to crawl. The Django container also uses Pipenv to install dependencies to encourage use of this new Python package management tool.\n\n## Getting started\n\n1. Clone this repo\n2. Delete the **.git** folder\n    - `rm -rf .git/`\n3. Create a new git repo\n    - `git init`\n    - `git add .`\n    - `git commit -m \"Initial Commit\"`\n4. Install Python dependencies in a Python3 virtual environment\n    - `pipenv install --three`\n5. Create a new Django project\n    - `pipenv run django-admin startproject appname _app/`\n6. Make the following changes to your Django project's **settings.py**:\n\n```py\n# appname/settings.py\nimport os\n\n# SECURITY WARNING: keep the secret key used in production secret!\nSECRET_KEY = os.getenv('DJANGO_SECRET_KEY')\n\n# SECURITY WARNING: don't run with debug turned on in production!\nDEBUG = os.getenv('DEBUG', False) == 'true'\n\n# Enable traffic and form submissions from localhost and PROD_HOST_NAME\nALLOWED_HOSTS = ['localhost']\nCSRF_TRUSTED_ORIGINS = ['http://localhost']\n\nPROD_HOST_NAME = os.getenv('PROD_HOST_NAME', None)\nif PROD_HOST_NAME:\n    ALLOWED_HOSTS.append(PROD_HOST_NAME)\n    CSRF_TRUSTED_ORIGINS.append(f'https://{PROD_HOST_NAME}')\n\n# Configure whitenoise for static file hosting\nINSTALLED_APPS = [\n    # ...\n    # See http://whitenoise.evans.io/en/latest/django.html#using-whitenoise-in-development\n    \"whitenoise.runserver_nostatic\",\n    \"django.contrib.staticfiles\",\n    # ...\n]\n\nMIDDLEWARE = [\n    # ...\n    \"django.middleware.security.SecurityMiddleware\",\n    # See http://whitenoise.evans.io/en/latest/django.html#enable-whitenoise\n    \"whitenoise.middleware.WhiteNoiseMiddleware\",\n    # ...\n]\n\n# Point Django to Docker-hosted Postgres\nDATABASES = {\n    'default': {\n        'ENGINE': 'django.db.backends.postgresql_psycopg2',\n        'NAME': os.getenv('POSTGRES_USER'),\n        'USER': os.getenv('POSTGRES_USER'),\n        'PASSWORD': os.getenv('POSTGRES_PASSWORD'),\n        'HOST': 'db',\n        'PORT': 5432,\n    }\n}\n\n# Set up static files\nSTATIC_ROOT = 'static'\n# See http://whitenoise.evans.io/en/latest/django.html#enable-whitenoise\nSTATICFILES_STORAGE = \"whitenoise.storage.CompressedManifestStaticFilesStorage\"\n\n# Redis cache support\n# https://docs.djangoproject.com/en/4.0/topics/cache/#redis-1\n\nCACHES = {\n    'default': {\n        'BACKEND': 'django.core.cache.backends.redis.RedisCache',\n        'LOCATION': 'redis://redis:6379/1',\n    }\n}\nSESSION_ENGINE = 'django.contrib.sessions.backends.cache'\nSESSION_CACHE_ALIAS = 'default'\n```\n\n7. Update the **.env** file to specify values for the environment variables defined within\n8. Do a global search of all files here for \"appname\" and replace it with the actual name of your app\n9. Start Django for development at http://localhost:8000\n    - `docker compose up`\n\n## Components\n\n### Dockerfile\n\nBuilds the Django container. The container is built from a standard **python** Docker image and will run Django's `colletstatic` when being built.\n\n### docker-compose.yml + docker-compose.dev.yml\n\nUse `./start-dev.sh` to start Django for development. This will spin up three containers: the above container for **Django**, one for **PostgreSQL**, and one for **Redis**.\n\nDjango can be accessed in DEBUG mode directly from http://localhost:8000 during development. The Gunicorn workers are set to reload when file changes are detected.\n\nPostgres can also be directly accessed at `localhost:5432` using the credentials you specified in the **.env** file.\n\n### docker-compose.yml + docker-compose.prod.yml\n\nSee the **Production Hosting** section below for more information.\n\nUse `./start-dev.sh` to start Django for production. You can also run `./update-prod-django.sh` whenever you need to deploy a new build.\n\n### Pipfile/Pipfile.lock\n\nIncludes Python packages needed to make Django, Postgre, and Redis work together.\n\n### .env\n\nContains environment variables for the containers. Several variables are included for configuring Postgres and Django secrets.\n\n### .dockerignore\n\nDefines files that Docker should _never_ include when building the Django image.\n\n### .editorconfig\n\nDefines some common settings to help ensure consistency of styling across files.\n\n### .flake8\n\nConfigures the **flake8** Python linter. Includes a few common settings to my personal preferences.\n\n### .vscode/settings.json\n\nHelps configure the Python plugin to lint with flake8. A placeholder Python interpreter setting is left in to simplify pointing to the local virtual environment created with Pipenv.\n\n### _app/gunicorn.cfg.py\n\nDefines settings for gunicorn, including a port binding, workers, and a gunicorn-specific error log.\n\n### _caddy/Caddyfile\n\nEstablishes a reverse-proxy to Django, and serves Django static files using [Caddy](https://caddyserver.com/v2). See **Production Hosting** below for more info.\n\n## Production Hosting\n\nThis project includes two options for handling production hosting, including reverse-proxying Django and handling SSL:\n\n1. Use [Caddy](https://caddyserver.com/v2)\n2. Use [Cloudflare Tunnel](https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/)\n\n**You'll only need one of these!** Which ever option you choose below, delete the other commented-out service in **docker-compose.prod.yml**.\n\n### Option 1: Use Caddy\n\nFollow these steps:\n\n1. Uncomment the `caddy` service in **docker-compose.prod.yml**\n2. Uncomment all `volumes` in **docker-compose.prod.yml**\n3. Uncomment the `static_files_volume:...` entry in `django` service's `volumes` property in **docker-compose.prod.yml**\n4. Configure your server's firewall to expose TCP for ports 80 and 443, and UDP for port 443. These will allow Caddy to host the site, and generate and periodically update SSL certificates for the site via Let's Encrypt.\n\nWhen these steps are complete, running **start-prod.sh** should make Django available on the public internet at `https://$PROD_HOST_NAME`.\n\n### Option 2: Use Cloudflare Tunnel\n\nUncomment the `cloudflaretunnel` service in **docker-compose.prod.yml** and then follow these steps:\n\n1. Log into the [Cloudflare Zero Trust dashboard](https://dash.teams.cloudflare.com/)\n2. Click **Access \u003e Tunnels**\n3. Click **Create a tunnel**\n4. Specify a **Tunnel name**\n5. Click **Docker** on the **Install connector** step\n6. Save the value of the `--token` flag in the page's `docker` command to this project's **.env** file as the `CLOUDFLARE_TUNNEL_TOKEN` environment variable\n7. Run **start-prod.sh** to start the tunnel and display an entry under **Connectors**\n8. Click **Next**\n9. Set up a **Public hostname**\n10. For the **Service** select \"**HTTP**\" and then enter \"**django:8000**\"\n11. Click **Save \u0026lt;tunnel name\u0026gt; tunnel** to complete setup\n12. Set the `PROD_HOST_NAME` variable in the **.env** file to the tunnel's configured **Public hostname**\n\nWhen these steps are complete, running **start-prod.sh** should make Django available on the public internet at `https://$PROD_HOST_NAME`. No firewall ports need to be opened on the production host, and in fact you may wish to set up the firewall to block all incoming traffic for good measure.\n\n## Troubleshooting\n\n### The Django container reports \"exited with code 3\"\n\nYou probably forgot to replace the string \"appname\" with the actual name you passed to `django-admin startproject`. Check **_app/gunicorn_appname.log** to see if Gunicorn is erroring out with something like this:\n\n```\nModuleNotFoundError: No module named 'appname'\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmasterkale%2Fdocker-django","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmasterkale%2Fdocker-django","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmasterkale%2Fdocker-django/lists"}