在我的NodeJS docker容器中找不到npm命令

时间:2016-11-20 13:51:46

标签: node.js docker nvm

我创建了一个Docker镜像:

$ docker build -t stephaneeybert/nodejs .
Sending build context to Docker daemon  2.56 kB
Step 1 : FROM debian
 ---> 1b088884749b
Step 2 : RUN apt-get clean && apt-get update
 ---> Using cache
 ---> b12133d6342f
Step 3 : RUN apt-get install -y curl
 ---> Using cache
 ---> 22dfb4882b12
Step 4 : RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
 ---> Using cache
 ---> 27f2fac45254
Step 5 : RUN . ~/.nvm/nvm.sh; nvm install stable
 ---> Using cache
 ---> 20d99d545755
Step 6 : RUN . ~/.nvm/nvm.sh; nvm use stable
 ---> Using cache
 ---> 9ec14efb2407
Step 7 : RUN . ~/.nvm/nvm.sh; npm install -g npm
 ---> Using cache
 ---> d264d38565f3
Step 8 : EXPOSE 9001
 ---> Using cache
 ---> 29e3589557e1
Step 9 : ENTRYPOINT /usr/bin/tail -f /dev/null
 ---> Using cache
 ---> 2ce499300fe1
Successfully built 2ce499300fe1

图像脚本是:

FROM debian

RUN apt-get clean && apt-get update
RUN apt-get install -y curl

# Installing nodesjs

RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash

RUN . ~/.nvm/nvm.sh; nvm install stable
RUN . ~/.nvm/nvm.sh; nvm use stable
RUN . ~/.nvm/nvm.sh; npm install -g npm

EXPOSE 9001

ENTRYPOINT ["/usr/bin/tail", "-f", "/dev/null"]

然后我运行容器并打开一个bash shell:

$ docker run -d -p 127.0.0.1:9001:9001 --name nodejs stephaneeybert/nodejs
c6dddf0a5eb0f11c897f63910eb01f2868fe0f39a80e5e2a580ef3a82935b27b
[stephane@stephane-ThinkPad-X301 nodejs]
$ docker exec -it nodejs bash
root@c6dddf0a5eb0:/# 

进入那里后,我试着获得版本:

root@c6dddf0a5eb0:/# npm -v
bash: npm: command not found

但是找不到npm。

在交互式shell中键入命令nvm use stable时,会出现以下错误:N/A: version "N/A" is not yet installed.

我知道有一个针对不存在的节点版本的别名。

nvm ls命令显示:

root@60446f9286d0:/# nvm ls
            N/A
node -> stable (-> N/A) (default)
iojs -> N/A (default)

调试器有这个显示:

root@60446f9286d0:/# nvm debug
nvm --version: v0.32.1
$SHELL: /bin/bash
$HOME: /root
$NVM_DIR: '$HOME/.nvm'
$PREFIX: ''
$NPM_CONFIG_PREFIX: ''
nvm current: none
which node: 
which iojs: 
which npm: 
npm config get prefix: bash: npm: command not found
npm root -g: bash: npm: command not found

1-为什么我需要在每个命令上获取此脚本. ~/.nvm/nvm.sh;

2-为什么在bash shell中找不到我的Node包管理器?

编辑:我改变了Dockerfile文件的内容:

RUN curl -o-https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash \
  && . ~/.nvm/nvm.sh \
  && nvm install stable \
  && nvm alias default stable \
  && nvm use default

现在建立它显示了这个:

Step 4 :RUN curl -o https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash   && . ~/.nvm/nvm.sh   && nvm install stable   && nvm alias default stable   && nvm use default
 ---> Running in 7d2c404135dd
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 10250  100 10250    0     0  18258      0 --:--:-- --:--:-- --:--:-- 18238
=> Downloading nvm as script to '/root/.nvm'

=> Appending source string to /root/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="/root/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v7.2.0 (npm v3.10.9)
Creating default alias: default -> stable (-> v7.2.0 *)
default -> stable (-> v7.2.0 *)
Now using node v7.2.0 (npm v3.10.9)
 ---> ad960a4addbe
Removing intermediate container 7d2c404135dd
Step 5 : EXPOSE 9001
 ---> Running in df9284421302
 ---> 14d386f009fb
Removing intermediate container df9284421302
Step 6 : ENTRYPOINT /usr/bin/tail -f /dev/null
 ---> Running in fa2d71b6dfdf
 ---> d02c8e88eb7f
Removing intermediate container fa2d71b6dfdf
Successfully built d02c8e88eb7f

我可以看到它安装了节点v7.2.0并正在使用它。

但是当我使用命令docker exec -it nodejs bash登录容器时,它在任何地方都看不到任何节点:

root@f8f2a32b462a:/# nvm --version   
0.32.1
root@f8f2a32b462a:/# npm --version   
bash: npm: command not found
root@f8f2a32b462a:/# echo $NVM_DIR   
/root/.nvm
root@f8f2a32b462a:/# ls -l /root/.nvm
total 100
-rwxr-xr-x 1 root root   313 Nov 26 13:01 nvm-exec
-rw-r--r-- 1 root root 95660 Nov 26 13:01 nvm.sh
root@f8f2a32b462a:/# ls -l /root/.npm
ls: cannot access /root/.npm: No such file or directory

2 个答案:

答案 0 :(得分:2)

我改变了安装Node的方式,并在没有nvm工具的情况下完成了它:

RUN curl -sL https://deb.nodesource.com/setup_7.x | bash
RUN apt-get install -y nodejs

现在,登录容器时,它可以找到Node可执行文件:

$ docker run -d -p 127.0.0.1:9001:9001 --name nodejs stephaneeybert/nodejs
f3a2f054934ef92a9b05486b6f6dbe53abd4390826c06d1b7a490d671d8e3422
[stephane@stephane-ThinkPad-X301 nodejs]
$ docker exec -it nodejs bash
root@f3a2f054934e:/# npm --version                                                                                                           
3.10.9

也许,当我使用nvm工具时,我应该在客户端shell npm中找到. ~/.nvm/nvm.sh npm --version命令。

答案 1 :(得分:0)

对于amazonlinux(Ubuntu Fedora),您可以在Dockerfile中使用yum进行安装:

RUN curl -sL https://rpm.nodesource.com/setup_10.x | bash # for node version 10.x
RUN yum -y install nodejs
RUN node --version # optional to check that it worked
RUN npm --version # optional to check that it worked

使用docker build -t [name] [local_folder_like_dot]

让我步入Google的时代,对linux并不太了解,但似乎每个版本之间的差异都很大。就像Google docker npm,使用apt get nah不起作用,找出linux版本的amasonlinux是..好的叫Fedora ..它有yum而不是apt ..好的..您需要在{{1中回答yes }},泊坞窗不允许它然后跳过它。.wth ..我可以用yum install nah hackz0r,只是垃圾邮件| yes forevah ..好吧,我可以y naz不行在Fedora中。.okiz可以killall进行安装,而不会出现Questionz askz? yum

2小时后,我们有了这个:)