Windows上的PostgreSQL docker容器

时间:2017-04-19 19:15:01

标签: windows postgresql docker permissions

我一直试图在我的Windows机器上运行PostgresSQL Docker容器并使用以下命令挂载数据卷:

docker run -p 5432:5432 -it --volume c:\Users\me\Desktop\pg\data\:/var/lib/postgresql/data postgres:latest -e POSTGRES_USER=user POSTGRES_PASSWORD=password

但是,当容器试图启动时,我会不断收到权限被拒绝的错误列表:

chown: changing ownership of ‘/var/lib/postgresql/data/pg_log’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_logical/mappings’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_logical/snapshots’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_logical’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_multixact/members/0000’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_multixact/members’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_multixact/offsets/0000’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_multixact/offsets’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_multixact’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_notify/0000’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_notify’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_replslot’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_serial’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_snapshots’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_stat’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_stat_tmp/db_0.stat’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_stat_tmp/db_16395.stat’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_stat_tmp/global.stat’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_stat_tmp’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_subtrans/0000’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_subtrans’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_tblspc’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_twophase’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/PG_VERSION’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_xlog/00000001000000000000000A’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_xlog/00000001000000000000000B’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_xlog/archive_status’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_xlog’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/postgresql.auto.conf’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/postgresql.conf’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/postmaster.opts’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/postmaster.pid’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data’: Permission denied

有人可以指出我做错了吗?

提前致谢。

4 个答案:

答案 0 :(得分:8)

这是Docker for Windows中的已知限制。 Linux< - >从Windows目录到Linux目录的Windows文件系统映射语义不完美,因为挂载是使用CIFS / SMB完成的。其中一个不起作用的是chown(更改所有者),因为它无法映射到您的Windows文件系统。

您应该使用命名卷。此论坛帖子有详细信息:https://forums.docker.com/t/data-directory-var-lib-postgresql-data-pgdata-has-wrong-ownership/17963/24?u=friism

答案 1 :(得分:1)

您是否正在使用具有hyper-v或docker-toolbox的docker?因为根据我的经验,我只使用docker-toolbox(它使用docker-machine),并使用--volume或-v(相同)成功映射卷,你应该把这个术语放在Win路径中:

docker run -p 5432:5432 -it -v /c/Users/me/Desktop/pg/data:/var/lib/postgresql/data postgres:latest -e POSTGRES_USER=user POSTGRES_PASSWORD=password

不确定它是否有帮助。也许您的问题是由于映射卷的路径。祝你好运!

答案 2 :(得分:1)

我为相关的git问题链接之一发布了部分解决方案。 通过使用Postgres 表空间将数据库对象文件存储在Windows主机系统上,可以实现部分解决方法。这不是一个完美的解决方案,因为核心数据目录(由PGDATA表示)仍然位于LinuxVM上。但是至少您可以在Windows主机和Linux VM外部管理后续的数据库创建和文件存储。

docker run --rm --name mypostgres
           -e POSTGRES_PASSWORD=pwd
           -d -p 5432:5432 
           -v /var/lib/docker/basedata:/var/lib/postgresql/data
           -v d:/data/largedb:/mnt/largedb
           postgres

这会将默认数据存储设置为Linux VM(/var/lib/docker/basedata)上的(持久)目录。 它还告诉docker将Windows D:\data\largedb挂载为一个对Postgres容器可见的卷,/mnt/largedb。 (Windows目录不需要存在。) 登录到postgres并使用psql或任何工具,执行以下DDL:

CREATE TABLESPACE winhoststorage LOCATION '/mnt/largedb';
CREATE DATABASE "my_large_db" WITH TABLESPACE = "winhoststorage";

继续,在my_large_db中创建表和数据。停止容器。在Windows资源管理器中导航至D:\data\largedb,您将看到数据文件。重新启动容器,您将看到上一个会话中的数据。

(注意:Win 10上的Windows版Docker桌面(2.2),使用DockerDesktopVM而不是MobyLinux或docker-machine。)

答案 3 :(得分:-1)

这是我制作的一个在Windows操作系统中运行的docker容器。它可能对你有帮助。

https://cloud.docker.com/swarm/dragnet/repository/docker/dragnet/postgres-windows/general

此处的源代码:https://github.com/PaulDMendoza/Docker-Postgres-Windows