Docker: problem starting npm after creating a new user

So, I have one more question on how to install node.js structure in Docker on CoreOS, for this post .

So, since npm is subtle when installing from package.json root, I had to create a nonroot sudo user to install the package. This is what my Dockerfile currently looks inside our repo, creating 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 this is an inelegant way of doing something, but I don't understand how to complete the npm installation. When I try to do this, I get errors in all packages when they try to install:

  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> ... 

Any thoughts on how I can modify my Docker file? I can only assume that this is a definite problem with what I provided to the nonroot user above, which can be especially important for the Docker platform; I have no problem doing such things only when installing vanilla ubuntu, although not from the script.

+8
docker npm ubuntu coreos
source share
3 answers

so it turns out that it could be a docker problem .

was able to get around this by switching from USER nonroot to RUN /bin/su nonroot , and then everything worked fine.

+9
source share

I got similar errors when trying to create any yoman project and eventually found a solution :)

I was getting this error because the owner of the .npm folder in my home directory was the "root" user, so I used

 sudo chown [username] .npm 

and now I can use Yeoman and npm without errors :)

Hope this helps!

+8
source share

I have several suggestions for changes / suggestions:

  • Load the node modules earlier in the Dockerfile using something like this (put this right after apt-get :

     ADD package.json /tmp/package.json RUN cd /tmp && npm install RUN mkdir -p /src && cp -a /tmp/node_modules /src 

    That way, if your application code changes, you do not rebuild all of your node modules every time. Put this in front of your ADD . /src ADD . /src . More details / examples are available in my blog post .

  • You don’t need to worry that you are performing actions as root in your Dockerfile ..., which is the default. Perhaps your problem is not with the root, but with the content inside your host directory. Perhaps you need to clear the lock file from your code directory?

+5
source share

All Articles