不可靠的npm安装在docker容器中

时间:2018-06-26 09:26:25

标签: node.js docker npm

我正在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使用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 ]

2 个答案:

答案 0 :(得分:0)

为什么不将docker build与入口点分开,将docker run与入口点分开?

尝试:

Dockerfile

FROM node:9.11-alpine
COPY ./ /usr/src/app/
WORKDIR /usr/src/app/
RUN npm install && npm run build

然后,创建图像:

docker build -t your_image_name .

以网络模式=主机运行命令,以避免与网络有关的某些问题:

docker run -d --net=host your_image_name npm run watch

如果它不起作用,则可以通过两种方式检查日志:

  1. 启动容器共享卷:docker run -d -v /tmp/any_dir:/root/.npm/_logs/ ...:可从主机获取。
  2. 使用sh启动容器,进入容器,手动执行命令并检查日志文件:docker run -ti your_image_name sh

答案 1 :(得分:0)

因此,连接问题确实是本地连接问题。我从其他位置使用了不同的wifi,我已经确认这是我所在公寓的wifi设置。由于这是我第一次同时使用npm和docker,所以我不能排除其他问题...

关于日志记录,我发现了两个很棒的docker选项,即使在容器无法构建时也可以找到日志;

docker logs <containerid>

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

适用于未运行的容器。这使我可以更有效地调试问题,而无需更改Dockerfile