docker-node icon indicating copy to clipboard operation
docker-node copied to clipboard

Document somewhere that npm can't install dependency without preparing the environment

Open serut opened this issue 7 years ago • 9 comments

Context

I had some serious issues to run my test on gitlab ci using your image node:7.10.1 and node:8.2.1.

Nothing is documented on the docker node webpage, so I though it was as simple as that (.gitlab.yml):

image: node

stages:
  - test

test:
  stage: test
  script:
   - npm install
   - npm test

To get the same bug as me, you need to define a postinstall script, which calls others script :

(1)    "postinstall": "npm run build:dll && npm run build:production-dll",
(3)    "build:production-dll": "npm run build:production-core-dll && npm run build:production-coreoss-dll",
(2)    "build:dll": "webpack --progress --config webpack.dev.dll.config.js --display-error-details",
(4)    "build:production-core-dll": "webpack --progress --config webpack.prod.core.dll.config.js --display-error-details",
(5)    "build:production-coreoss-dll": "webpack --progress --config webpack.prod.coreoss.dll.config.js --display-error-details",

Logs

On my CI logs, I can see these are executed :

  • npm install is well executed
  • postinstall is started
  • the first part, build:dll ends up correctly
  • then a simple WARN is printed but it does not stop the build npm WARN lifecycle [email protected]~postinstall: cannot run in wd %s %s (wd=%s) [email protected] npm run build:dll && npm run build:production-dll
  • build:production-dll is not called, neither build:production-core-dll and build:production-coreoss-dll And that's it, it starts to test with an unexpected environment.

WTF is happening ?

When you run npm run <smth> as root, you need to define unsafe-perm to true to avoid any issue.

Solution

There is two solutions so far :

  • create a new user and run tests using it
  • Add the following to your test file, which allows npm to be launched as root
   - echo "unsafe-perm=true" > ~/.npmrc 

Can you document that on the docker image ?

serut avatar Jul 25 '17 09:07 serut

We do include a node user which is recommend to use (USER node).

I'd love to see a PR improving our documentation in that area, though! Could you send one?

SimenB avatar Jul 25 '17 17:07 SimenB

Hmm, maybe later but I must be sure about what I am doing.

  • [ ] Create a PR to improve the README about user rights and a warning about npm and root.

When we mount the folder like what gitlab ci does, we have user / group rights on files. It looks like there is no problem with root, but I'm not sure that user node has rights well configured !

  • [x] Test that node user has access to my app folder while testing

Is this command cross platform (I'm not sure about node alpine) ? :

image: node

stages:
  - test

test:
  stage: test
  script:
   - su - node
   - cd comes/back/to/my/app
   - npm install
   - npm test

serut avatar Jul 26 '17 09:07 serut

Ok I tryed to use USER node instead of root and :

  • the workspace that gitlab mounts is owned by root (at least in terms of uid gid) with chmod 666, so there is no problem with rights.
  • ~~your USER node also has write access to /usr/local/lib/node_modules (no problem to install -g and npm link)~~ USER node can't use install -g and npm link
  • ~~However, we loose gitlab ci environment variables by switching to another user.~~
  • su is cross OS

What do you think about it ?

serut avatar Jul 26 '17 11:07 serut

We should also add an explanation about that log :

npm WARN lifecycle <module name>~<lifecycle step>: cannot run in wd %s %s (wd=%s)

Which basically means that your app have been misinstalled and will not work properly.

serut avatar Jul 28 '17 14:07 serut

your USER node also has write access to /usr/local/lib/node_modules (no problem to install -g and npm link)

Really? When I tried this with node:8.4.0 and node:8.4.0-alpine I get permission denied on that folder.

# Alpine
/ $ ls -l /usr/local/lib | grep node_modules
drwxr-xr-x    3 root     root          4096 Aug 15 22:21 node_modules

# Debian
node@d5958ee4adde:/$ ls -l /usr/local/lib | grep node_modules
drwxrwxr-x 3  500   500 4096 Aug 15 16:24 node_modules
node@1ae43601ee81:/$ id node
uid=1000(node) gid=1000(node) groups=1000(node)

gibfahn avatar Sep 11 '17 00:09 gibfahn

However, we loose gitlab ci environment variables by switching to another user.

It's odd that you're losing environment variables, they seem to be kept if you switch inside the image:

root@1ae43601ee81:/\# export foo=bar
root@1ae43601ee81:/\# su node
node@1ae43601ee81:/$ echo $foo
bar

gibfahn avatar Sep 11 '17 00:09 gibfahn

su -E preserves env var I believe

LaurentGoderre avatar Sep 11 '17 12:09 LaurentGoderre

It's odd that you're losing environment variables, they seem to be kept if you switch inside the image

Aaaalright ! That's my fault, I should have run su node instead of su - node. I tried the solution that propose @LaurentGoderre but su -E is not available on alpine :

/ # su -E node
su: unrecognized option: E
[..]
        -,-l    Clear environment, go to home dir, run shell as login shell

And I just end the research there. I saw somewhere that the user node was created and configured but no it's not configured to handle install -g and npm link ! Nice catch ! I will stroke everything that's not correct :S

serut avatar Sep 18 '17 20:09 serut

I saw somewhere that the user node was created and configured but no it's not configured to handle install -g and npm link ! Nice catch ! I will stroke everything that's not correct :S

That's true, but you can fix it by doing something like:

export npm_config_prefix=$HOME/.npm

after you su node.

gibfahn avatar Sep 19 '17 06:09 gibfahn