fix(docker): 禁用代理环境变量防止网络干扰

在 Docker 容器配置中禁用 HTTP/HTTPS 代理环境变量,防止代理设置干扰容器网络连接
同时优化 sandbox 工具的环境变量处理逻辑
This commit is contained in:
lintsinghua 2025-12-17 15:33:53 +08:00
parent 1a720f7072
commit f1796ca044
4 changed files with 52 additions and 11 deletions

View File

@ -109,6 +109,18 @@ class SandboxManager:
timeout = timeout or self.config.timeout timeout = timeout or self.config.timeout
# 禁用代理环境变量,防止 Docker 自动注入的代理干扰容器网络
no_proxy_env = {
"HTTP_PROXY": "",
"HTTPS_PROXY": "",
"http_proxy": "",
"https_proxy": "",
"NO_PROXY": "*",
"no_proxy": "*",
}
# 合并用户传入的环境变量(用户变量优先)
container_env = {**no_proxy_env, **(env or {})}
try: try:
# 创建临时目录 # 创建临时目录
with tempfile.TemporaryDirectory() as temp_dir: with tempfile.TemporaryDirectory() as temp_dir:
@ -131,7 +143,7 @@ class SandboxManager:
"/tmp": "rw,size=100m,mode=1777" "/tmp": "rw,size=100m,mode=1777"
}, },
"working_dir": working_dir or "/workspace", "working_dir": working_dir or "/workspace",
"environment": env or {}, "environment": container_env,
# 安全配置 # 安全配置
"cap_drop": ["ALL"], "cap_drop": ["ALL"],
"security_opt": ["no-new-privileges:true"], "security_opt": ["no-new-privileges:true"],
@ -222,14 +234,22 @@ class SandboxManager:
timeout = timeout or self.config.timeout timeout = timeout or self.config.timeout
try: # 禁用代理环境变量,防止 Docker 自动注入的代理干扰容器网络
# 🔥 清除代理环境变量的方式:在命令前添加 unset no_proxy_env = {
# 因为设置空字符串会导致工具尝试解析空 URI 而出错 "HTTP_PROXY": "",
unset_proxy_prefix = "unset HTTP_PROXY HTTPS_PROXY http_proxy https_proxy; " "HTTPS_PROXY": "",
wrapped_command = unset_proxy_prefix + command "http_proxy": "",
"https_proxy": "",
"NO_PROXY": "*",
"no_proxy": "*",
}
# 合并用户传入的环境变量(用户变量优先)
container_env = {**no_proxy_env, **(env or {})}
# 用户传入的环境变量 try:
container_env = env or {} # 清除代理环境变量:在命令前添加 unset双重保险
unset_proxy_prefix = "unset HTTP_PROXY HTTPS_PROXY http_proxy https_proxy ALL_PROXY all_proxy 2>/dev/null; "
wrapped_command = unset_proxy_prefix + command
# 准备容器配置 # 准备容器配置
container_config = { container_config = {
@ -247,10 +267,10 @@ class SandboxManager:
}, },
"tmpfs": { "tmpfs": {
"/home/sandbox": "rw,size=100m,mode=1777", "/home/sandbox": "rw,size=100m,mode=1777",
"/tmp": "rw,size=100m,mode=1777" # 🔥 添加 /tmp 目录供工具写入临时文件 "/tmp": "rw,size=100m,mode=1777" # 添加 /tmp 目录供工具写入临时文件
}, },
"working_dir": "/workspace", "working_dir": "/workspace",
"environment": container_env, # 🔥 用户传入的环境变量 "environment": container_env,
"cap_drop": ["ALL"], "cap_drop": ["ALL"],
"security_opt": ["no-new-privileges:true"], "security_opt": ["no-new-privileges:true"],
} }

View File

@ -90,6 +90,13 @@ services:
restart: unless-stopped restart: unless-stopped
ports: ports:
- "3000:80" - "3000:80"
environment:
# 禁用代理 - nginx 需要直连后端
- HTTP_PROXY=
- HTTPS_PROXY=
- http_proxy=
- https_proxy=
- NO_PROXY=*
depends_on: depends_on:
- backend - backend
networks: networks:

View File

@ -86,6 +86,13 @@ services:
restart: unless-stopped restart: unless-stopped
ports: ports:
- "3000:80" - "3000:80"
environment:
# 禁用代理 - nginx 需要直连后端
- HTTP_PROXY=
- HTTPS_PROXY=
- http_proxy=
- https_proxy=
- NO_PROXY=*
depends_on: depends_on:
- backend - backend
networks: networks:

View File

@ -85,6 +85,13 @@ services:
- ./frontend/nginx.conf:/etc/nginx/conf.d/default.conf:ro # 挂载 nginx 配置 - ./frontend/nginx.conf:/etc/nginx/conf.d/default.conf:ro # 挂载 nginx 配置
ports: ports:
- "3000:80" # Nginx 监听 80 端口 - "3000:80" # Nginx 监听 80 端口
environment:
# 禁用代理 - nginx 需要直连后端
- HTTP_PROXY=
- HTTPS_PROXY=
- http_proxy=
- https_proxy=
- NO_PROXY=*
depends_on: depends_on:
- backend - backend
networks: networks: