首页 > 解决方案 > 不可靠的 npm 安装在 docker 容器中

问题描述

我在一个Dockerfile.

FROM node:9.11-alpine
COPY ./ /usr/src/app/
WORKDIR /usr/src/app/
RUN npm install && npm run build
ENTRYPOINT [ "npm", "run", "watch"]

当我这样做docker build .时,构建成功完成的机会很小。如果我在几分钟内定期运行相同的命令而不对我的Dockerfile或其他任何内容进行任何更改,则构建将成功。大多数时候我得到这个;

...
npm WARN tar ENOENT: no such file or directory, open '/usr/src/app/node_modules/.staging/bluebird-5b126a50/js/browser/bluebird.js'
npm ERR! code ENOTFOUND
npm ERR! errno ENOTFOUND
npm ERR! network request to https://registry.npmjs.org/json-bigint/-/json-bigint-0.2.3.tgz failed, reason: getaddrinfo ENOTFOUND registry.npmjs.org registry.npmjs.org:443
npm ERR! network This is a problem related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network 
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network '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/2018-06-26T08_18_44_426Z-debug.log

因为构建失败,我无法检查/root/.npm/_logs/2018-06-26T08_18_44_426Z-debug.log(至少据我所知)。该错误说它是一个连接问题,但在线 npm 存储库是否有某种限制?还是 docker 像代理一样行事?

有没有办法将 npm 与 docker 一起使用来避免我不知道的这个错误?

编辑

npm install使用 shell 在容器内手动运行的详细日志

1420 verbose type system
1421 verbose stack FetchError: request to https://registry.npmjs.org/lodash/-/lodash-4.17.10.tgz failed, reason: getaddrinfo ENOTFOUND registry.npmjs.org registry.npmjs.org:443
1421 verbose stack     at ClientRequest.req.on.err (/usr/local/lib/node_modules/npm/node_modules/pacote/node_modules/make-fetch-happen/node_modules/node-fetch-npm/src/index.js:68:14)
1421 verbose stack     at ClientRequest.emit (events.js:180:13)
1421 verbose stack     at TLSSocket.socketErrorListener (_http_client.js:395:9)
1421 verbose stack     at TLSSocket.emit (events.js:180:13)
1421 verbose stack     at emitErrorNT (internal/streams/destroy.js:64:8)
1421 verbose stack     at process._tickCallback (internal/process/next_tick.js:178:19)
1422 verbose cwd /usr/src/app
1423 verbose Linux 4.14.48-2-MANJARO
1424 verbose argv "/usr/local/bin/node" "/usr/local/bin/npm" "install"
1425 verbose node v9.11.2
1426 verbose npm  v5.6.0
1427 error code ENOTFOUND
1428 error errno ENOTFOUND
1429 error network request to https://registry.npmjs.org/lodash/-/lodash-4.17.10.tgz failed, reason: getaddrinfo ENOTFOUND registry.npmjs.org registry.npmjs.org:443
1430 error network This is a problem related to network connectivity.
1430 error network In most cases you are behind a proxy or have bad network settings.
1430 error network
1430 error network If you are behind a proxy, please make sure that the
1430 error network 'proxy' config is set properly.  See: 'npm help config'
1431 verbose exit [ 1, true ]

标签: node.jsdockernpm

解决方案


So the connection issue was indeed a local connection issue. I used a different wifi from a different location I have confirmed it is the wifi settings of the appartment I am in. Since it was my first time using npm and docker together, I couldn't rule out other issues...

As for logging, I have discovered two great docker options for finding logs even when a container fails to build;

docker logs <containerid>

and

docker cp <containerid>:/path/to/log.file ./local/path/of/log.file

which work on containers that aren't running. This has allowed me to debug issues much more efficiently without changing the Dockerfile.


推荐阅读