在 Docker 中运行时,`npm install` 导致`cb() never called!`

Posted

技术标签:

【中文标题】在 Docker 中运行时,`npm install` 导致`cb() never called!`【英文标题】:`npm install` results in `cb() never called!` when running in Docker 【发布时间】:2019-04-11 12:18:52 【问题描述】:

我有一个带有相当小的package.json 文件的项目:


    "name": "chilldev-web",
    "version": "2.1.0-SNAPSHOT",
    "description": "Client-side build tool for a project.",
    "license": "UNLICENSED",
    "private": true,
    "dependencies": 
        "internal-edge-render": "file:/root/.m2/repository/pl/chilldev/internal/internal-edge-render/0.1.2/internal-edge-render-0.1.2.tar.gz",
        "react": "16.6.1",
        "react-dom": "16.6.1",
        "react-helmet": "5.2.0",
        "director": "1.2.8"
    ,
    "devDependencies": 
        "typescript": "2.9.2",
        "browserify": "16.2.3",
        "gulp": "3.9.1",
        "tslint": "5.11.0",
        "tslint-react": "3.6.0",
        "cssnano": "4.1.7",
        "autoprefixer": "9.3.1",
        "envify": "4.1.0",
        "uglifyify": "5.0.1",
        "sassdoc": "2.5.1",
        "typedoc": "0.13.0",
        "gulp-typedoc": "2.2.0",
        "gulp-postcss": "8.0.0",
        "gulp-tslint": "8.1.3",
        "gulp-jscpd": "0.0.8",
        "gulp-sass": "4.0.2",
        "gulp-typescript": "4.0.2",
        "gulp-install": "1.1.0",
        "gulp-zip": "4.2.0",
        "gulp-concat": "2.6.1",
        "gulp-header": "2.0.5",
        "gulp-uglify": "3.0.1",
        "vinyl-source-buffer": "1.1.1",
        "@types/whatwg-streams": "0.0.7",
        "@types/google.analytics": "0.0.39",
        "@types/react": "16.4.18",
        "@types/react-dom": "16.0.9",
        "@types/react-helmet": "5.0.7",
        "@types/headroom": "0.7.31",
        "@types/aws-lambda": "8.10.15",
        "bourbon": "5.1.0",
        "susy": "3.0.5"
    

当使用 Node/NPM 的本地安装时,它可以正常工作(Node v11.0.0、NPM v6.4.1)来执行全新安装 (rm -rf node_modules && npm install)。

但是在运行 Dockerized 任务(例如影响 CI 管道)时,我收到了 cb() never called! 错误:

root@4b8396e40e22:/usr/src/app# npm install --loglevel=verbose                                                                                                                                                                                                                 
npm info it worked if it ends with ok                                                                                                                                                                                                                                          
npm verb cli [ '/usr/local/bin/node',                                                                                                                                                                                                                                          
npm verb cli   '/usr/local/bin/npm',                                                                                                                                                                                                                                           
npm verb cli   'install',                                                                                                                                                                                                                                                      
npm verb cli   '--loglevel=verbose' ]                                                                                                                                                                                                                                          
npm info using npm@6.4.1                                                                                                                                                                                                                                                       
npm info using node@v11.0.0                                                                                                                                                                                                                                                    
npm verb npm-session c4ce6e4ee1ed4de0                                                                                                                                                                                                                                          
npm info lifecycle chilldev-web@2.1.0-SNAPSHOT~preinstall: chilldev-web@2.1.0-SNAPSHOT
npm timing stage:loadCurrentTree Completed in 14ms                                                                                                                                                                                                                             
npm timing stage:loadIdealTree:cloneCurrentTree Completed in 0ms                                                                                                                                                                                                               
npm timing stage:loadIdealTree:loadShrinkwrap Completed in 899ms
npm timing stage:loadIdealTree:loadAllDepsIntoIdealTree Completed in 1603ms
npm timing stage:loadIdealTree Completed in 3006ms
npm timing stage:generateActionsToTake Completed in 390ms
npm verb correctMkdir /root/.npm/_locks correctMkdir not in flight; initializing
npm verb lock using /root/.npm/_locks/staging-af89dff005e44f9e.lock for /usr/src/app/node_modules/.staging
npm timing audit compress Completed in 108ms
npm info audit Submitting payload of 62503bytes
npm timing audit submit Completed in 1539ms
npm http fetch POST 200 https://registry.npmjs.org/-/npm/v1/security/audits/quick 1539ms
npm timing audit body Completed in 1ms
npm timing action:extract Completed in 6742ms
npm timing action:finalize Completed in 940ms
npm timing npm Completed in 14338ms
npm ERR! cb() never called!

npm ERR! This is an error with npm itself. Please report this error at:
npm ERR!     <https://npm.community>

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2018-11-07T19_09_16_380Z-debug.log

只需再次运行任务即可成功:

