一、安装Docker以及Docker Compose
Docker安装教程:Ubuntu安装Docker
Docker Compose安装教程:Ubuntu安装DokcerCompose
二、克隆 Dify 源代码至本地
git clone https://github.com/langgenius/dify.git
三、启动Dify
cd dify/docker
cp .env.example .env
docker compose up -d
注意
如果您的系统安装了 Docker Compose V2 而不是 V1,请使用 docker compose 而不是 docker-compose。通过$ docker compose version检查这是否为情况。在这里阅读更多信息。
部署结果示例:
[+] Running 11/11
✔ Network docker_ssrf_proxy_network Created 0.1s
✔ Network docker_default Created 0.0s
✔ Container docker-redis-1 Started 2.4s
✔ Container docker-ssrf_proxy-1 Started 2.8s
✔ Container docker-sandbox-1 Started 2.7s
✔ Container docker-web-1 Started 2.7s
✔ Container docker-weaviate-1 Started 2.4s
✔ Container docker-db-1 Started 2.7s
✔ Container docker-api-1 Started 6.5s
✔ Container docker-worker-1 Started 6.4s
✔ Container docker-nginx-1 Started 7.1s
检查是否所有容器都正常运行
docker compose ps
包括3个业务服务api / worker / web
,包括6个基础组件weaviate / db / redis / nginx / ssrf_proxy / sandbox
。
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
docker-api-1 langgenius/dify-api:0.6.13 "/bin/bash /entrypoi…" api About a minute ago Up About a minute 5001/tcp
docker-db-1 postgres:15-alpine "docker-entrypoint.s…" db About a minute ago Up About a minute (healthy) 5432/tcp
docker-nginx-1 nginx:latest "sh -c 'cp /docker-e…" nginx About a minute ago Up About a minute 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp
docker-redis-1 redis:6-alpine "docker-entrypoint.s…" redis About a minute ago Up About a minute (healthy) 6379/tcp
docker-sandbox-1 langgenius/dify-sandbox:0.2.1 "/main" sandbox About a minute ago Up About a minute
docker-ssrf_proxy-1 ubuntu/squid:latest "sh -c 'cp /docker-e…" ssrf_proxy About a minute ago Up About a minute 3128/tcp
docker-weaviate-1 semitechnologies/weaviate:1.19.0 "/bin/weaviate --hos…" weaviate About a minute ago Up About a minute
docker-web-1 langgenius/dify-web:0.6.13 "/bin/sh ./entrypoin…" web About a minute ago Up About a minute 3000/tcp
docker-worker-1 langgenius/dify-api:0.6.13 "/bin/bash /entrypoi…" worker About a minute ago Up About a minute 5001/tcp
四、更新Dify
cd dify/docker
docker compose down
git pull origin main
docker compose pull
docker compose up -d
==注意:同步环境变量配置 ==
- 如果 .env.example 文件有更新,请务必同步修改您本地的 .env 文件。
- 检查 .env 文件中的所有配置项,确保它们与您的实际运行环境相匹配。您可能需要将 .env.example 中的新变量添加到 .env 文件中,并更新已更改的任何值。
五、访问系统
在浏览器中输入 http://localhost
访问 Dify。
六、添加模型
七、添加自定义工具(天气查询)
- 使用flask搭建http服务(部署到服务器)
from flask import Flask, request, jsonify
import random
app = Flask(__name__)
@app.route('/weather', methods=['GET'])
def get_weather():
auth_header = request.headers.get('Authorization')
if auth_header != 'Bearer hanfangyuan':
return {"msg": "Invalid Authorization header"}, 403
city = request.json.get('city', None)
if city is None:
return jsonify({
'status': 'error',
'errorInfo': 'No city provided',
'data': None
})
# 随机生成温度,风速和风向
temperature = f'{random.randint(10, 20)}℃'
windspeed = f'{random.randint(1, 5)}级'
winddirect = random.choice(['北风', '南风', '西风', '东风']) # 随机选择风向
# 返回JSON格式的响应
# return jsonify({
# 'status': 'OK',
# 'errorInfo': None,
# 'data': {
# 'city': city,
# 'temp': temperature,
# 'windspeed': windspeed,
# 'winddirect': winddirect
# }
# })
# 返回对LLM友好的字符串格式的响应
return f"{city}今天是晴天,温度{temperature}, 风速{windspeed}, 风向{winddirect}"
if __name__ == '__main__':
app.run(host='0.0.0.0',debug=False, port=4397)
- 测试接口(使用的Apifox)
- 生成openapi schema
curl --location --request POST 'http://服务器地址:4397/weather' \
--header 'Authorization: Bearer hanfangyuan' \
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
--header 'Content-Type: application/json' \
--data-raw '{
"city": "太原"
}'
利用gpt4把curl命令转为openapi schema,代码如下
{
"openapi": "3.1.0",
"info": {
"title": "Weather Service API",
"description": "API for retrieving weather data for a specified city.",
"version": "1.0.0"
},
"servers": [
{
"url": "http://10.254.0.16:4397"
}
],
"paths": {
"/weather": {
"post": {
"description": "Retrieve weather data for a specific city",
"operationId": "getWeatherData",
"requestBody": {
"description": "City for which to fetch weather",
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "Name of the city"
}
},
"required": ["city"]
}
}
}
},
"responses": {
"200": {
"description": "Successful response",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"temperature": {
"type": "number",
"description": "Current temperature in Celsius"
},
"description": {
"type": "string",
"description": "Weather condition description"
}
}
}
}
}
}
},
"security": [
{
"bearerAuth": []
}
]
}
}
},
"components": {
"securitySchemes": {
"bearerAuth": {
"type": "http",
"scheme": "bearer",
"bearerFormat": "JWT"
}
}
}
}
-
在Dify中添加自定义工具
-
使用工具
- 调试完成后进行发布
常见问题
- nginx容器无法启动:(查看本地是否已经启动nginx服务)
- 停止本地nginx服务
- 修改本地nginx监听的端口
- 修改dify环境变量
.env.local
中的nginx端口
- docker compose无法拉取代码
修改/etc/dokcer/daemon.json
文件,配置镜像
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn",
"https://registry.cn-hangzhou.aliyuncs.com",
"https://docker.m.daocloud.io",
"https://kqdukn27.mirror.aliyuncs.com"
]