尝试使用卷时,Postgresql 引发“数据目录的所有权错误”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24569394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:31:58  来源:igfitidea点击:

Postgresql raises 'data directory has wrong ownership' when trying to use volume

postgresqldocker

提问by lyapun

I'm trying to run postgresql in docker container, but of course I need to have my database data to be persistent, so I'm trying to use data only container which expose volume to store database at this place.

我正在尝试在 docker 容器中运行 postgresql,但当然我需要让我的数据库数据保持持久性,所以我试图使用仅公开卷的数据容器在这个地方存储数据库。

So, my data container has such Dockerfile:

所以,我的数据容器有这样的 Dockerfile:

FROM ubuntu

# Create data directory
RUN mkdir -p /data/postgresql

# Create /data volume
VOLUME /data/postgresql

Which I run:

我运行的:

docker run --name postgresql_data lyapun/postgresql_data true

In my postgresql.conf I set:

在我的 postgresql.conf 我设置:

data_directory = '/data/postgresql'

Then I run my postgresql container in such way:

然后我以这种方式运行我的 postgresql 容器:

docker run -d --name postgre --volumes-from postgresql_data lyapun/postgresql

And I got:

我得到了:

2014-07-04 07:45:57 GMT FATAL:  data directory "/data/postgresql" has wrong ownership
2014-07-04 07:45:57 GMT HINT:  The server must be started by the user that owns the data directory.

How to deal with this issue? I googled a lot to find some information about using postgresql with docker volumes, but I didn't found anything.

如何处理这个问题?我在谷歌上搜索了很多关于在 docker 卷中使用 postgresql 的信息,但我没有找到任何东西。

Thanks!

谢谢!

采纳答案by lyapun

Ok, seems like I found workaround for this issue.

好的,似乎我找到了解决此问题的方法。

Instead of running postgres in such way:

而不是以这种方式运行 postgres:

CMD ["/usr/lib/postgresql/9.1/bin/postgres", "-D", "/var/lib/postgresql/9.1/main", "-c", "config_file=/etc/postgresql/9.1/main/postgresql.conf"]

I wrote bash script:

我写了 bash 脚本:

chown -Rf postgres:postgres /data/postgresql
chmod -R 700 /data/postgresql
sudo -u postgres /usr/lib/postgresql/9.1/bin/postgres -D /var/lib/postgresql/9.1/main -c config_file=/etc/postgresql/9.1/main/postgresql.conf

And replaced CMD in postgresql image to:

并将 postgresql 图像中的 CMD 替换为:

CMD ["bash", "/run.sh"]

It works!

有用!

回答by Carl Levasseur

A better way to solve that issue, assuming your postgres images is named "postgres" and that your backup is ./backup.tar:

一个更好的方法来解决这个问题,假设你的 postgres 图像被命名为“postgres”并且你的备份是 ./backup.tar:

First, add this to your postgres Dockerfile:

首先,将此添加到您的 postgres Dockerfile:

VOLUME  ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]

Then run:

然后运行:

docker run -it --name postgres -v $(pwd):/db postgres sh -c "tar xvf /db/backup.tar --no-overwrite-dir" && \
docker run -it --name data --volumes-from postgres busybox true && \
docker rm postgres && \
docker run -it --name postgres --volumes-from=data postgres

You don't have permission issues since the archive is extracted by the postgres user of your postgres image, so it is the owner of the extracted files. You can then backup your data using the data container. The advantage of this solution is that you don't chmod/chown every time you run the image.

您没有权限问题,因为存档是由 postgres 图像的 postgres 用户提取的,因此它是提取文件的所有者。然后,您可以使用数据容器备份您的数据。此解决方案的优点是您不必每次运行映像时都 chmod/chown。

回答by Elmar Dott

This type of errors is quite common when you link a NTFS directory into your docker container. NTFS directories don't support ext3 file & directory access control. The only way to make it work is to link directory from a ext3 drive into your container.

将 NTFS 目录链接到 docker 容器时,此类错误很常见。NTFS 目录不支持 ext3 文件和目录访问控制。使其工作的唯一方法是将目录从 ext3 驱动器链接到您的容器。

I got a bit desperate when I played around Apache / PHP containers with linking the www folder. After I linked files reside on a ext3 filesystem the problem disappear.

当我使用链接 www 文件夹来处理 Apache / PHP 容器时,我有点绝望。在我链接的文件驻留在 ext3 文件系统上后,问题就消失了。

I published a short Docker tutorial on youtube, may it helps to understand this problem: https://www.youtube.com/watch?v=eS9O05TTFjM

我在 youtube 上发布了一个简短的 Docker 教程,可能有助于理解这个问题:https: //www.youtube.com/watch?v=eS9O05TTFjM

回答by Jan Marek

You have to set ownership of directory /data/postgresql to the same user, under which you are running your postgresql binary. For example, in Ubuntu it is usually postgresuser.

您必须将目录 /data/postgresql 的所有权设置为运行 postgresql 二进制文件的同一用户。例如,在 Ubuntu 中,它通常是postgres用户。

Then you have to use this command:

然后你必须使用这个命令:

chown postgres.postgres /data/postgresql