postgresqldockerdocker-composedocker-volumepersistent-storage

Why isn't my PostgreSQL data persistent in Docker using volumes?


I am currently learning how to use Flask, PostgreSQL and Docker because I am trying to do a web app.

I have created two containers for the development phase: one for the database and other one for the web. The problem that I have is that whenever I stop and restart my containers, the data is not there anymore.

For the creation of the containers I have followed the following link: https://testdriven.io/blog/dockerizing-flask-with-postgres-gunicorn-and-nginx/#project-setup

I have seen that I could create a volume to persist the data in the container but it doesn't seem to work and I don't really understand why. Here is my docker-compose file:

version: '3.7'

services:
 web:
   build: ./services/web
   restart: always
   command: python manage.py run -h 0.0.0.0
   volumes:
     - ./services/web/:/usr/src/app/
     - ./migrations:/usr/src/app/migrations
   ports:
     - 5000:5000
   env_file:
     - ./dev.env
   depends_on:
     - db
 db:
   container_name: postgres
   restart: always
   image: postgres:latest
   volumes:
     - pgdata:/var/lib/postgresql/data
     - .:/usr/src/app #For refreshing the container if the code changes
   ports: 
     - 5432:5432
   environment:
     - POSTGRES_USER=hello_flask
     - POSTGRES_PASSWORD=hello_flask
     - POSTGRES_DB= hello_flask_dev #To change

volumes:
 pgdata:

I am careful to use docker-compose up -d so I don't remove any volume and the following volume is created seen with docker volume inspect:

[
    {
        "CreatedAt": "2021-04-19T13:19:05Z",
        "Driver": "local",
        "Labels": {
            "com.docker.compose.project": "hera_docker",
            "com.docker.compose.version": "1.29.0",
            "com.docker.compose.volume": "pgdata"
        },
        "Mountpoint": "/var/lib/docker/volumes/hera_docker_pgdata/_data",
        "Name": "hera_docker_pgdata",
        "Options": null,
        "Scope": "local"
    }
]

I would like to maintain the docker-compose commands because the two containers are together in a multicontainer.

Any help will be of great help. I have checked some other questions in this forum but I don't really know what's going on that mine doesn't work.

Thanks in advance.


Solution

  • Well, after trying and trying and almost giving up, I found the solution. I will leave it here if it helps someone. It appears that the "web" application was overwriting the database and the solution was to change in the docker-compose the depends_on: db in web to depends_on: web in db. Now, it looks like this:

    version: '3.7'
    
    services:
      web:
        build: ./services/web
        restart: always
        command: python manage.py run -h 0.0.0.0
        volumes:
          - ./services/web/:/usr/src/app/
        ports:
          - 5000:5000
        env_file:
          - ./dev.env
      db:
        container_name: postgres
        restart: always
        image: postgres:latest
        volumes:
          - "pgdata:/var/lib/postgresql/data"
          - .:/usr/src/app
        ports: 
          - 5432:5432
        environment:
          - POSTGRES_USER=hello_flask
          - POSTGRES_PASSWORD=hello_flask
          - POSTGRES_DB=hello_flask_dev
        depends_on:
          - web
    volumes:
      pgdata:
    

    The change from pgdata:/var/lib/postgresql/data to "pgdata:/var/lib/postgresql/data" was just to not create a new folder in my flask directory.