chatgpt-web icon indicating copy to clipboard operation
chatgpt-web copied to clipboard

docker模式下运行代理失败

Open mydaoyuan opened this issue 1 year ago • 2 comments

docker 镜像运行

FetchError: request to https://api.openai.com/v1/chat/completions failed, reason: getaddrinfo EAI_AGAIN http://127.0.0.1
    at ClientRequest.<anonymous> (/app/node_modules/.pnpm/[email protected]/node_modules/node-fetch/src/index.js:108:11)
    at ClientRequest.emit (node:events:525:35)
    at ClientRequest.emit (node:domain:489:12)
    at onerror (/app/node_modules/.pnpm/[email protected]/node_modules/agent-base/dist/src/index.js:117:21)
    at callbackError (/app/node_modules/.pnpm/[email protected]/node_modules/agent-base/dist/src/index.js:136:17)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  type: 'system',
  errno: undefined,
  code: undefined,
  erroredSysCall: undefined

image

打印了容器内部的变量传递正确。 并且可以访问代理。我本地的docker也可以正常代理运行。 经过一下午的排查,发现项目 proxy 的代码和 fetch failed (OpenAI API banned in certain countries like China) 稍有差异。 修改后成功访问。

mydaoyuan avatar Mar 10 '23 15:03 mydaoyuan

不是代理的问题,是你用法不对,Docker 内部不能用宿主机的 127.0.0.1 代理,除非你在 Docker 容器内部起了代理服务

whatwewant avatar Mar 11 '23 17:03 whatwewant

不是代理的问题,是你用法不对,Docker 内部不能用宿主机的 127.0.0.1 代理,除非你在 Docker 容器内部起了代理服务

感谢你的回复,我确实是使用了宿主机的代理。之前我也尝试过docker network,也没有通。最后使用了https-proxy-agent解决了问题。附带我的启动命令。

docker run --name chatgpt-web -d -p 3003:3002 \
    --env OPENAI_API_KEY=xxxxxxx \
    --env SOCKS_PROXY_HOST=host.docker.internal \
    --env SOCKS_PROXY_PORT=7890 \
    --env http_proxy=http://host.docker.internal:7890 \
    --env https_proxy=http://host.docker.internal:7890 \
    --add-host=host.docker.internal:host-gateway \
    chatgpt-web

测试代理

2fa449edb0ad:/app# curl -x http://host.docker.internal:7890 google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>


curl 'http://localhost:3002/chat-process' \
    -H 'authority: localhost' \
    -H 'accept: application/json, text/plain, */*' \
    -H 'accept-language: zh-CN,zh;q=0.9,en;q=0.8' \
    -H 'cache-control: no-cache' \
    -H 'content-type: application/json' \
    -H 'cookie: Hm_lvt_968167d96042a9140d70a3daeb85e692=1677484732,1678441390; Hm_lpvt_968167d96042a9140d70a3daeb85e692=1678441390' \
    -H 'dnt: 1' \
    -H 'origin: http://localhost:3002' \
    -H 'pragma: no-cache' \
    -H 'referer: http://localhost:3002/' \
    -H 'sec-ch-ua: "Chromium";v="110", "Not A(Brand";v="24", "Google Chrome";v="110"' \
    -H 'sec-ch-ua-mobile: ?0' \
    -H 'sec-ch-ua-platform: "macOS"' \
    -H 'sec-fetch-dest: empty' \
    -H 'sec-fetch-mode: cors' \
    -H 'sec-fetch-site: same-origin' \
    -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36' \
    --data-raw '{"prompt":"测试","options":{}}' \
    --compressed

{"role":"assistant","id":"chatcmpl-6t3mbVO4NGfeZ6q7MbCR9EhoXClC7","parentMessageId":"d7abf426-1dff-40d5-86ca-4c9b607ddb95","text":"您好,您似乎在测试这个系统的功能。有什么问题我","delta":"我","detail":{"id":"chatcmpl-6t3mbVO4NGfeZ6q7MbCR9EhoXClC7","object":"chat.completion.chunk","created":1678580601,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"我"},"index":0,"finish_reason":null}]}}

我平时 dokcer 用的不多,不知道使用 宿主机的 127.0.0.1 代理 有什么弊端吗?目前文档中使用的代理方式也是通过 --env传参传入容器内部的。

mydaoyuan avatar Mar 12 '23 00:03 mydaoyuan