Docker 容器内的 npm 安装失败

npm install failing inside of Docker container

我正在尝试让 npm install 在我系统上的 Docker 个容器内工作...

我试过以下容器:

而且他们都有同样的错误。当我尝试安装 npm 包时,它会出现如下所示的错误:

# npm install -g typescript                                                                                                                                                                                      
npm ERR! Unexpected end of JSON input while parsing near '...ipt":"next","tsd":"la'                                                                                                                                

npm ERR! A complete log of this run can be found in:                                                                                                                                                               
npm ERR!     /root/.npm/_logs/2020-04-09T03_42_16_003Z-debug.log

如果我 运行 类似 npm cache clean --force 的东西,并且 运行 再次安装命令,它会在解析 JSON 的其他时候失败。

有趣的是,有时如果它是一个非常小的包,它会毫无问题地安装它:

# npm install padleft                                                                                                                                                                                   [12/1883]
npm WARN saveError ENOENT: no such file or directory, open '/package.json'
npm WARN notsup Unsupported engine for padleft@0.2.0: wanted: {"node":"6.3.1","npm":"3.10.6"} (current: {"node":"12.14.1","npm":"6.13.4"})
npm WARN notsup Not compatible with your version of node/npm: padleft@0.2.0
npm WARN enoent ENOENT: no such file or directory, open '/package.json'
npm WARN !invalid#1 No description
npm WARN !invalid#1 No repository field.
npm WARN !invalid#1 No README data
npm WARN !invalid#1 No license field.

+ padleft@0.2.0
added 1 package from 1 contributor and audited 2 packages in 1.472s
found 0 vulnerabilities

如果下载速度不快就无法安装,这几乎就像是失败了。我用 nvm 尝试了 node/npm 的不同版本,它们都做同样的事情。在我的主机上,npm 工作正常。只是在所有 Docker 个容器中都有问题。我只是在基于 Ubuntu.

的另一个 Docker 容器上安装了 nvm 版本失败了

我只能猜测 Container 正在丢弃数据包。

尝试向容器中添加更多 CPU。

https://docs.docker.com/config/containers/resource_constraints/

如果您愿意尝试其他事情:如果您没有选择 "host mode networking" 试试看。

https://docs.docker.com/network/host/