【问题标题】:Docker blocked on NPM install - Socket TimeoutDocker 在 NPM 安装时被阻止 - 套接字超时
【发布时间】:2026-02-17 09:25:01
【问题描述】:

我有一个运行节点的简单 Dockerfile,这是配置:

FROM node:latest

WORKDIR /usr/src/auth-starter-server

COPY ./ ./

RUN npm install

CMD ["sh"]

在 docker-compose.yml 上使用的是这样的:

version: "3.8"

services:
  # Node Server
  auth-starter-server:
    container_name: server
    restart: unless-stopped
    build: ./
    command: npm run start:live
    working_dir: /usr/src/auth-starter-server
    ports:
      - "5000:5000"
    volumes:
      - ./:/usr/src/auth-starter-server

从一天到另一天,我都无法再构建图像了。它开始没有问题,

Creating network "react-auth-starter_default" with the default driver
Building auth-starter-server
Step 1/5 : FROM node:latest
 ---> 6f7f341ab8b8
Step 2/5 : WORKDIR /usr/src/auth-starter-server
 ---> Using cache
 ---> b25f7c08d3eb
Step 3/5 : COPY ./ ./
 ---> Using cache
 ---> 2a06e76bab32
Step 4/5 : RUN npm install
 ---> Running in 0034d6afa38e

但是当使用docker-compose up --build(或简单的docker build -t auth-starter-server .)构建时,它只会在“npm install”上超时并返回以下错误:

npm notice 
npm notice New minor version of npm available! 7.0.15 -> 7.3.0
npm notice Changelog: <https://github.com/npm/cli/releases/tag/v7.3.0>
npm notice Run `npm install -g npm@7.3.0` to update!
npm notice 
npm ERR! code ERR_SOCKET_TIMEOUT
npm ERR! errno ERR_SOCKET_TIMEOUT
npm ERR! request to https://registry.companyregistry.com/yargs-parser/-/yargs-parser-13.1.2.tgz failed, reason: Socket timeout

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-12-21T15_37_42_509Z-debug.log
ERROR: Service 'auth-starter-server' failed to build : The command '/bin/sh -c npm install' returned a non-zero code: 1

我注意到它没有使用官方的 npm 注册表,即使它还没有设置为默认注册表。这里是 npm 配置(通过运行 npm config ls -l 获得):

cli configs
long = true
metrics-registry = "https://registry.npmjs.org/"
scope = ""
user-agent = "npm/6.14.9 node/v15.4.0 darwin x64"

; builtin config undefined
prefix = "/usr/local"

; default values
access = null
allow-same-version = false
also = null
always-auth = false
audit = true
audit-level = "low"
auth-type = "legacy"
before = null
bin-links = true
browser = null
ca = null
cache = "/Users/ale917k/.npm"
cache-lock-retries = 10
cache-lock-stale = 60000
cache-lock-wait = 10000
cache-max = null
cache-min = 10
cafile = undefined
cert = null
cidr = null
color = true
commit-hooks = true
depth = null
description = true
dev = false
dry-run = false
editor = "vi"
engine-strict = false
fetch-retries = 2
fetch-retry-factor = 10
fetch-retry-maxtimeout = 60000
fetch-retry-mintimeout = 10000
force = false
format-package-lock = true
fund = true
git = "git"
git-tag-version = true
global = false
global-style = false
globalconfig = "/usr/local/etc/npmrc"
globalignorefile = "/usr/local/etc/npmignore"
group = 20
ham-it-up = false
heading = "npm"
https-proxy = null
if-present = false
ignore-prepublish = false
ignore-scripts = false
init-author-email = ""
init-author-name = ""
init-author-url = ""
init-license = "ISC"
init-module = "/Users/ale917k/.npm-init.js"
init-version = "1.0.0"
json = false
key = null
legacy-bundling = false
link = false
local-address = undefined
loglevel = "notice"
logs-max = 10
; long = false (overridden)
maxsockets = 50
message = "%s"
; metrics-registry = null (overridden)
node-options = null
node-version = "15.4.0"
noproxy = null
offline = false
onload-script = null
only = null
optional = true
otp = null
package-lock = true
package-lock-only = false
parseable = false
prefer-offline = false
prefer-online = false
; prefix = "/usr/local/Cellar/node/15.4.0" (overridden)
preid = ""
production = false
progress = true
proxy = null
read-only = false
rebuild-bundle = true
registry = "https://registry.npmjs.org/"
rollback = true
save = true
save-bundle = false
save-dev = false
save-exact = false
save-optional = false
save-prefix = "^"
save-prod = false
scope = ""
script-shell = null
scripts-prepend-node-path = "warn-only"
searchexclude = null
searchlimit = 20
searchopts = ""
searchstaleness = 900
send-metrics = false
shell = "/bin/zsh"
shrinkwrap = true
sign-git-commit = false
sign-git-tag = false
sso-poll-frequency = 500
sso-type = "oauth"
strict-ssl = true
tag = "latest"
tag-version-prefix = "v"
timing = false
tmp = "/var/folders/2t/36_q44_s62d1b57hnl0l8khw0000gn/T"
umask = 18
unicode = true
unsafe-perm = true
update-notifier = true
usage = false
user = 0
; user-agent = "npm/{npm-version} node/{node-version} {platform} {arch} {ci}" (overridden)
userconfig = "/Users/ale917k/.npmrc"
version = false
versions = false
viewer = "man"

