Created
November 25, 2024 17:28
-
-
Save ajeetraina/14594f3d772bfc112ef8502632b63402 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
# '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(",")
-
Set the
SECRET_KEY
to read from environment files.
Insettings.py
, setSECRET_KEY
to:# SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = os.environ.get("DJANGO_SECRET_KEY")
-
*Set
DEBUG
to read from environment files.
Insettings.py
, setDEBUG
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
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
DJANGO_SECRET_KEY=your_secret_key
DEBUG=True
DJANGO_LOGLEVEL=info
DJANGO_ALLOWED_HOSTS=localhost
DATABASE_ENGINE=postgresql_psycopg2
DATABASE_NAME=dockerdjango
DATABASE_USERNAME=dbuser
DATABASE_PASSWORD=dbpassword
DATABASE_HOST=db
DATABASE_PORT=5432