/django_postgresql_template

Dockerized django example with postgresql.

Primary LanguageDockerfileApache License 2.0Apache-2.0

django_postgresql docker template

Dockerized django backend example with postgresql.

Project setup

  1. Create the Django project by running the docker compose run command as follows.
sudo docker compose run web django-admin startproject composeexample .
  1. Connect db
    • Edit composeexample/settings.py file.
    • Replace DATABASES = ...
# settings.py
import os
[...]
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.environ.get('POSTGRES_NAME'),
        'USER': os.environ.get('POSTGRES_USER'),
        'PASSWORD': os.environ.get('POSTGRES_PASSWORD'),
        'HOST': 'db',
        'PORT': 5432,
    }
}
  1. Run docker compose up
docker compose up

At this point, your Django app should be running at port 8000 on your Docker host. On Docker Desktop for Mac and Docker Desktop for Windows, go to http://localhost:8000 on a web browser to see the Django welcome page.
In Windows 10 set ALLOWED_HOSTS in settings.py. For demo porpouse: ALLOWED_HOSTS = ['*']