运行 postgresql docker 具有持久数据的图像 returns 权限错误

Run postgresql docker image with persistent data returns permission error

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

当我运行这个:

$ 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

如果您只关心持久化数据,我建议使用数据卷而不是主机卷。

运行 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 所说。您可以使用命名卷来保存数据