Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm v6.4.1 not running `prepare` inside docker

I am trying to install a package inside of a docker container but the prepare script is not being run.

Here is a Dockerfile which replicates the issue:

FROM ubuntu:18.04
# Replace shell with bash so we can source files to use npm
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
RUN apt-get update && apt-get upgrade -y
RUN apt-get install wget git -y
RUN wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
RUN source ~/.nvm/nvm.sh; nvm install v10.12.0; nvm use v10.12.0
RUN mkdir -p /usr/app/
WORKDIR /usr/app/
RUN source ~/.nvm/nvm.sh; npm install jcollard/d3-ng2-service#jcollard/add-dist

The above installs nvm and switches to using node v10.12.0 and npm v6.4.1 before attempting to install a package directly from github which should fail.

$ docker build -t npm-hack:latest .
$ docker run --rm -it npm-hack:latest /bin/bash -c 'source ~/.nvm/nvm.sh; npm --version'
6.4.1
$ docker run --rm -it npm-hack:latest /bin/bash -c 'source ~/.nvm/nvm.sh; node --version'
v10.12.0

The final line of the docker command should fail.

The package.json located on that repository branch is here: https://github.com/jcollard/d3-ng2-service/blob/jcollard/add-dist/package.json#L15

You'll see "prepare": "BREAK BREAK BREAK",

When I run this outside of the docker container, this results in the expected error:

$ npm install jcollard/d3-ng2-service#jcollard/add-dist
npm ERR! prepareGitDep 1>
npm ERR! prepareGitDep > [email protected] prepare /home/jcollard/.npm/_cacache/tmp/git-clone-77d32f21
npm ERR! prepareGitDep > BREAK BREAK BREAK
npm ERR! prepareGitDep
npm ERR! prepareGitDep
npm ERR! prepareGitDep 2> npm WARN install Usage of the `--dev` option is deprecated. Use `--only=dev` instead.
npm ERR! prepareGitDep sh: 1: BREAK: not found
npm ERR! prepareGitDep npm ERR! file sh
npm ERR! prepareGitDep npm ERR! code ELIFECYCLE
npm ERR! prepareGitDep npm ERR! errno ENOENT
npm ERR! prepareGitDep npm ERR! syscall spawn
npm ERR! prepareGitDep npm ERR! [email protected] prepare: `BREAK BREAK BREAK`
npm ERR! prepareGitDep npm ERR! spawn ENOENT
npm ERR! prepareGitDep npm ERR!
npm ERR! prepareGitDep npm ERR! Failed at the [email protected] prepare script.
npm ERR! prepareGitDep npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

I'm assuming there is some configuration locally that is informing npm to run prepare but I can't seem to find it. Any help would be appreciated.

Thanks!

like image 874
Joe Avatar asked Dec 03 '22 19:12

Joe


1 Answers

That was an interesting rabbit hole. It is this bug: https://github.com/npm/npm/issues/17346. Prepare doesn't run as root. You could run the container as non-root, but I just used the fix in the issue.

I changed your last line to this

RUN source ~/.nvm/nvm.sh; npm config set unsafe-perm true; npm install jcollard/d3-ng2-service#jcollard/add-dist

Now it fails as expected.

like image 181
maxm Avatar answered Dec 31 '22 00:12

maxm