在Docker中运行时,npm install导致cb()从不调用!

时间:2018-11-08 01:00:26

标签: node.js docker npm package.json

我有一个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时,一切正常!

2 个答案:

答案 0 :(得分:0)

您具有要安装的本地依赖项。

"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。假设后者是不可行的...

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

答案 1 :(得分:0)

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

 docker build . --network host