9

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?

9
  • it will be under $env:ALLUSERSPROFILE\docker\volumes Commented Feb 6, 2018 at 15:07
  • 1
    I have looked at every folders in C:\Users. But, can't find that docker\volumes folder. Commented Feb 6, 2018 at 15:11
  • Open powershell prompt and type dir $env:ALLUSERSPROFILE\docker\volumes Commented Feb 6, 2018 at 15:39
  • gist.github.com/yccheok/e02a87e596a30fd95fe93bafd3f6d74c - Directory volumes doesn't exists. Commented Feb 6, 2018 at 18:01
  • 1
    Never mind I just realized you are using UNIX containers. I'm not sure how those are presented on Windows filesystem since container actually run in VM on Windows Commented Feb 6, 2018 at 19:21

1 Answer 1

0

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.

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.