使用代码执行节点,用python3对接SQL Server

需要先到Dify的 dify-sandbox:<版本号>容器内部安装pyodbc模组。docker exec直接进入容器安装 可能是一次性的,容器重启重构之后 就没了。所以需要将pyodbc环境写在docker-compose.yaml文件里,这样就能永久存在。

1、创建一个 Dockerfile(放在 sandbox 目录)

进入dify的docker文件夹下,创建一个dockerfile文件,比如sandbox.Dockerfile

内容如下,顺便装下常用的网络工具(别问为什么装=.= ,装就完事了)

FROM langgenius/dify-sandbox:0.2.11

# 安装 pyodbc 依赖、常用网络工具、pip 国内加速源
RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple && \
    apt-get update && \
    apt-get install -y --no-install-recommends \
        gcc \
        g++ \
        unixodbc-dev \
        iproute2 \
        iputils-ping \
        net-tools \
        dnsutils \
        curl && \
    pip install pyodbc && \
    apt-get clean && rm -rf /var/lib/apt/lists/*

2、修改 docker-compose.yaml 中的 sandbox 配置

使用/  # The DifySandbox 搜索找到sandbox配置

image: 改为 build:,指定新写的 Dockerfile

  sandbox:
    build:
      context: .
      dockerfile: sandbox.Dockerfile
    restart: always
    environment:
      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

3、重新构建并启动容器

docker-compose build sandbox
docker-compose up -d sandbox

4、修改docker-compose.yaml中internal: true配置,true改成false

internal: true 的意思是不允洗容器访问外部网络,会导致sandbox无法访问我们外部的数据库

保存后重启dify

docker compose down
docker compose up -d

进入容器内测试,到外部网络已经通了