运行带持久数据的postgresql docker镜像返回权限错误

我一直在我的Vagrant机器上运行我的docker图像(盒子是ubuntu 14.04 ),没有任何大问题。 但是,下面的错误是绞尽脑汁。 我希望你们能帮助我。

当我运行这个:

$ docker run -it -v /vagrant/postgresql/data:/var/lib/postgresql/data postgres:9.2

我得到这个错误

 The files belonging to this database system will be owned by user "postgres". This user must also own the server process. The database cluster will be initialized with locale "en_US.utf8". The default database encoding has accordingly been set to "UTF8". The default text search configuration will be set to "english". fixing permissions on existing directory /var/lib/postgresql/data ... ok initdb: could not create directory "/var/lib/postgresql/data/pg_xlog": Permission denied initdb: removing contents of data directory "/var/lib/postgresql/data" vagrant@legionat:/vagrant/sonarqube$ docker run -it -v /vagrant/postgresql/data:/var/lib/postgresql/data postgres:9.2 The files belonging to this database system will be owned by user "postgres". This user must also own the server process. The database cluster will be initialized with locale "en_US.utf8". The default database encoding has accordingly been set to "UTF8". The default text search configuration will be set to "english". fixing permissions on existing directory /var/lib/postgresql/data ... ok initdb: could not create directory "/var/lib/postgresql/data/pg_xlog": Permission denied initdb: removing contents of data directory "/var/lib/postgresql/data" 

我已经尝试打开/ vagrant / postgresql的所有权限,但没有成功。 也许这是官方docker形象的问题。

编辑:

我刚刚注意到,这是很多人面对同样的问题,我: https : //github.com/docker-library/postgres/issues/26

而且,正如有人在评论中提到的那样,这里是这样的:

 $ ls -l /vagrant/postgresql/data total 0 

如果您只关心数据的持久性,我会build议使用数据卷而不是主机卷。

运行docker volume create --name pgdata然后将其连接到你的容器: docker run --rm --name pg -v pgdata:/var/lib/PostgreSQL/data postgres:9.2

即使在容器已经不存在的情况下,您也可以启动一个新的容器,并且您的数据将在那里。

只要确保你的Vagrant用户有权限访问这个目录:

 ls -ld /vagrant/postgresql/data 

deinspanjer说。 您可以使用命名卷来保存数据