Can't Install New Palettes: EACCES

I'm trying to get Node Red up and running in docker, but I can't install new palettes. It seems that the node installer is trying to place its modules in the root directory instead of in /data

sudo docker create \                           
  -p 1880:1880 \
  -v /usr/share/nodered/data:/data \
  --link mosquitto:mosquitto \
  --restart unless-stopped \
  --user 2003:2003 \
  --name nodered \
  nodered/node-red-docker:latest

If I try to install from the Manage Palettes area I get the following error:

-----------------------------------------------------------
2019-03-16T06:27:37.017Z Install : node-red-contrib-home-assistant 0.3.2
2019-03-16T06:27:37.054Z npm install --no-audit --no-update-notifier --save --save-prefix="~" --production node-red-contrib-home-assistant@0.3.2
2019-03-16T06:27:37.619Z [err] Unhandled rejection Error: EACCES: permission denied, mkdir '/.npm'
2019-03-16T06:27:37.619Z [err] npm ERR! cb() never called!
2019-03-16T06:27:37.620Z [err] 
2019-03-16T06:27:37.620Z [err] npm ERR!
2019-03-16T06:27:37.620Z [err]  This is an error with npm itself. Please report this error at:
2019-03-16T06:27:37.620Z [err] npm ERR!     <https://npm.community>
2019-03-16T06:27:37.622Z rc=1

And if I try to do it from within the container as suggested, I run into a similar error:

$ cd /data
I have no name!@def387831f9c:/data$ npm install node-red-contrib-home-assistant
Unhandled rejection Error: EACCES: permission denied, mkdir '/.npm'
npm ERR! cb() never called!
npm ERR! This is an error with npm itself. Please report this error at:
npm ERR!     <https://npm.community>

Did I miss something in my setup? Why is node trying to install into / instead of /data?

Did you try sudo npm install ... from the container ?

$ sudo npm install node-red-contrib-home-assistant-websocket
bash: sudo: command not found

Looks like sudo isn't baked into the docker image.

/usr/share/ is owned by root ?
Perhaps try to use a dir that is owned by the user you use to run docker container, if that is root then I don't know.

I made sure to set the permissions correctly, AFAICT:

/usr/share/nodered % ls -ahl
total 12K
drwxr-xr-x   3 nodered nodered 4.0K Mar 14 22:59 .
drwxr-xr-x 142 root    root    4.0K Mar 16 02:07 ..
drwxr-xr-x   3 nodered nodered 4.0K Mar 16 01:49 data

In fact, Node Red was able to write all of its initial configuration out without issues. The problem appears to be that npm wants to write its files to /.npm instead of somewhere inside /data/...

I am using docker-compose to get a couple of NR instances going. Have you tried docker-compose instead of the command line?

By the way, I figured out the problem. Using the --user 2003:2003 makes docker run as user 2003, but the docker file just create a new user (which ended up with id 1001, IIRC):

RUN useradd --home-dir /usr/src/node-red --no-create-home node-red \
  && chown -R node-red:node-red /data \
  && chown -R node-red:node-red /usr/src/node-red

The container mostly works, because the mapped volume -v /usr/share/nodered/data:/data is also owned by 2003:2003 in the host; I just can't install new npm modules.

I'm going to see about creating a sub-image that changes the internal node-red user to have a different uid.