node.js docker:创建新用户后运行 npm install 时遇到问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21926425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
docker: having trouble running npm install after creating a new user
提问by fox
So I have another follow-up question regarding installing a node.js-based framework under Docker on CoreOS, per this post.
因此,根据这篇文章,我还有另一个关于在 CoreOS 上的 Docker 下安装基于 node.js 的框架的后续问题。
So, because npmis finicky about installing from package.jsonvia root, I've had to create a nonroot sudo user in order to install the package. This is what my Dockerfilecurrently looks like inside of our repo, building off of an ubuntu image:
所以,因为npm从package.jsonroot安装很挑剔,我不得不创建一个非 root sudo 用户来安装包。这是我Dockerfile目前在我们的 repo 中的样子,建立在一个 ubuntu 图像之上:
# Install dependencies and nodejs
RUN apt-get update
RUN apt-get install -y python-software-properties python g++ make
RUN add-apt-repository ppa:chris-lea/node.js
RUN apt-get update
RUN apt-get install -y nodejs
# Install git
RUN apt-get install -y git
# Bundle app source
ADD . /src
# Create a nonroot user, and switch to it
RUN /usr/sbin/useradd --create-home --home-dir /usr/local/nonroot --shell /bin/bash nonroot
RUN /usr/sbin/adduser nonroot sudo
RUN chown -R nonroot /usr/local/
RUN chown -R nonroot /usr/lib/
RUN chown -R nonroot /usr/bin/
RUN chown -R nonroot /src
USER nonroot
# Install app source
RUN cd /src; npm install
I know that this is an inelegant way to do things, but I'm otherwise at a loss as to how to complete an npm install here. When I try the above, I get errors on all of the packages as they try to install:
我知道这是一种不雅的做事方式,但我不知道如何在此处完成 npm 安装。当我尝试上述操作时,我在尝试安装所有软件包时都遇到错误:
Error: Attempt to unlock [email protected], which hasn't been locked
at unlock (/usr/lib/node_modules/npm/lib/cache.js:1304:11)
at cb (/usr/lib/node_modules/npm/lib/cache.js:646:5)
at /usr/lib/node_modules/npm/lib/cache.js:655:20
at /usr/lib/node_modules/npm/lib/cache.js:1282:20
at afterMkdir (/usr/lib/node_modules/npm/lib/cache.js:1013:14)
at /usr/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53
at Object.oncomplete (fs.js:107:15)
If you need help, you may report this *entire* log,
including the npm and node versions, at:
<http://github.com/npm/npm/issues>
...
Any thoughts as to how I should modify my Dockerfile? I can only assume that this is some permissioning issue having to do with the way that I've provisioned the nonrootuser above that might be particular to the Docker framework; I have no problem doing this kind of thing on just a vanilla ubuntu install, albeit not from script.
关于我应该如何修改我的 Dockerfile 的任何想法?我只能假设这是一些许可问题,与我在nonroot上面配置用户的方式有关,这可能是 Docker 框架特有的;我在普通的 ubuntu 安装上做这种事情没有问题,尽管不是来自脚本。
回答by fox
so it turns out that this may be an issue with docker.
所以事实证明这可能是docker 的问题。
was able to get around this by switching from USER nonrootto RUN /bin/su nonrootinstead, afterwards everything worked fine.
能够通过从 切换USER nonroot到RUN /bin/su nonroot来解决这个问题,之后一切正常。
回答by Rodrigo Cifuentes Gómez
I was getting the similar mistakes when trying to create any yeoman project and finnally found the solution :)
我在尝试创建任何自耕农项目时遇到了类似的错误,并最终找到了解决方案:)
I was getting that error because the owner of .npm folder in my home directory was "root" user so I used
我收到该错误是因为我的主目录中 .npm 文件夹的所有者是“root”用户,所以我使用了
sudo chown [username] .npm
and now I can use Yeoman and npm without errors :)
现在我可以毫无错误地使用 Yeoman 和 npm 了:)
Hope it helps!
希望能帮助到你!
回答by insitusec
I have a couple of suggestions for modifications/suggestions:
我有一些修改/建议的建议:
Cache your node modules earlier in the
Dockerfileusing something like this (put this right after theapt-get:ADD package.json /tmp/package.json RUN cd /tmp && npm install RUN mkdir -p /src && cp -a /tmp/node_modules /srcThis way if your application code changes you aren't rebuilding all your node modules each time. Put this before your
ADD . /src. More details/examples are available in my blog postarticle.You shouldn't need to worry about running things as root in your
Dockerfile... that is the default. Perhaps your problem isn't related to root but to the content inside your host's directory. Do you maybe need to clean out a lockfile from your code's dir?
在
Dockerfile使用类似的东西之前缓存你的节点模块(把它放在apt-get:ADD package.json /tmp/package.json RUN cd /tmp && npm install RUN mkdir -p /src && cp -a /tmp/node_modules /src这样,如果您的应用程序代码发生更改,您就不会每次都重新构建所有节点模块。把这个放在你的
ADD . /src. 更多细节/示例可在我的博客文章中找到。您无需担心以 root 身份运行东西
Dockerfile...这是默认设置。也许您的问题与 root 无关,而与主机目录中的内容有关。您可能需要从代码目录中清除锁定文件吗?

