postgresql - 在windows上绑定(bind)挂载postgresql数据不起作用

标签 postgresql docker

如果我对 MySQL 使用此绑定(bind)挂载,它将在容器启动后填充文件夹/mysql-data

mysql:
  image: mysql:8.0
  volumes:
    - ./mysql-data:/var/lib/mysql

但是如果我对 PostgreSQL 使用相同的方法,文件夹/pg-data 仍为空

postgres:
  image: postgres:11.3-alpine
  volumes:
    - ./pg-data:/var/lib/postgresql[/data]

我已经尝试了右侧的两条路径:/var/lib/postgresql/var/lib/postgresql/data

我知道我可以使用数据卷来保存 postgresql 数据。但我很惊讶,为什么同样的方法适用于 MySQL,但不适用于 PostgreSQL 容器。

设置:Windows 10、Docker Desktop v2.0.0.3 (31259)

最佳答案

  1. 我详细检查您的问题,用 docker for windows , volumes: - ./mysql-data:/var/lib/mysql将挂载 Windows 文件夹 ./mysql-data到容器的文件夹/var/lib/mysql拥有所有权root:root 。然后,mysql:8.0的入口点将启动 mysql 服务器并将内容放入 /var/lib/mysql ,这样您就可以看到 Windows 文件夹中的内容。

  2. 但是 postgres:11.3-alpine 的情况有所不同,如果你检查日志:

    PS E:\test> docker-compose logs postgres
    Attaching to test_postgres_1
    postgres_1  | The files belonging to this database system will be owned by user "postgres".
    postgres_1  | This user must also own the server process.
    postgres_1  |
    postgres_1  | The database cluster will be initialized with locale "en_US.utf8".
    postgres_1  | The default database encoding has accordingly been set to "UTF8".
    postgres_1  | The default text search configuration will be set to "english".
    postgres_1  |
    postgres_1  | Data page checksums are disabled.
    postgres_1  |
    postgres_1  | fixing permissions on existing directory /var/lib/postgresql/data ... ok
    postgres_1  | creating subdirectories ... ok
    postgres_1  | selecting default max_connections ... 20
    postgres_1  | selecting default shared_buffers ... 400kB
    postgres_1  | selecting dynamic shared memory implementation ... posix
    postgres_1  | creating configuration files ... ok
    postgres_1  | 2019-07-09 13:50:21.843 UTC [47] FATAL:  data directory "/var/lib/postgresql/data" has wrong ownership
    postgres_1  | 2019-07-09 13:50:21.843 UTC [47] HINT:  The server must be started by the user that owns the data directory.
    postgres_1  | child process exited with exit code 1
    postgres_1  | initdb: removing contents of data directory "/var/lib/postgresql/data"
    postgres_1  | running bootstrap script ...
    

    您可以看到data directory "/var/lib/postgresql/data" has wrong ownership ,虽然docker-entrypoint.sh已经做了chown -R postgres "$PGDATA"将所有权从 root 更改为至postgres ,但不幸的是,如果你在 Linux 上运行容器,这才有效。对于 Windows,这会失败,但是 postgres需要此文件夹的所有权为 postgres ,不是root 。因此,即使该服务没有在 Windows 上启动,那么,您当然也看不到 Windows 文件夹中弹出的内容。

  3. 最后,为什么chown -R postgres "$PGDATA"如果 docker 主机是 windows 会失败? This就是答案。

    Unfortunately, with the current implementation (based on CIFS/Samba) we can't improve this.

    Attempting to change these values via chmod/chown will return success but have no effect.

    https://www.samba.org/samba/docs/man/manpages-3/mount.cifs.8.html#id2532725

    I'm leaving this issue open for tracking.

  4. 我们能做什么?

    目前,解决此问题的方法是使用命名卷:

    docker-compose.yaml:

    postgres:
    image: postgres:11.3-alpine
      volumes:
        - pg-data:/var/lib/postgresql/data
    

    通过上述操作,数据不会弹出到windows文件夹pg-data中,它只会由 docker 自己维护。您可以使用 next 来检查音量:

    PS E:\test> docker volume inspect pg-data
    [
        {
            "CreatedAt": "2019-07-09T14:09:38Z",
            "Driver": "local",
            "Labels": null,
            "Mountpoint": "/var/lib/docker/volumes/pg-data/_data",
            "Name": "pg-data",
            "Options": null,
            "Scope": "local"
        }
    ]
    

    虽然Mountpoint位于 MOBY VM 中,这是一台您无法访问的 Hyper-V 计算机,但它将始终保留在那里。这意味着即使您删除容器,下次使用相同的命名卷,您的容器仍然可以使用持久数据。

  5. 我们可以期待什么?

    也许你可以期待WSL2 ,微软将Linux内核嵌入到Windows中, 然后我们可以在其中安装docker,也许可以解决这个问题。

关于postgresql - 在windows上绑定(bind)挂载postgresql数据不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56946619/

相关文章:

sql - 如何将列名转换为多行?

sql - PostgreSQL函数检查两个文本是否完全相同

postgresql - 在字符串中实现每个查询的结果

sql - 使用 Postgres : Create a Column of Binary Data Conditional on Data in Another Table

selenium - Firefox Gecko 相当于 Chrome 的 --disable-dev-shm-usage 标志是什么

ruby-on-rails - 在 postgres 中按升序排序,最后为 0

node.js - docker apt-get 更新错误

docker - Docker-尝试连接到数据库时出现php_network_getaddresses错误

如果将主机添加到 daemon.json,Windows Docker 守护进程会崩溃

windows - Docker for Windows 在更新到版本 2.1.1.0 Edge(或稳定版)后卡在 "Kubernetes is Starting"