在docker容器

时间:2017-09-08 10:04:04

标签: node.js docker npm ubuntu-16.04

我需要使用节点v6.10.3创建一个docker容器,但是使用最新的npm(当前为v5.4.1)来为本地包使用新的npm功能。

这样的安装在我的Mac上运行没有任何问题,但是当我尝试使用这样的安装创建一个docker镜像时,在更新npm之后,npm工具被破坏并抛出一堆关于丢失包的错误。

以下是Dockerfile的示例,我可以使用它重现此问题(请注意,我的真实Dockerfile更复杂):

FROM ubuntu:xenial

RUN apt-get update
RUN apt-get install -y curl

RUN curl -sL https://deb.nodesource.com/setup_6.x | bash -
RUN apt-get install -y nodejs

RUN npm i -g npm
RUN npm i -g lerna

当构建过程到达行RUN npm i -g lerna时,它会抛出一堆错误,如:

Error: Cannot find module 'process-nextick-args'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/usr/lib/node_modules/npm/node_modules/readable-stream/lib/_stream_readable.js:26:23)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)

任何其他npm脚本都会导致相同的错误。重新安装npm所依赖的所有软件包对我来说似乎不是一个解决方案。

我还尝试使用nvm在容器内安装节点,但我遇到了同样的错误。

我的码头版:

Docker version 17.06.2-ce, build cec0b72

这个Dockerfile有什么问题,我错过了什么?

1 个答案:

答案 0 :(得分:2)

我找到了使用yarn解决此问题的方法。

看起来很奇怪,但确实有效:

FROM ubuntu:xenial

RUN apt-get update
RUN apt-get install -y curl

RUN curl -sL https://deb.nodesource.com/setup_6.x | bash -
RUN apt-get install -y nodejs

RUN npm i -g yarn
RUN npm uninstall npm -g
RUN yarn global add npm
RUN npm i -g lerna

如果有人可以解释为什么原始解决方案不起作用,和/或帮助找到更好的解决方法,那将会很棒。