npm 安装时 docker 构建失败,原因:connect ECONNREFUSED 104.16.17.35:443

Blo*_*gic 1 ubuntu node.js docker

我在启动 Docker 容器时遇到了困难,它在控制台中显示了一些奇怪的错误,因为我是新手,所以我对它的线索为零:

Dockerfile:

FROM node:14

WORKDIR /usr/src/app/

COPY package.json package.json
COPY server.js server.js

RUN ping -c 4 google.com
RUN  npm config set registry https://registry.npmjs.com/
RUN echo "${http_proxy}" && echo "${HTTP_PROXY}" 

RUN npm install
# COPY . .

EXPOSE 3000

CMD ['npm', 'start']
Run Code Online (Sandbox Code Playgroud)

当我运行 docker build 命令时

$ docker build -t myapp .
Run Code Online (Sandbox Code Playgroud)

我收到的错误:

Sending build context to Docker daemon  947.7kB
Step 1/7 : FROM node:14
 ---> 7bef16bb2cf1
Step 2/7 : WORKDIR /usr/src/app/
 ---> Using cache
 ---> 90402606c386
Step 3/7 : COPY package.json ./
 ---> Using cache
 ---> b839b81ee876
Step 4/7 : RUN npm install
 ---> Running in 64378581f715
npm ERR! code ECONNREFUSED
npm ERR! errno ECONNREFUSED
npm ERR! FetchError: request to https://registry.npmjs.org/@slack%2fevents-api failed, reason: connect ECONNREFUSED 104.16.18.35:443
npm ERR!     at ClientRequest.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/node-fetch-npm/src/index.js:68:14)
npm ERR!     at ClientRequest.emit (events.js:315:20)
npm ERR!     at TLSSocket.socketErrorListener (_http_client.js:469:9)
npm ERR!     at TLSSocket.emit (events.js:315:20)
npm ERR!     at emitErrorNT (internal/streams/destroy.js:106:8)
npm ERR!     at emitErrorCloseNT (internal/streams/destroy.js:74:3)
npm ERR!     at processTicksAndRejections (internal/process/task_queues.js:80:21)
npm ERR!  FetchError: request to https://registry.npmjs.org/@slack%2fevents-api failed, reason: connect ECONNREFUSED 104.16.18.35:443
npm ERR!     at ClientRequest.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/node-fetch-npm/src/index.js:68:14)
npm ERR!     at ClientRequest.emit (events.js:315:20)
npm ERR!     at TLSSocket.socketErrorListener (_http_client.js:469:9)
npm ERR!     at TLSSocket.emit (events.js:315:20)
npm ERR!     at emitErrorNT (internal/streams/destroy.js:106:8)
npm ERR!     at emitErrorCloseNT (internal/streams/destroy.js:74:3)
npm ERR!     at processTicksAndRejections (internal/process/task_queues.js:80:21) {
npm ERR!   type: 'system',
npm ERR!   errno: 'ECONNREFUSED',
npm ERR!   code: 'ECONNREFUSED'
npm ERR! }
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/2021-03-02T08_20_08_546Z-debug.log
The command '/bin/sh -c npm install' returned a non-zero code: 1
Run Code Online (Sandbox Code Playgroud)

我使用的是 Ubuntu 20.4,它只是一个带有 server.js 文件的简单节点应用程序。如果您需要我提供任何其他信息来修复它,请告诉我。预先感谢您的帮助!

https://i.stack.imgur.com/Nl7g1.png

Blo*_*gic 5

docker 网络、代理或防火墙似乎有问题。它可以通过指定来修复--network="host"

$ docker build -t myapp . --network="host"
Run Code Online (Sandbox Code Playgroud)

繁荣它开始像魅力一样发挥作用!