root@4b8396e40e22:/usr/src/app# npm install --loglevel=verbose
npm info it worked if it ends with ok
npm verb cli [ '/usr/local/bin/node',
npm verb cli   '/usr/local/bin/npm',
npm verb cli   'install',
npm verb cli   '--loglevel=verbose' ]
npm info using npm@6.4.1
npm info using node@v11.0.0
npm verb npm-session efbf901b4a03cead
npm info lifecycle chilldev-web@2.1.0-SNAPSHOT~preinstall: chilldev-web@2.1.0-SNAPSHOT
npm timing stage:loadCurrentTree Completed in 2177ms
npm timing stage:loadIdealTree:cloneCurrentTree Completed in 21ms
npm timing stage:loadIdealTree:loadShrinkwrap Completed in 1153ms
npm timing stage:loadIdealTree:loadAllDepsIntoIdealTree Completed in 1463ms
npm timing stage:loadIdealTree Completed in 3225ms
npm timing stage:generateActionsToTake Completed in 103ms
npm verb correctMkdir /root/.npm/_locks correctMkdir not in flight; initializing
npm verb lock using /root/.npm/_locks/staging-af89dff005e44f9e.lock for /usr/src/app/node_modules/.staging
npm verb unlock done using /root/.npm/_locks/staging-af89dff005e44f9e.lock for /usr/src/app/node_modules/.staging
npm timing stage:executeActions Completed in 16ms
npm timing stage:rollbackFailedOptional Completed in 0ms
npm info linkStuff chilldev-web@2.1.0-SNAPSHOT
npm info lifecycle chilldev-web@2.1.0-SNAPSHOT~install: chilldev-web@2.1.0-SNAPSHOT
npm info lifecycle chilldev-web@2.1.0-SNAPSHOT~postinstall: chilldev-web@2.1.0-SNAPSHOT
npm timing audit compress Completed in 25ms
npm info audit Submitting payload of 62503bytes
npm info lifecycle chilldev-web@2.1.0-SNAPSHOT~prepublish: chilldev-web@2.1.0-SNAPSHOT
npm info lifecycle chilldev-web@2.1.0-SNAPSHOT~prepare: chilldev-web@2.1.0-SNAPSHOT
npm timing stage:runTopLevelLifecycles Completed in 6162ms
npm verb saving []
npm verb shrinkwrap skipping write for package.json because there were no changes.
npm info lifecycle undefined~preshrinkwrap: undefined
npm info lifecycle undefined~shrinkwrap: undefined
npm verb shrinkwrap skipping write for package-lock.json because there were no changes.
npm info lifecycle undefined~postshrinkwrap: undefined
npm timing audit submit Completed in 2165ms
npm http fetch POST 200 https://registry.npmjs.org/-/npm/v1/security/audits/quick 2019ms
npm timing audit body Completed in 3ms
audited 4746 packages in 8.13s
found 6 vulnerabilities (2 low, 4 high)
run `npm audit fix` to fix them, or `npm audit` for details
npm verb exit [ 0, true ]
npm timing npm Completed in 8439ms
npm info ok 

以上命令在 Docker 镜像node:11.0.0 中执行。但是,当我尝试在不同的映像中安装 Node 时会出现相同的结果(例如,如果我使用 maven:3.5.2-jdk-8 来运行绑定了 npm install 的 Maven 项目)。

-编辑-

更多细节 - 为了解决 Docker 执行后的文件所有权问题,我使用bindfs

DIR=`mktemp -d`
sudo bindfs --create-for-user=`id -nu` --create-for-group=`id -ng` $PWD $DIR
docker run --rm -it -v $DIR:/usr/src/app -w /usr/src/app node:11.0.0 npm install

当我只使用 -v $PWD:/usr/src/app 而不是临时挂载点运行 Docker 时,一切正常!

【问题讨论】:

【参考方案1】:

您有一个正在尝试安装的本地依赖项。

"internal-edge-render": "file:/root/.m2/repository/pl/chilldev/internal/internal-edge-render/0.1.2/internal-edge-render-0.1.2.tar.gz"

Docker 不知道它的路径。从 npm 安装依赖项或将目录挂载到 docker 中。假设后者是没有选择的......

不幸的是,日志在这种情况下并没有真正的帮助。

【讨论】:

Docker 以正确公开目录的方式启动,正确解决了依赖关系。这不是原因 谢谢 - 在我的情况下,依赖项是从 git+ssh:// URL 安装的,但我忘记在 Docker 容器中安装 git【参考方案2】:

将 docker 网络设置为“主机”为我解决了这个问题和其他问题。

 docker build . --network host

【讨论】:

以上是关于在 Docker 中运行时,`npm install` 导致`cb() never called!`的主要内容,如果未能解决你的问题,请参考以下文章

当 Docker 桌面在 WSL2 中运行时,bash 中的长工作目录

在 docker 中运行时出现异常:org.postgresql.util.PSQLException:致命:用户“hamzabelmellouki”的密码验证失败

当 Java APM 代理在 Docker 中运行时,如何将 Java APM 代理连接到 APM 服务器?

为嵌套文件夹运行 npm install 的最佳方式?

当图像在 docker 机器中运行时,X11 转发不起作用,但如果图像在没有 docker 机器的情况下运行,则它可以正常工作

在 kubernetes 环境中运行时 nodemon 未启动