docker: having trouble running npm install after creating a new user
Solution 1
so it turns out that this may be an issue with docker.
was able to get around this by switching from USER nonroot
to RUN /bin/su nonroot
instead, afterwards everything worked fine.
Solution 2
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
sudo chown [username] .npm
and now I can use Yeoman and npm without errors :)
Hope it helps!
Solution 3
I have a couple of suggestions for modifications/suggestions:
-
Cache your node modules earlier in the
Dockerfile
using 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 /src
This 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 post article. 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?
fox
Updated on July 31, 2022Comments
-
fox almost 2 years
So I have another follow-up question regarding installing a node.js-based framework under Docker on CoreOS, per this post.
So, because
npm
is finicky about installing frompackage.json
via root, I've had to create a nonroot sudo user in order to install the package. This is what myDockerfile
currently looks like inside of our repo, building off of an ubuntu image:# 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:
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
nonroot
user 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. -
fox about 10 yearsalso the
/src
dir was the only one that I needed permissions on -
Naor about 9 yearsWhy do we need to copy package.json to a temporary folder and not directly to /src?
-
user2688473 about 9 yearsDockerfile builds check each step to see if anything has changed - if nothing has changed, it can reuse a cached build step. If you do the
package.json
first as a separate step, if your code has changed but your package.json has not, it won't have to reinstall yournode_modules
. -
Steve almost 8 yearsvery helpful - to go along with this - here is a good article on this approach: bitjudo.com/blog/2014/03/13/…
-
redOctober13 about 5 yearsRather than moving to /src, just
COPY package*.json ./
early, thenRUN npm install
, then copy everything else withCOPY . .
That way, if no packages changed, you don't need to reinstall all npm packages.