【发布时间】:2018-12-04 23:13:45
【问题描述】:
我在 Dockerfile 中运行 npm。
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 像代理一样?
有没有办法通过 docker 使用 npm 来避免这个我不知道的错误?
编辑
使用 shell 在容器内手动运行 npm install 的详细日志
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_modules/目录,问题可能出在复制行。你试过没有那个吗? -
@jannis 我没试过我的主机上没有安装节点
-
然后看看这是Docker特有的问题还是你的主机网络配置/不稳定的问题。