微信扫码
添加专属顾问
我要投稿
深入解析Dify v1.5.0中的sandbox服务配置与运行原理,助你快速搭建安全代码执行环境。 核心内容: 1. sandbox服务的基础配置与镜像选择 2. 关键环境变量设置与安全策略 3. 数据卷挂载与网络代理配置
本文使用dify v1.5.0版本,主要介绍了Dify
中的sandbox
服务,包括本地sandbox源码运行,以及相关配置文件的介绍。
该部分配置定义了 DifySandbox 服务,用于代码执行的安全沙箱环境。
# The DifySandbox
sandbox:
image: langgenius/dify-sandbox:0.2.12
restart: always
environment:
# The DifySandbox configurations
# Make sure you are changing this key for your deployment with a strong key.
# You can generate a strong key using `openssl rand -base64 42`.
API_KEY: ${SANDBOX_API_KEY:-dify-sandbox}
GIN_MODE: ${SANDBOX_GIN_MODE:-release}
WORKER_TIMEOUT: ${SANDBOX_WORKER_TIMEOUT:-15}
ENABLE_NETWORK: ${SANDBOX_ENABLE_NETWORK:-true}
HTTP_PROXY: ${SANDBOX_HTTP_PROXY:-http://ssrf_proxy:3128}
HTTPS_PROXY: ${SANDBOX_HTTPS_PROXY:-http://ssrf_proxy:3128}
SANDBOX_PORT: ${SANDBOX_PORT:-8194}
PIP_MIRROR_URL: ${PIP_MIRROR_URL:-}
volumes:
- ./volumes/sandbox/dependencies:/dependencies
- ./volumes/sandbox/conf:/conf
healthcheck:
test: [ 'CMD', 'curl', '-f', 'http://localhost:8194/health' ]
networks:
- ssrf_proxy_network
使用 langgenius/dify-sandbox:0.2.12
镜像
配置为 always
自动重启
API_KEY
: 安全密钥(建议使用强密钥)
GIN_MODE
: 运行模式(release 模式)
WORKER_TIMEOUT
: 工作超时时间(15秒)
ENABLE_NETWORK
: 允许网络访问
HTTP_PROXY
/HTTPS_PROXY
: 代理设置
SANDBOX_PORT
: 服务端口(8194)
PIP_MIRROR_URL
: Python 包镜像地址
依赖文件目录:./volumes/sandbox/dependencies:/dependencies
配置文件目录:./volumes/sandbox/conf:/conf
通过 curl 请求 /health
端点。
连接到 ssrf_proxy_network
(内部安全网络)。
文件位置:dify\docker\.env
# ------------------------------
# Environment Variables for sandbox Service
# ------------------------------
# The API key for the sandbox service
SANDBOX_API_KEY=dify-sandbox
# The mode in which the Gin framework runs
SANDBOX_GIN_MODE=release
# The timeout for the worker in seconds
SANDBOX_WORKER_TIMEOUT=15
# Enable network for the sandbox service
SANDBOX_ENABLE_NETWORK=true
# HTTP proxy URL for SSRF protection
SANDBOX_HTTP_PROXY=http://ssrf_proxy:3128
# HTTPS proxy URL for SSRF protection
SANDBOX_HTTPS_PROXY=http://ssrf_proxy:3128
# The port on which the sandbox service runs
SANDBOX_PORT=8194
环境变量配置 | 解释 |
---|---|
SANDBOX_API_KEY | |
SANDBOX_GIN_MODE | |
SANDBOX_WORKER_TIMEOUT | |
SANDBOX_ENABLE_NETWORK | |
SANDBOX_HTTP_PROXY SANDBOX_HTTPS_PROXY | |
SANDBOX_PORT |
文件位置:dify\docker\middleware.env
文件位置:dify\docker\docker-compose.middleware.yaml
(1)EXPOSE_SANDBOX_PORT=8195
(2)SSRF_SANDBOX_HOST=127.0.0.1
(3)"EXPOSE_SANDBOX_PORT:−8195:{SANDBOX_PORT:-8194}"
理想情况下,拉代码、安装依赖库、编译和运行,如下所示:
git clone https://github.com/langgenius/dify-sandbox
./install.sh
/build/build_[amd64|arm64].sh
./main
app:
port:8194
debug:True
key:dify-sandbox
max_workers:4
max_requests:50
worker_timeout:5
python_path:/home/duomiagi/miniconda3/bin/python
python_lib_path:
-"/home/duomiagi/miniconda3/lib/python3.10"
-"/home/duomiagi/miniconda3/lib/python3.10/site-packages"
-"/home/duomiagi/miniconda3/lib/python3.10/lib-dynload"
-"/usr/lib/x86_64-linux-gnu"
-"/etc/ssl/certs/ca-certificates.crt"
-"/etc/nsswitch.conf"
-"/etc/hosts"
-"/etc/resolv.conf"
# - "/run/systemd/resolve/stub-resolv.conf" # Permission denied
# - "/run/resolvconf/resolv.conf" # not such file
-"/etc/localtime"
-"/usr/share/zoneinfo"
-"/etc/timezone"
python_pip_mirror_url:https://pypi.tuna.tsinghua.edu.cn/simple
enable_network:True# please make sure there is no network risk in your environment
enable_preload:False# please keep it as False for security purposes
allowed_syscalls:# please leave it empty if you have no idea how seccomp works
proxy:
socks5:''
http:''
https:''
python_lib_path部分解释,如下所示:
(1)Python 库路径(行 10-12)- 当前启用的 Python 标准库位置
(2)/usr/lib/x86_64-linux-gnu
- Linux 系统上 64 位共享库的标准位置
(3)网络和安全相关文件
(4)时区相关文件
但是,使用WSL2环境(Windows11+Ubuntu22.04)可能会遇到一些权限问题。解决方案如下所示:
sudo mkdir -p /var/sandbox/sandbox-python && sudo cp internal/core/runner/python/python.so /var/sandbox/sandbox-python/
sudo mkdir -p /var/sandbox/sandbox-nodejs && sudo cp internal/core/runner/nodejs/nodejs.so /var/sandbox/sandbox-nodejs/
sudo chmod -R 777 /var/sandbox/sandbox-python
sudo chmod -R 777 /var/sandbox/sandbox-nodejs
sudo chmod -R 777 /usr/lib/x86_64-linux-gnu /etc/ssl/certs/ca-certificates.crt /etc/nsswitch.conf /etc/hosts /etc/resolv.conf /etc/localtime /usr/share/zoneinfo /etc/timezone
特别说明:以上sudo chmod -R 777
方式仅供本地调试学习使用。
(1)"/run/systemd/resolve/stub-resolv.conf"
由于sudo chmod -R 777
后,GoLand调试还是没有权限,因此注释(暂不清楚是否有后遗症)。
(2)"/run/resolvconf/resolv.conf"
由于没有这个文件,因此注释。
本地sandbox源码运行,如下所示:
新建一个极简的Chatflow
示例,测试代码节点,如下所示:
文件位置:dify\docker\volumes\sandbox\conf\config.yaml.example
这是一个沙盒环境的配置文件,用于定义应用程序的运行参数和限制。该配置文件主要用于设置一个受控的沙盒环境,确保应用程序在安全的环境中运行,同时提供必要的资源访问权限。
(1)应用程序基本配置
app.port: 8194
- 应用程序监听的端口号
app.debug: True
- 是否启用调试模式
app.key: dify-sandbox
- 应用程序的密钥标识
(2)资源和性能限制
max_workers: 4
- 最大工作线程数量
max_requests: 50
- 最大请求数量
worker_timeout: 5
- 工作线程超时时间(单位:秒)
(3)Python环境配置
python_path
- Python解释器的路径
python_lib_path
- Python库和系统文件的路径列表,包括:
Python库目录
SSL证书
系统配置文件
时区信息等
python_pip_mirror_url
- 使用清华大学的PyPI镜像源
(4)Node.js配置
nodejs_path: /usr/local/bin/node
- Node.js解释器的路径(5)安全和网络设置
enable_network: True
- 是否启用网络功能
allowed_syscalls
- 允许的系统调用ID列表
proxy
- 代理服务器设置(当前均为空)
SOCKS5代理
HTTP代理
HTTPS代理
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费场景POC验证,效果验证后签署服务协议。零风险落地应用大模型,已交付160+中大型企业
2025-07-04
将Dify对接到NextChat中的具体操作和原理
2025-07-03
dify 迎来重大更新!工作流调试工具也太实用了
2025-07-02
一文了解dify如何支持多agent架构
2025-06-30
基于Dify平台实现推荐问题点击交互功能的技术方案
2025-06-29
DifyAI关键技术深度剖析
2025-06-28
Dify1.5.0神级更新:变量监视+状态缓存,开发效率暴增200%!
2025-06-26
提升 Dify 集成 MCP 便利性与 Prompt 迭代效率的实践
2025-06-26
Dify 集成 Azure AI 内容安全插件:从部署到落地的全流程教程
2025-06-25
2025-06-04
2025-04-18
2025-04-28
2025-05-08
2025-06-03
2025-05-08
2025-04-14
2025-04-15
2025-04-14
2025-06-26
2025-06-17
2025-05-29
2025-05-28
2025-05-22
2025-04-27
2025-04-15
2025-03-20