1

I am running a simple django webapp with docker-compose. I define both a web service and a db service in a docker-compose.yml file:

version: "3.8"

services:
  db:
    image: postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    ports:
      - "8000:8000"
    env_file:
      - ./.env.dev
    depends_on:
      - db

volumes:
  postgres_data:

I start the service by running:

docker-compose up -d

I can load some data in there with a custom django command that I wrote for my app. Everything is running fine (with data) on localhost:8000.

However, when I run

docker-compose down

(so without -v) and then again

docker-compose up -d

the database is empty again. The volume was not persisted. From what I read in the docker-compose docs and also in several posts here at SO, persisting the volume and reusing it when you start a new container should be the default behavior (which, if I understand it correctly, you can disable by using the --renew-anon-volumes flag).

However in my case, the volume is not persisted. Or maybe it is, but my data is gone.

By doing docker volume ls I can see that my volume (I'll use the name my_volume here) still exists after the docker-compose down command. However, the CreatedAt value has been changed. This makes me think it's a different volume with the same name, and my data is already gone, but I don't know how to confirm that.

This SO answer suggests to mount the volume on /var/lib/postgresql instead of /var/lib/postgresql/data. However, I've seen other resources (like this one) where the opposite is suggested. I've tried both, but neither option works.

Thanks for any advice.

bartaelterman
  • 795
  • 10
  • 26

1 Answers1

-2

It turns out that the Dockerfile of my app was using an entrypoint in which the following command was executed: python manage.py flush which clears all data in the database. As this gets executed every time the app container starts, it clears all data. It had nothing to do with docker-compose.

bartaelterman
  • 795
  • 10
  • 26