Docker-compose postgreSQL权限错误

4

我在使用docker-compose postgreSQL时遇到了问题,我正在使用Windows WSL 2 + Docker Desktop。当我在Linux服务器上运行docker-compose时,它可以正常工作,但是当我尝试在本地启动它时,我会收到以下错误:

postgres      | chmod: changing permissions of '/var/lib/postgresql/data': Operation not permitted
ngnix         | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist
postgres      | The files belonging to this database system will be owned by user "postgres".
postgres      | This user must also own the server process.
postgres      |
postgres      | The database cluster will be initialized with locale "en_US.utf8".
postgres      | The default database encoding has accordingly been set to "UTF8".
postgres      | The default text search configuration will be set to "english".
postgres      |
postgres      | Data page checksums are disabled.
postgres      |
ngnix         | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
ngnix         | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
postgres      | fixing permissions on existing directory /var/lib/postgresql/data ... initdb: error: could not change permissions of directory "/var/lib/postgresql/data": Operation not permitted
ngnix         | /docker-entrypoint.sh: Configuration complete; ready for start up
postgres exited with code 1

我尝试像这样更改所有项目的权限 => chmod 777 -R project_folder

Dcoker-compose.yml:

postgresdb:
    container_name: postgres
    build:
      context: ./docker/postgres
      dockerfile: Dockerfile
    environment:
      - POSTGRES_PASSWORD=password123
    volumes:
    - ./docker/postgres/init.sql:/docker-entrypoint-initdb.d/init.sql
    - ./postgres-data:/var/lib/postgresql/data

    ports:
    - "5432:5432"

我不知道该如何解决它。请帮帮我!


你为什么不使用Docker Volumes来管理数据目录,而是选择了相对路径呢? - madflow
没有理由,我是docker系统的新手 :) - Alexei Safronov
你正在WSL(也就是Linux)上运行。尝试使用命令打开./postgres_data目录:chmod 777 ./postgres_data。然后重新启动compose文件。 - BertC
4个回答

5
所以我的猜测是,由于你在WSL中将目录作为"data"目录挂载到了docker-compose文件相对路径下,因此会出现权限错误。你可以尝试修复这些权限(使用chmod、chown等命令来修改本地目录的权限)。
你也可以使用"命名"卷——这样应该也可以解决权限问题。
# docker-compose.yml

services:
  # ...
  db:
    image: postgres:latest
    volumes:
      - "dbdata:/var/lib/postgresql/data"

volumes:
  dbdata:

1

我的解决方案是从Windows PowerShell开始build,这样就不会出现权限问题。


除了你的解决方案可行之外,我的解决方案是改回Mac... - Edwin

1

通过在WSL 2实例的/etc/wsl.conf文件中添加以下行来更新目录的挂载选项:

[automount]
options = "metadata,uid=1000,gid=1000,umask=22,fmask=11"

然后

wsl --shutdown

对我有效


0
这个问题(至少在我的情况下)可以通过编辑/etc/wsl.conf来解决,就像Hanson在另一个答案中所说的那样。然而,你只需要设置的选项是启用元数据。
[automount]
options = "metadata"

关闭所有WSL虚拟机的实例/终端,等待至少8秒以停止进程,然后重新启动。 uid、gid、umask和fmask的默认设置应该可以正常工作。有关/etc/wsl.conf中的默认设置和配置,请参阅https://learn.microsoft.com/en-us/windows/wsl/wsl-config#configuration-settings-for-wslconf
有关通过WSL在Windows和Linux之间进行文件权限交互的更多信息,请参阅https://learn.microsoft.com/en-us/windows/wsl/file-permissions

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接