前言
如果消息平台(NapCat/Lagrange)和 LangBot 均使用 Docker 启动,那么你需要配置 Docker 的网络连接。
新建一个网络:
lua
docker network create langbot-network
在 LangBot 目录的 docker-compose.yaml
文件中添加网络配置(添加到 services.langbot
字段下,并在 networks
字段下添加 langbot-network
网络配置):
yaml
services:
langbot:
...
networks:
- langbot-network
...
networks:
langbot-network:
external: true
然后在消息平台的 docker-compose.yaml
文件中添加同样的网络配置(如果是直接 docker run 命令启动的,请在启动命令中添加 --network langbot-network
)。接着在消息平台配置文件中,把 WS 反向连接地址中的 IP 地址修改为 langbot
(例如:ws://langbot:2280/ws
)。最后重启两个容器。
以下是详细配置指南
完整配置步骤
1. 创建 Docker 网络
bash
lua
docker network create langbot-network
2. 修改 Dify 的 docker-compose.yaml
yaml
yaml
version: '3'
services:
nginx:
container_name: dify-nginx # 关键:设置容器名称
image: nginx:latest
ports:
- "80:80"
networks:
- langbot-network # 加入网络
# ... 其他配置保持不变
backend:
image: dify/backend:latest
networks:
- langbot-network # 加入网络
# ... 其他配置保持不变
worker:
image: dify/worker:latest
networks:
- langbot-network # 加入网络
# ... 其他配置保持不变
# ... 其他服务同样添加网络配置
networks:
langbot-network:
external: true # 使用外部创建的网络
3. 修改 LangBot 的 docker-compose.yaml
yaml
yaml
version: '3'
services:
langbot:
image: your-langbot-image:latest
environment:
- BASE_URL=http://dify-nginx/v1 # 关键配置
networks:
- langbot-network # 加入同一个网络
ports:
- "2280:2280" # LangBot 默认端口
# ... 其他配置保持不变
networks:
langbot-network:
external: true # 使用外部创建的网络
4. 配置 LangBot 访问 Dify
在 LangBot 的配置文件(通常是 .env
或 config.yaml
)中添加:
env
ini
# LangBot 配置
DIFY_BASE_URL=http://dify-nginx/v1
DIFY_API_KEY=your_dify_api_key_here
5. 启动服务
bash
bash
# 启动 Dify
cd dify-directory
docker-compose up -d
# 启动 LangBot
cd langbot-directory
docker-compose up -d
验证配置是否成功
- 检查网络连通性:
bash
bash
docker exec -it langbot ping dify-nginx
# 应该能看到成功的 ping 响应
- 测试 API 连接:
bash
bash
docker exec -it langbot curl -X GET http://dify-nginx/v1/workspaces
# 应该返回 Dify 的工作空间信息
- 检查 LangBot 日志:
bash
ini
docker logs langbot-langbot-1
# 查找类似 "Connected to Dify API at http://dify-nginx/v1" 的消息
常见问题解决方案
问题1:LangBot 无法连接 Dify
-
解决方案:
- 检查容器是否在同一网络:
docker network inspect langbot-network
- 在 LangBot 容器内测试连接:
docker exec -it langbot curl -v http://dify-nginx
- 确保 Dify 的 nginx 服务正常运行
- 检查容器是否在同一网络:
问题2:出现 502 Bad Gateway 错误
-
解决方案:
- 检查 Dify 后端是否启动:
docker-compose logs backend
- 验证 nginx 配置:
docker exec -it dify-nginx nginx -t
- 确保 API 路径正确:
BASE_URL=http://dify-nginx/v1
- 检查 Dify 后端是否启动:
问题3:跨域请求(CORS)问题
- 在 Dify 的 nginx 配置中添加:
nginx
bash
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization';
# ...其他配置
}
最终架构示意图

按照以上步骤配置后,LangBot 应该能够通过 http://dify-nginx/v1
地址成功访问您本地部署的 Dify 服务。这种配置方式利用了 Docker 的内部 DNS 解析机制,确保容器间可以通过服务名称直接通信。