Skip to content

Instantly share code, notes, and snippets.

@ajeetraina
Created November 25, 2024 17:28
Show Gist options
  • Save ajeetraina/14594f3d772bfc112ef8502632b63402 to your computer and use it in GitHub Desktop.
Save ajeetraina/14594f3d772bfc112ef8502632b63402 to your computer and use it in GitHub Desktop.
services:
db:
image: postgres:17
environment:
POSTGRES_DB: ${DATABASE_NAME}
POSTGRES_USER: ${DATABASE_USERNAME}
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
ports:
- "5432:5432"
env_file:
- .env
django-web:
build: .
container_name: django-docker
ports:
- "8000:8000"
depends_on:
- db
environment:
DJANGO_SECRET_KEY: ${DJANGO_SECRET_KEY}
DEBUG: ${DEBUG}
DJANGO_LOGLEVEL: ${DJANGO_LOGLEVEL}
DJANGO_ALLOWED_HOSTS: ${DJANGO_ALLOWED_HOSTS}
DATABASE_ENGINE: ${DATABASE_ENGINE}
DATABASE_NAME: ${DATABASE_NAME}
DATABASE_USERNAME: ${DATABASE_USERNAME}
DATABASE_PASSWORD: ${DATABASE_PASSWORD}
DATABASE_HOST: ${DATABASE_HOST}
DATABASE_PORT: ${DATABASE_PORT}
env_file:
- .env
@ajeetraina
Copy link
Author

   # 'DJANGO_ALLOWED_HOSTS' should be a single string of hosts with a , between each.
   # For example: 'DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1,[::1]'
   ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS","127.0.0.1").split(",")
  1. Set the SECRET_KEY to read from environment files.
    In settings.py, set SECRET_KEY to:

    # SECURITY WARNING: keep the secret key used in production secret!
    SECRET_KEY = os.environ.get("DJANGO_SECRET_KEY")
  2. *Set DEBUG to read from environment files.
    In settings.py, set DEBUG to:

    # SECURITY WARNING: don't run with debug turned on in production!
    DEBUG = bool(os.environ.get("DEBUG", default=0))

Step five: Build and run your new Django project

@ajeetraina
Copy link
Author

PostgreSQL:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.{}'.format(
            os.getenv('DATABASE_ENGINE', 'sqlite3')
        ),
        'NAME': os.getenv('DATABASE_NAME', 'polls'),
        'USER': os.getenv('DATABASE_USERNAME', 'myprojectuser'),
        'PASSWORD': os.getenv('DATABASE_PASSWORD', 'password'),
        'HOST': os.getenv('DATABASE_HOST', '127.0.0.1'),
        'PORT': os.getenv('DATABASE_PORT', 5432),
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment