Mount Postgres data to Windows host directory

余生颓废 提交于 2019-12-08 17:30:13

问题


I want to ensure my Postgres data (using Linux based image) persist, even after my Windows host machine restart.

I tried to follow the steps How to persist data in a dockerized postgres database using volumes

docker-compose.yml

volumes:
  - ./postgres_data:/var/lib/postgresql/data

However, I'm getting error

waiting for server to start....FATAL:  data directory "/var/lib/postgresql/data/pgdata" has wrong ownership
HINT:  The server must be started by the user that owns the data directory.
 stopped waiting
pg_ctl: could not start server

Then, I tried to follow step in https://forums.docker.com/t/trying-to-get-postgres-to-work-on-persistent-windows-mount-two-issues/12456/5

The suggested method are

docker volume create --name postgres_data --driver local

docker-compose.yml

services:
  postgres:
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:
    external: true

However, I'm confused on the command docker volume create --name postgres_data --driver local. As, it doesn't mention the exact path of Windows host machine.

I tried

C:\celery-hello-world>docker volume create postgres_data
postgres_data

C:\celery-hello-world>docker volume inspect postgres_data
[
    {
        "CreatedAt": "2018-02-06T14:54:48Z",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/postgres_data/_data",
        "Name": "postgres_data",
        "Options": {},
        "Scope": "local"
    }
]

May I know where is the Windows directory location, which VOLUME postgres_data mount to?


回答1:


Today i was asking myself the same question but i have figured it out.

First to note is that inside postgres container the path is:

 /var/lib/postgresql/data

Now the path you tried to track down is another path:

C:\celery-hello-world>docker volume inspect postgres_data [ { "CreatedAt": "2018-02-06T14:54:48Z", "Driver": "local", "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/postgres_data/_data", "Name": "postgres_data", "Options": {}, "Scope": "local" } ]

/var/lib/docker/volumes/postgres_data/_data

I am also using Docker for windows and Unix containers.

As you can check normally you will not see this docker machine:

docker-machine ls
NAME   ACTIVE   DRIVER   STATE   URL   SWARM   DOCKER   ERRORS

But there's a trick to access it regarding Unix containers:

docker run -it --rm --privileged --pid=host justincormack/nsenter1

Just run this from your CLI and it'll drop you in a container with full permissions on the Moby VM. Only works for Moby Linux VM (doesn't work for Windows Containers). Note this also works on Docker for Mac.

Reference: https://www.bretfisher.com/getting-a-shell-in-the-docker-for-windows-vm/

From there you can navigate to this folder (note that i named my volume postgresql-volume):

/var/lib/docker/volumes/postgresql-volume/_data    ls
    PG_VERSION            pg_dynshmem           pg_notify             pg_stat_tmp           postgresql.auto.conf
    base                  pg_hba.conf           pg_replslot           pg_subtrans           postgresql.conf
    global                pg_ident.conf         pg_serial             pg_tblspc             postmaster.opts
    pg_clog               pg_logical            pg_snapshots          pg_twophase           postmaster.pid
    pg_commit_ts          pg_multixact          pg_stat               pg_xlog

So to be clear it's inside of the VM provided by Docker for Windows in the directory above.

Docker for windows needs Hyper-V enabled for virtualization.

You can also find image location in Docker for windows go to Settings->Advanced->Disk image location in my case it's:

"C:\ProgramData\DockerDesktop\vm-data\DockerDesktop.vhdx"

Also note that the same could be found in Hyper-V Manager.



来源:https://stackoverflow.com/questions/48645804/mount-postgres-data-to-windows-host-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!