将Postgres数据挂载到Windows主机目录 [英] Mount Postgres data to Windows host directory

查看:170
本文介绍了将Postgres数据挂载到Windows主机目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使在Windows主机重启后,我也想确保我的Postgres数据(使用基于Linux的映像)持久存在。

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

我尝试按照步骤如何使用卷将数据持久化在dockerized postgres数据库中

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

但是,我得到错误

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

然后,我尝试遵循 https://forums.docker.com/t/trying-to-get-postgres-to-work-on-persistent-windows-mount-two-issues/12456/5

建议的方法是

docker volume create --name postgres_data --driver local



docker-compose.yml



docker-compose.yml

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

volumes:
  postgres_data:
    external: true

但是,我对命令感到困惑 docker volume create --name postgres_data --driver local 。因此,它没有提及Windows主机的确切路径。

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.

我尝试过

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"
    }
]

我可以知道Windows目录位置在哪里,哪个VOLUME postgres_data 装载到?

推荐答案

今天我在问自己同样的问题,但我已经解决了。

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

首先要注意的是,在postgres容器内的路径是:

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

 /var/lib/postgresql/data

现在您尝试追踪道琼斯指数的路径n是另一条路径:

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,
名称: 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" } ]



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

我还在Windows上使用 Docker Unix 容器。

I am also using Docker for windows and Unix containers.

通常情况下,您不会看到此docker机器:

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

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

但是对于 Unix 容器,有一个技巧可以访问它:

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

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




只需从您的计算机上运行CLI,它将把您放入具有$ by $ b的Moby VM完全权限的容器中。仅适用于Moby Linux VM(
不适用于Windows容器)。请注意,这也适用于Mac的Docker。

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.

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

从那里可以导航到该文件夹​​(请注意,我将我的卷命名为 postgresql-volume ):

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

因此请注意,它位于 Docker for Windows提供的VM的内部在上面的目录中。

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

用于Windows的Docker 需要启用了虚拟化的Hyper-V

您还可以在 Docker for Windows 转到设置->高级 -> 磁盘映像位置,在我的情况下是:

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"

还要注意,在 Hyper-V Manager 中也可以找到相同的内容。

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

这篇关于将Postgres数据挂载到Windows主机目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