我正在尝试在Docker容器中安装软件包,但是prepare
脚本未运行。
以下是Dockerfile
,它复制了该问题:
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
上面的安装了nvm
并切换为使用node v10.12.0
和npm v6.4.1
,然后尝试直接从github安装可能会失败的软件包。
$ 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
docker命令的最后一行应该失败。
位于该存储库分支上的package.json
在这里:https://github.com/jcollard/d3-ng2-service/blob/jcollard/add-dist/package.json#L15
您会看到"prepare": "BREAK BREAK BREAK",
当我在docker容器外运行此命令时,将导致预期的错误:
$ npm install jcollard/d3-ng2-service#jcollard/add-dist
npm ERR! prepareGitDep 1>
npm ERR! prepareGitDep > d3-ng2-service@2.3.0 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! d3-ng2-service@2.3.0 prepare: `BREAK BREAK BREAK`
npm ERR! prepareGitDep npm ERR! spawn ENOENT
npm ERR! prepareGitDep npm ERR!
npm ERR! prepareGitDep npm ERR! Failed at the d3-ng2-service@2.3.0 prepare script.
npm ERR! prepareGitDep npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
我假设本地有一些配置通知npm
运行prepare
,但是我似乎找不到它。任何帮助将不胜感激。
谢谢!
答案 0 :(得分:3)
那是一个有趣的兔子洞。就是这个错误:https://github.com/npm/npm/issues/17346。 Prepare不能以root身份运行。您可以以非root用户身份运行容器,但是我只是在问题中使用了此修复程序。
我将您的最后一行更改为此
RUN source ~/.nvm/nvm.sh; npm config set unsafe-perm true; npm install jcollard/d3-ng2-service#jcollard/add-dist
现在它按预期失败。