Home > database >  how can I save docker's database data locally on my server?
how can I save docker's database data locally on my server?

Time:09-27

I'm running an app inside a docker container. That app uses docker Postgres image to save data in a database. I need to keep a local copy of this database's data to avoid losing data if the container is removed or purged somehow ..so I am using volumes inside my `docker-compose.YAML file,, but still the local DB folder is always empty .. so whenever I move the container or purge it the data are lost

docker-compose.yaml

version: "2"

services:

  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    ports:
      - '5433:5432'
    restart: always       
    command: -p 5433
    environment:
      - POSTGRES_DB=mydata
      - POSTGRES_USER=mydata
      - POSTGRES_PASSWORD=mydata@
      - PGDATA=/tmp


  django-apache2:
    build: .
    container_name: rolla_django
    restart: always 
    environment:
      - POSTGRES_DB=mydata
      - POSTGRES_USER=mydata
      - POSTGRES_PASSWORD=mydata@
      - PGDATA=/tmp
    ports:
      - '4002:80'
      - '4003:443'
    volumes:
      - ./www/:/var/www/html
      - ./www/demo_app/static_files:/var/www/html/demo_app/static_files
      - ./www/demo_app/media:/var/www/html/demo_app/media

#    command: sh -c 'python manage.py migrate && python manage.py loaddata db_backkup.json && apache2ctl -D FOREGROUND'
    command: sh -c 'wait-for-it db:5433 -- python manage.py migrate && apache2ctl -D FOREGROUND'

    depends_on:
      - db

as you can see i used ./data/db:/var/lib/postgresql/data , but locally the ./data/db directory is always empty !!

NOTE

when I use docker volume list it shows no volumes at all

CodePudding user response:

According to your setup, the data is in /tmp: PGDATA=/tmp. Remove this and you volume mapping should work.

Also your command -p 5433 makes postgres run on port 5433, but you still map the port 5432. So if you cant reach the database it might be because of that.

  • Related