Dockerized django backend example with postgresql.
- Create the Django project by running the docker compose run command as follows.
sudo docker compose run web django-admin startproject composeexample .
- Connect db
- Edit
composeexample/settings.py
file. - Replace
DATABASES = ...
- Edit
# 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,
}
}
- 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 = ['*']