2

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.

2
  • 1
    Could you confirm that when you run your application the Postgres data is actually in the /var/lib/docker/volumes/hera_docker_pgdata/_data on your host? I mean before stopping which allegedly clears it. Commented Apr 19, 2021 at 14:04
  • @MichalRosenbaum sorry, I didn't get a notification for your comment. I don't really know how to look for that route in my computer, but whenever I add something in the web, if I do docker exec -it {container} psql -U hello_flask hello_flask_dev and show data_directory; it appears what I put in pgdata in the docker-compose /var/lib/postgresql/data. Is that what you meant? Commented Apr 20, 2021 at 7:15

1 Answer 1

1

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.

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

Comments

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.