0

After reading

How to persist data in a dockerized postgres database using volumes

I know mount a volume to local host folder, is a good way to prevent data loss, in case fatal happens in docker process.

I'm using Windows as host and Linux based docker image. Windows is having problem to mount postgres data (Linux based docker image) to Windows host directory - Mount Postgres data to Windows host directory

Later, I discover another technique

  postgres:
    build:
      context: ./postgres
      dockerfile: Dockerfile
    restart: always
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data: {}

I can use a non external volume (I have no idea what is that yet)

After implement such, I try to perform

docker-compose down
docker-compose up

The data is still there.

May I know,

  1. Is this still a good way to persist data in dockerized postgres?
  2. Where exactly data is being stored? Is it some hidden directory in host machine?
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

1 Answers1

1
  1. Is this still a good way to persist data in dockerized postgres? no but olso yes ....postgres is DB so data should be externalized to avoid data-loss in case of connection failure to the container etc...

But a good practice would be to have 2 DB 1 on containers 1 on host or in 2 containers (then with data inside containers) in master/slave mode synchro to have high availability for container maintenance for example but this is high availability only not backup ! :) if it doesn't exist you have to create it of course :-)

  1. Where exactly data is being stored? Is it some hidden directory in host machine?

No it is where you share the /var/lib/postgres so in your example in a directory called postgres_data on host

(use full path is a good practice & then you saw/guess by yourself where it was define in your file) :)

francois P
  • 306
  • 6
  • 20
  • But, I look around my host Windows machine still couldn't find where is `postgres_data` folder. Do you have any idea? – Cheok Yan Cheng Feb 06 '18 at 19:26
  • you decide where you create it ... I think somewhere like d:/shares/postgres_data would be fine. – francois P Feb 06 '18 at 19:28
  • 1
    I think `- ./postgres_data:/var/lib/postgresql/data` (Will point to your local host directory) is different from `- postgres_data:/var/lib/postgresql/data` (I have no idea where it points to?) – Cheok Yan Cheng Feb 06 '18 at 19:30
  • no /var/lib/posgresql/data is local postgres_data is variable on host local data maybe if you don't know where it is it should be the default c/Users//var/local/...../data if you run your container manually docker run -rm -it -v /toto:/var/log/ somecontainer bash ls /var/log you will see content of host toto directory wich is mounted as /var/log inside the container – francois P Feb 06 '18 at 19:31
  • But, I'm using Windows machine as host. Windows machine doesn't have `/var/lib/posgresql/data` – Cheok Yan Cheng Feb 06 '18 at 19:35
  • this is why you use a mapper -v to share a host directory to /var/lib/postgresql/data this is the role of `volumes: - postgres_data:/var/lib/postgresql/data` this is similar to CLI option -v localhostdirectoy:containerpathofsamedirectory – francois P Feb 06 '18 at 19:37
  • 1
    look for folder `base` - parent will be `PGDATA` – Vao Tsun Feb 06 '18 at 19:59