它停止构建的原因可能是因为它指向了错误的注册表吗?如果是这样,如何解决?

另一方面,我也注意到有关 npm 更新的警告;如何更新 docker 镜像中的 npm 包?

非常感谢您的任何回答,感谢您的投入!

【问题讨论】:

    标签: docker npm


    【解决方案1】:

    我也遇到过同样的问题,后来意识到我的网络连接不好导致了 ERR_SOCKET_TIMEOUT。

    不是因为 运行npm install -g npm@7.3.0 进行更新!`。正如您在下面看到的,它仍在运行

    【讨论】:

    • 我实际上发现很多其他人有同样的问题,他们将问题指向连接,但事实并非如此(我没有强大的连接,但应该是足以运行此类进程)-我已经添加了帮助我解决此特定问题的答案,但是非常感谢您尝试提供帮助!
    • 在我的情况下,这是由于 npm install -g npm 删除它有帮助
    【解决方案2】:

    发现我公司的服务器出现故障,导致相关注册表和类似情况关闭。

    这导致无法处理软件包安装,因为出于某种原因,我似乎有来自公司自定义注册表的依赖项剩余。

    为了解决这个问题,我:

    1. 首先创建了不同的 npmrc 配置文件,以确保我们在处理特定项目时使用适当的注册表 - 可以找到参考 here

    2. 然后删除node_modulespackage-lock.json 以清理剩余部分

    3. 使用npmrc &lt;profile&gt;设置指向开源注册表(https://registry.npmjs.org/)的配置文件

    4. (可选):只需运行npmrc即可检查活动注册表配置文件

    5. npm install重新安装所有包

    6. 使用 docker-compose up --build 重建 docker-image 并完成了这项工作

    希望这对未来的其他人有所帮助

    【讨论】:

      【解决方案3】:

      Dockerfile 节点基础镜像版本 16 也有同样的问题,所以降级到 14 即可解决问题 而不是“节点:最新”使用“节点:14”

      【讨论】:

      • 这个!图像从来没有遇到过问题,但截至今天无法在本地构建它。降级到node:14 有帮助
      【解决方案4】:

      我是如何解决这个问题的。

      我注意到在package-lock.json 以这种方式修补后问题开始出现。

      当您尝试在 PC 上进行本地安装时,您会得到:

      pm WARN old lockfile 
      npm WARN old lockfile The package-lock.json file was created with an old version of npm,
      npm WARN old lockfile so supplemental metadata must be fetched from the registry.
      npm WARN old lockfile 
      npm WARN old lockfile This is a one-time fix-up, please be patient...
      npm WARN old lockfile 
      ....
      # installs packages and finish
      

      在 docker npm ci 上一直超时。

      解决方案

      注意:确保您在 PC 上运行最新的 npmnode 版本。

      sudo apt install npm nodejs 没有删减。

      升级节点版本:

      • 安装包:sudo apt install npm nodejs
      • 运行:curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
      • 然后:sudo apt-get install -y nodejs
      • 然后升级 NPM:sudo npm install -g npm@latest

      升级后重新安装如下软件包:

      • 删除package-lock.json
      • 删除节点模块
      • 使用npm install 重新安装软件包

      现在运行你的 docker。

      【讨论】:

        【解决方案5】:

        尝试添加 tty: true,类似这样

         backend:
            build: 
              context: ./backend
              dockerfile: Dockerfile
            
            tty: true
        

        【讨论】:

          【解决方案6】:

          我遇到了同样的问题,上述解决方案都没有奏效。我在 RUN npm run install 命令之前通过 Dockerfile 中的 package-lock.json 文件解决了它。 所以修改上面的Dockerfile如下解决它。

          FROM node:latest
          WORKDIR /usr/src/auth-starter-server
          COPY package.json ./
          COPY package-lock.json ./
          RUN npm install
          CMD ["sh"]
          

          我猜想在之前添加 package-lock.json 会极大地减少下载包和它的依赖项的时间。

          【讨论】:

          • 我认为你错过了从回购中复制其他所有内容?此外,package-lock.json 文件仅用于定义所有 npm 模块的版本控制,但您是对的,首先仅复制 package.json,然后安装,然后复制其他所有内容比问题上的公式更有效
          • 我建议对您的答案稍作更改,先使用COPY package*.json .,然后是RUN npm install,然后是COPY . .;如果你得到一个套接字超时,你可能试图复制你不应该复制的文件(例如节点模块);为了解决您的问题,请确保在您的根目录中有一个 .dockerignore,其中包含 node_modules 以及您认为不应传递给容器的所有其他内容