npm install在构建docker镜像时返回连接超时错误。

9

我正在尝试制作一个包括newman的docker镜像。我试图使用node:10-alpine构建镜像,但是在执行npm install -g newman时出现连接被拒绝的错误。这是我的docker命令:

RUN set http_proxy= && \
    set https_proxy= && \
    yarn config delete proxy && \
    npm config rm https-proxy && \
    npm config rm proxy && \
    npm config set registry http://registry.npmjs.org/ && \
    npm config set strict-ssl false && \
    npm cache clean --force && \
    npm cache verify && \
    npm install -g newman

完整的错误信息如下:
npm ERR! code ECONNREFUSED
npm ERR! errno ECONNREFUSED
npm ERR! FetchError: request to https://registry.npmjs.org/newman failed, reason: connect ECONNREFUSED 192.168.14.109:1087
npm ERR!     at ClientRequest.req.on.err (/usr/local/lib/node_modules/npm/node_modules/node-fetch-npm/src/index.js:68:14)
npm ERR!     at ClientRequest.emit (events.js:198:13)
npm ERR!     at onerror (/usr/local/lib/node_modules/npm/node_modules/agent-base/index.js:101:9)
npm ERR!     at callbackError (/usr/local/lib/node_modules/npm/node_modules/agent-base/index.js:123:5)
npm ERR!     at process._tickCallback (internal/process/next_tick.js:68:7)
npm ERR!  { FetchError: request to https://registry.npmjs.org/newman failed, reason: connect ECONNREFUSED 192.168.14.109:1087
npm ERR!     at ClientRequest.req.on.err (/usr/local/lib/node_modules/npm/node_modules/node-fetch-npm/src/index.js:68:14)
npm ERR!     at ClientRequest.emit (events.js:198:13)
npm ERR!     at onerror (/usr/local/lib/node_modules/npm/node_modules/agent-base/index.js:101:9)
npm ERR!     at callbackError (/usr/local/lib/node_modules/npm/node_modules/agent-base/index.js:123:5)
npm ERR!     at process._tickCallback (internal/process/next_tick.js:68:7)
npm ERR!   message:
npm ERR!    'request to https://registry.npmjs.org/newman failed, reason: connect ECONNREFUSED 192.168.14.109:1087',
npm ERR!   type: 'system',
npm ERR!   errno: 'ECONNREFUSED',
npm ERR!   code: 'ECONNREFUSED',
npm ERR!   stack:
npm ERR!    'FetchError: request to https://registry.npmjs.org/newman failed, reason: connect ECONNREFUSED 192.168.14.109:1087\n    at ClientRequest.req.on.err (/usr/local/lib/node_modules/npm/node_modules/node-fetch-npm/src/index.js:68:14)\n    at ClientRequest.emit (events.js:198:13)\n    at onerror (/usr/local/lib/node_modules/npm/node_modules/agent-base/index.js:101:9)\n    at callbackError (/usr/local/lib/node_modules/npm/node_modules/agent-base/index.js:123:5)\n    at process._tickCallback (internal/process/next_tick.js:68:7)' }
npm ERR! 
npm ERR! If you are behind a proxy, please make sure that the
npm ERR! 'proxy' config is set properly.  See: 'npm help config'

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-03-02T14_57_46_444Z-debug.log

我尝试使用 alpine:3.8 创建我的镜像,但当执行 apk update 时出现仓库错误。这是我的第二个版本:

RUN apk update && apk add --no-cache nodejs npm
RUN npm install -g newman

完整的错误日志如下:

Step 5/7 : RUN apk update && apk add --no-cache nodejs npm
 ---> Running in 6149b2571389
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/main/x86_64/APKINDEX.tar.gz
ERROR: http://dl-cdn.alpinelinux.org/alpine/v3.8/main: operation timed out
WARNING: Ignoring APKINDEX.adfa7ceb.tar.gz: No such file or directory
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/community/x86_64/APKINDEX.tar.gz
ERROR: http://dl-cdn.alpinelinux.org/alpine/v3.8/community: operation timed out
WARNING: Ignoring APKINDEX.efaa1f73.tar.gz: No such file or directory
2 errors; 13 distinct packages available
The command '/bin/sh -c apk update && apk add --no-cache nodejs npm' returned a non-zero code: 2

我还尝试使用--network=host选项构建镜像;但两个版本都遇到了相同的错误。

另外,ping dl-cdn.alpinelinux.org在我的系统上运行良好。

此外,重新启动了docker服务sudo systemctl restart docker,然后再次尝试。但是结果仍然相同。

2个回答

2
在我的情况下,以下步骤适用:
  1. 在主机上,我更改了主机服务器/etc/sysctl.conf:

我添加了以下内容:

net.ipv4.ip_forward=1
  1. 我使用

    nslookup registry.npmjs.org

给我IP 104.16.25.38

  1. Docker构建带有IP

docker build . --add-host registry.npmjs.org:104.16.25.38 -t test


似乎它没有到达“添加nodejs”点。它在更新“apk”存储库时失败了。顺便说一下,我尝试了你的解决方案,但对我没有用;我得到了相同的错误。 - Zeinab Abbasimazar

1
在我的案例中,以下单一步骤有效: docker build --add-host registry.npmjs.org:104.16.25.38 -t .

这与@Hernaldo之前建议的完全相同。请阅读他答案下面的我的评论。 - Zeinab Abbasimazar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接