Dockerfile/npm: create a sudo user to run npm inst

2019-02-25 08:51发布

Creating a Dockerfile to install a node framework that we've created (per my earlier post here):

# 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

The problem is that npm expects to be run not as root -- is there a way to chain a series of sudo useradd commands to create a temp user that has sudo privileges that I can then switch to USER to run the npm install?

EDIT: updated the above, now getting this issue after successfuly creating a user and getting to the npm install line and choking:

 Error: Attempt to unlock javascript-brunch@1.7.1, 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>

3条回答
Anthone
2楼-- · 2019-02-25 09:07

I came across a similar npm install error when I was trying to execute is as a non-root user in my Dockerfile. Svante's explanation of the issue is bang on, npm does some caching under the $HOME dir. Here's a simple Dockerfile that works with npm install:

FROM dockerfile/nodejs

# Assumes you have a package.json in the current dir
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 chown -R nonroot /src

# Switch to our nonroot user
USER nonroot

# Set the HOME var, npm install gets angry if it can't write to the HOME dir, 
# which will be /root at this point
ENV HOME /usr/local/nonroot

# Install app source
WORKDIR /src
RUN npm install
查看更多
神经病院院长
3楼-- · 2019-02-25 09:23

The "Attempt to unlock" issue is often caused by not having the environment variable HOME set properly. npm needs this to be set to a directory that it can edit (it sets up and manages an .npm directory there).

You can specify environment variables in your docker run call with e. g. docker run -e "HOME=/home/docker".

查看更多
够拽才男人
4楼-- · 2019-02-25 09:26

To solve your "Attempt to unlock" issue, try cleaning the npm cache first by issuing

npm cache clean

After that, run

npm install
查看更多
登录 后发表回答