如果我对 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)
最佳答案
我详细检查您的问题,用
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 文件夹中的内容。但是
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 文件夹中弹出的内容。最后,为什么
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.
我们能做什么?
目前,解决此问题的方法是使用命名卷:
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 计算机,但它将始终保留在那里。这意味着即使您删除容器,下次使用相同的命名卷,您的容器仍然可以使用持久数据。我们可以期待什么?
也许你可以期待
WSL2
,微软将Linux内核嵌入到Windows中, 然后我们可以在其中安装docker,也许可以解决这个问题。
关于postgresql - 在windows上绑定(bind)挂载postgresql数据不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56946619/