我在启动 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']
当我运行 docker build 命令时
$ docker build -t myapp .
我收到的错误:
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
我在 Ubuntu 20.4 上,它只是一个带有 server.js 文件的简单节点应用程序。如果您需要我提供任何其他帮助来修复它,请告诉我。提前感谢您的帮助!
答案1
处理与 registry.npmjs.org 的安全连接时可能出现错误
此外,错误输出中显示的步骤与您的 Dockerfile 不匹配(例如:缺少 的输出RUN ping -c 4 google.com
),也许您随后编辑了文件以检查 dns 解析是否正常,我相信如此,因为错误npm install
显示了服务器 ip。
为了更好地识别错误,您可以在 Dockerfile 中添加一个新步骤(之前RUN npm install
):
RUN curl -v https://registry.npmjs.com/
然后再次运行 build 命令,curl 将尝试连接到 url,-v
flag 将输出很多详细信息,包括 tls 握手。这将有助于了解实际发生的情况。
附注:
- 您可以添加
--no-cache
到 docker build 命令,它将强制 docker 执行 Dockerfile 中的每个命令(而不仅仅是新/更新命令之后的命令):docker build --no-cache -t myapp .
有时可能需要重新构建。 - 您可以添加
--pull
到 docker build 命令,它将强制 docker 获取基础映像的最新版本:docker build --pull -t myapp .
当您知道基础已更新时很有用。