3

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,
    }
}
#(...)

1 Answer 1

3

The data persistance in docker can be tricky but here are 2 possible solutions :

  1. Don't worry, docker will handle it. In your case, since you are usinng a compose file, as long as you do not clean your volumes and that you stop your docker compose with a 'docker-compose down' there should be no issues. Once you will use a 'docker-compose up' your data should still be on the database.
  2. You want more control (and maybe share easely the data). In that case you can choose to manualy mount a file in which your data will be stored. For that you can learn more here (at the bottom of the page). Tips :volumes: - /my/own/datadir:/var/lib/postgresql/data

I hope that aswer your question :)

Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks for your answer! Starting with your first point, the thing is that I am always stopping docker with docker-compose down and starting it with docker-compose up, yet it still erases the data. Next, I am aware of the manual control over docker's volumes, and I am trying to use this feature. However, I think I lack understanding of how PostgreSQL works and consequently what directories need to be made persistent in order for it to work properly. I have described in the question which directories I tried to make persistent so far.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.