我正在尝试在 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.
最佳答案
我对您的 Dockerfile 进行了以下更改以使其正常工作:
首先,更换...
RUN sh /root/.nvm/install.sh;
...和:
RUN bash /root/.nvm/install.sh;
为什么?在基于 Redhat 的系统上,
/bin/sh
是 /bin/bash
的符号链接(symbolic link).但是在 Ubuntu , /bin/sh
是 /bin/dash
的符号链接(symbolic link).这就是 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
关于docker - 如何在 Dockerfile 中安装 nvm?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42079122/