【问题标题】:npm install returns connection timeout while building docker imagenpm install 在构建 docker 映像时返回连接超时
【发布时间】:2020-06-15 06:08:41
【问题描述】:

我正在尝试制作包含 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=hostoption 构建图像;但在两个版本中都有相同的错误。

另外ping dl-cdn.alpinelinux.org 在我的系统上也能正常工作。

另外,重启了docker服务sudo systemctl restart docker,又试了一次。又得到了同样的结果。

【问题讨论】:

    标签: docker npm node-modules npm-install


    【解决方案1】:

    在我的情况下使用以下步骤:

    1. 在主机中,我更改了主机服务器 /etc/sysctl.conf:

    我加了

    net.ipv4.ip_forward=1
    
    1. 我用

      nslookup registry.npmjs.org

    给我 IP 104.16.25.38

    1. 使用 IP 构建 Docker

    码头工人建造。 --add-host registry.npmjs.org:104.16.25.38 -t test

    【讨论】:

    • 它似乎没有达到add nodejs 点。更新 apk 存储库时失败。顺便说一句,我已经尝试过您的解决方案,但它对我不起作用;我遇到了同样的错误。
    【解决方案2】:

    在我的情况下,使用以下 single 步骤,

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

    【讨论】:

    • 这和@Hernaldo 之前建议的完全一样。请在他的回答下阅读我的评论。
    猜你喜欢
    • 2020-10-14
    • 1970-01-01
    • 2021-02-14
    • 1970-01-01
    • 2019-12-15
    • 1970-01-01
    • 2021-05-08
    • 2015-09-09
    • 1970-01-01
    相关资源
    最近更新 更多