I am learning Django by trying to create the polling app described on the Django's website. At the same time, I am using Docker for the first time and I have encountered problems trying to make PostgreSQL database, used by this app, persistent.
Following the tips on using Docker with Django, found on the Docker website, I am running two services with docker-compose: one handling the database and the other the app itself (at least that's how I understand it). I have tried mounting a volume at the /var/lib directory inside the database container, where I found PostgreSQL directory. Although this directory became persistent, which I checked by creating some dummy files, migrations done via the web service are being erased every time I restart those containers.
Below are my docker-copmpose.yml file and Django's settings.py. What should I do to make this database persistent?
#docker-compose.yml
version: '3'
services:
db:
image: postgres
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
volumes:
- .:/code
- .:/var/lib
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
#(...)
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
#(...)