如何在Dockerfile中安装nvm?

时间:2017-02-06 23:25:43

标签: docker dockerfile nvm

我试图在Dockerfile中安装nvm。它似乎安装正常,但nvm命令无效。

Dockerfile:

# Install nvm
RUN git clone http://github.com/creationix/nvm.git /root/.nvm;
RUN chmod -R 777 /root/.nvm/;
RUN sh /root/.nvm/install.sh;
RUN export NVM_DIR="$HOME/.nvm";
RUN echo "[[ -s $HOME/.nvm/nvm.sh ]] && . $HOME/.nvm/nvm.sh" >> $HOME/.bashrc;
RUN nvm ls-remote;

构建输出:

Step 23/39 : RUN git clone http://github.com/creationix/nvm.git /root/.nvm;
 ---> Running in ca485a68b9aa
Cloning into '/root/.nvm'...
 ---> a6f61d486443
Removing intermediate container ca485a68b9aa
Step 24/39 : RUN chmod -R 777 /root/.nvm/
 ---> Running in 6d4432926745
 ---> 30e7efc5bd41
Removing intermediate container 6d4432926745
Step 25/39 : RUN sh /root/.nvm/install.sh;
 ---> Running in 79b517430285
=> Downloading nvm from git to '$HOME/.nvm'
=> Cloning into '$HOME/.nvm'...
* (HEAD detached at v0.33.0)
  master
=> Compressing and cleaning up git repository

=> Appending nvm source string to /root/.profile
=> bash_completion source string already in /root/.profile
npm info it worked if it ends with ok
npm info using npm@3.10.10
npm info using node@v6.9.5
npm info ok
=> Installing Node.js version 6.9.5
Downloading and installing node v6.9.5...
Downloading https://nodejs.org/dist/v6.9.5/node-v6.9.5-linux-x64.tar.xz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v6.9.5 (npm v3.10.10)
Creating default alias: default -> 6.9.5 (-> v6.9.5 *)
/root/.nvm/install.sh: 136: [: v6.9.5: unexpected operator
Failed to install Node.js 6.9.5
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
 ---> 9f6f3e74cd19
Removing intermediate container 79b517430285
Step 26/39 : RUN export NVM_DIR="$HOME/.nvm";
 ---> Running in 1d768138e3d5
 ---> 8039dfb4311c
Removing intermediate container 1d768138e3d5
Step 27/39 : RUN echo "[[ -s $HOME/.nvm/nvm.sh ]] && . $HOME/.nvm/nvm.sh" >> $HOME/.bashrc;
 ---> Running in d91126b7de62
 ---> 52313e09866e
Removing intermediate container d91126b7de62
Step 28/39 : RUN nvm ls-remote;
 ---> Running in f13c1ed42b3a
/bin/sh: 1: nvm: not found
The command '/bin/sh -c nvm ls-remote;' returned a non-zero code: 127

错误:

Step 28/39 : RUN nvm ls-remote;
 ---> Running in f13c1ed42b3a
/bin/sh: 1: nvm: not found
The command '/bin/sh -c nvm ls-remote;' returned a non-zero code: 127

我的/root/.bashrc文件的结尾如下:

[[ -s /root/.nvm/nvm.sh ]] && . /root/.nvm/nvm.sh

Dockerfile中的其他所有内容都可以使用。添加nvm的东西是打破它的原因。 Here is the full file.

1 个答案:

答案 0 :(得分:5)

我对您的Dockerfile进行了以下更改以使其正常工作:

首先,替换......

RUN sh /root/.nvm/install.sh;

...与:

RUN bash /root/.nvm/install.sh;

为什么呢?在基于Redhat的系统上,/bin/sh/bin/bash的符号链接。但在Ubuntu上,/bin/sh/bin/dash的符号链接。这就是dash

的情况
root@52d54205a137:/# bash -c '[ 1 == 1 ] && echo yes!'
yes!
root@52d54205a137:/# dash -c '[ 1 == 1 ] && echo yes!'
dash: 1: [: 1: unexpected operator

其次,替换......

RUN nvm ls-remote;

...与:

RUN bash -i -c 'nvm ls-remote';

为什么呢?因为,Ubuntu中的用户的默认.bashrc(几乎在顶部)包含:

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

nvm脚本的源代码发生在底部。所以我们需要确保通过传递参数-i来交互式地调用bash。

第三,您可以跳过Dockerfile中的以下行:

RUN export NVM_DIR="$HOME/.nvm";
RUN echo "[[ -s $HOME/.nvm/nvm.sh ]] && . $HOME/.nvm/nvm.sh" >> $HOME/.bashrc;

为什么呢?因为bash /root/.nvm/install.sh;会自动为您执行此操作:

[fedora@myhost ~]$ sudo docker run --rm -it 2a283d6e2173 tail -2 /root/.bashrc
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm