前言
本文主要参考 github上dify的官方问答 这篇文章,感谢您抽出宝贵的时间阅读!欢迎批评指正!
在windows上用docker desktop + wsl2(我用的ubuntu)部署dify时,如果把dify的项目文件放在d盘(在ubuntu下的目录是/mnt/d),此时直接docker compose up -d启动容器, 会导致 postgre数据库不断重启,并报如下的权限问题:
db-1 | chmod: /var/lib/postgresql/data/pgdata: Operation not permitted
db-1 | The files belonging to this database system will be owned by user "postgres".
db-1 | This user must also own the server process.
具体原因跟据 官方问答中的评论,似乎是docker-compose.yml文件中,表明了容器内postgre数据库的相关文件存储地址 /var/lib/postgresql/data/pgdata 映射到了宿主机的 D:/dify-main/volumes/db/data上, 而又因为D盘本质上属于window系统,wsl2没有相应的权限去进行操作,所以导致刚才所述的问题, 解决方法就是改变映射,让他直接映射到wsl2的存储空间
另一个解释是来自这篇评论
从运行 Docker for Windows 的 VM 的角度来看,任何来自宿主机的共享目录都由 root( docker/for-win#63 )拥有。如果我记得没错的话,任何用户都可以读写这些目录。不幸的是,Postgres 必须以目录的所有者身份运行(无论读写是否成功),而不能以 root 身份运行。因此,唯一的解决方案是使用命名卷而不是将共享目录提供给 Windows 宿主机。
解决方案1:
-
将容器内postgre 数据库存储地址,映射到wsl2中的存储空间,例如/home 文件夹
-
具体操作如下
-
打开docker-compose.yml文件
-
找到postgre数据库对应的一段
# The postgres database.db:image: postgres:15-alpinerestart: alwaysenvironment:PGUSER: ${PGUSER:-postgres}POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-difyai123456}POSTGRES_DB: ${POSTGRES_DB:-dify}PGDATA: ${PGDATA:-/var/lib/postgresql/data/pgdata}command: >postgres -c 'max_connections=${POSTGRES_MAX_CONNECTIONS:-100}'-c 'shared_buffers=${POSTGRES_SHARED_BUFFERS:-128MB}'-c 'work_mem=${POSTGRES_WORK_MEM:-4MB}'-c 'maintenance_work_mem=${POSTGRES_MAINTENANCE_WORK_MEM:-64MB}'-c 'effective_cache_size=${POSTGRES_EFFECTIVE_CACHE_SIZE:-4096MB}'volumes:- ./volumes/db/data:/var/lib/postgresql/datahealthcheck:test: [ 'CMD', 'pg_isready' ]interval: 1stimeout: 3sretries: 30ports:- '${EXPOSE_DB_PORT:-5432}:5432'
-
修改映射关系
#找到volumes字段 volumes:- ./volumes/db/data:/var/lib/postgresql/data #改为 volumes:- /home/<你自己的文件夹>:/var/lib/postgresql/data
-
解决方案2:来自这篇文章的评论
Ok, so here is a solution that helped me. First, I ran the command docker volume create --name=pgdata
in the terminal. Then my co-worker (who has a lot more experience with Docker than me, who has barely any) helped me modify my docker-compose.yml file so that it included the following:
services:db:image: postgresrestart: unless-stoppedenv_file:- .envvolumes:- pgdata:/var/lib/postgresql/dataports:- "5434:5432"volumes:pgdata:external: true
Note: the volumes block is indented at the same level as services. This apparently helps tell Docker that the particular folder being used for data storage is defined as a volume, which has a different set of access rights versus a folder. I'm not entirely sure if this is the correct way to explain it, but my error of data directory "/var/lib/postgresql/data" has invalid permission
has ceased and the app is now working.
解决方案3:
The reason is you download the source code in your Windows system, but run docker in wsl2.
The correct opreation is:
- git clone in wsl2, not in windows
- docker pull
- dock compose
原因是docker使用wsl2后端,【wsl环境的docker环境里面的程序】无权限访问【windows环境下载的dify文件夹】,重新在wsl下git就能拉起。
总结
三种方式笔者均尝试了,均可成功部署dify
笔者认为正确的解释应该还是【wsl环境的docker环境里面的程序】无权限访问【windows环境下载的dify文件夹】,所以需要更改映射
欢迎批评指正!