如何在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; 

build立输出:

 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的东西是什么打破了它。 这是完整的文件。

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

首先,replace…

 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