docker-node
docker-node copied to clipboard
Document somewhere that npm can't install dependency without preparing the environment
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, neitherbuild:production-core-dll
andbuild: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 ?
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?
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
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 toinstall -g
andnpm link
)~~USER node
can't useinstall -g
andnpm link
- ~~However, we loose gitlab ci environment variables by switching to another user.~~
-
su
is cross OS
What do you think about it ?
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.
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)
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
su -E
preserves env var I believe
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
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
.