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

Zei*_*zar 5 npm node-modules docker npm-install

我正在尝试制作一个 docker 图像,包括newman. 我尝试使用 构建图像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
Run Code Online (Sandbox Code Playgroud)

完整的错误是:

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
Run Code Online (Sandbox Code Playgroud)

然后我尝试制作我的图像,alpine:3.8apk update. 这是我的第二个版本:

RUN apk update && apk add --no-cache nodejs npm
RUN npm install -g newman
Run Code Online (Sandbox Code Playgroud)

完整的错误日志是:

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
Run Code Online (Sandbox Code Playgroud)

我还尝试使用--network=host选项构建图像;但在两个版本中都出现了相同的错误。

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

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

Her*_*lez 2

就我而言,适用以下步骤:

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

我添加了

net.ipv4.ip_forward=1
Run Code Online (Sandbox Code Playgroud)
  1. 我用

    nslookupregistry.npmjs.org

给我IP 104.16.25.38

  1. Docker 使用 IP 构建

码头工人构建。--add-hostregistry.npmjs.org:104.16.25.38 -t 测试