标题: 解决Dify:failed to init dify plugin db题目 [打印本页] 作者: 拉不拉稀肚拉稀 时间: 2025-4-8 12:44 标题: 解决Dify:failed to init dify plugin db题目 Dify最新版本1.1.3(langgenius/dify: Dify is an open-source LLM app development platform. Dify's intuitive interface combines AI workflow, RAG pipeline, agent capabilities, model management, observability features and more, letting you quickly go from prototype to production.),使用docker compose进行当地部署时,在docker container的log中,总会出现如下提示:
“[error] failed to initialize database, got error failed to connect to `host=db user=postgres database=postgres`: hostname resolving error”。对此,dify issues的回答是"This version is not officially supported yet. Please do not use this version if you can not understand it.",并建议你使用0.15.3版本。
为了解决题目,我向kimi提问,在其中一个回答中给了如下解决方案:
“你已经使用了 depends_on 来确保 db 和 redis 服务先于 api 服务启动。但 depends_on 只能确保服务启动次序,不能确保服务完全可用。为了确保 PostgreSQL 完全启动后再启动 api 服务,可以使用 wait-for-it.sh 脚本或其他类似的工具。”
而我使用wait-for-it.sh确实让上述错误消散了。
首先我分析了dify\docker目录下的docker-compose.yaml文件,发现在dify环境中,各模块的相互依赖关系如下:
api:db,redis
woker:db,redis
plugin_daemon:db
nginx:api,web
经测试,nginx可以忽略,但是plugin_daemon、api、woker这三个模块必须保证依赖次序关系精确实现。为此我需要将wait-for-it.sh脚本嵌入到这三个模块的docker镜像中,并在该镜像被docker compose加载时,运行wait-for-it.sh,以确保镜像自身运行启动命令前,其前序的模块已经精确加载并运行。api和woker两个模块均依赖langgenius/dify-api:1.1.3,而plugin_daemon则依赖langgenius/dify-plugin-daemon:0.0.6-local。我需要将wait-for-it.sh嵌入到这两个镜像中,并修改dify\docker\docker-compose.yaml文件内容。
我的操纵步骤如下:
1.从 wait-for-it.sh GitHub 堆栈 下载脚本文件wait-for-it.sh。注意下载的wait-for-it.sh要确保其是unix文件格式,也就是行尾不能是CRLF,而应该是LF。一个简单的方法:用vscode打开这个sh文件,在窗口的状态栏中若显示的是"CRLF",则只需点击这个“CRLF”标识,将其改成LF即可。
2.将这个wait-for-it.sh文件拷贝至git clone的Dify目录下的docker子目录中。我的目录如下:D:\AiTools\dify\docker
3.同时在这个目录中创建一个Dockerfile,内容如下: