How to install nvm in docker?
When you RUN bash...
each time that runs in a separate process, anything set in the environment is not maintained. Here's how I install nvm
:
# Replace shell with bash so we can source filesRUN rm /bin/sh && ln -s /bin/bash /bin/sh# Set debconf to run non-interactivelyRUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections# Install base dependenciesRUN apt-get update && apt-get install -y -q --no-install-recommends \ apt-transport-https \ build-essential \ ca-certificates \ curl \ git \ libssl-dev \ wget \ && rm -rf /var/lib/apt/lists/*ENV NVM_DIR /usr/local/nvm # or ~/.nvm , dependingENV NODE_VERSION 0.10.33# Install nvm with node and npmRUN curl https://raw.githubusercontent.com/creationix/nvm/v0.20.0/install.sh | bash \ && . $NVM_DIR/nvm.sh \ && nvm install $NODE_VERSION \ && nvm alias default $NODE_VERSION \ && nvm use defaultENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modulesENV PATH $NVM_DIR/v$NODE_VERSION/bin:$PATH
To help everyone that are looking for a way to install the Node.js with NVM on Ubuntu (last version), I made the dockerfile below. I'm using the last version of Docker, Ubuntu, Node.js and the NVM is working properly (the $PATH was fixed). I'm using this in a production environment.
$ docker info \Server Version: 1.9.1Kernel Version: 4.1.13-boot2dockerOperating System: Boot2Docker 1.9.1 (TCL 6.4.1); master : cef800b - Fri Nov 20 19:33:59 UTC 2015Node.js Version: stable 4.2.4 LTSUbuntu Version: 14.04.3
dockerfile:
FROM ubuntu:14.04.3# Replace shell with bash so we can source filesRUN rm /bin/sh && ln -s /bin/bash /bin/sh# make sure apt is up to dateRUN apt-get update --fix-missingRUN apt-get install -y curlRUN apt-get install -y build-essential libssl-devENV NVM_DIR /usr/local/nvmENV NODE_VERSION 4.2.4# Install nvm with node and npmRUN curl https://raw.githubusercontent.com/creationix/nvm/v0.30.1/install.sh | bash \ && source $NVM_DIR/nvm.sh \ && nvm install $NODE_VERSION \ && nvm alias default $NODE_VERSION \ && nvm use defaultENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modulesENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATHRUN mkdir /usr/appRUN mkdir /usr/app/logWORKDIR /usr/app# log dirVOLUME /usr/app/log# Bundle app sourceCOPY . /usr/app# Install app dependenciesRUN npm installEXPOSE 3000CMD ["node", "server.js"]
Update 20/02/2020: This solution works if you're using a debian
base image. If you're using ubuntu
, see this answer.
Here is the cleanest way to install nvm
that I have found:
SHELL ["/bin/bash", "--login", "-c"]RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bashRUN nvm install 10.15.3
Explanation
The first line sets the Dockerfile's default shell to a bash login shell. Note: this means that every subsequent
RUN
,CMD
, andENTRYPOINT
will be run under the current user (usually root), and source the ~/.bashrc file if run in the shell form.The second line installs
nvm
with bash. When the script is run with bash, it appends to the ~/.bashrc file.The third line installs a particular version of nodejs and uses it. The
nvm
,npm
, andnode
commands are available because they are run via a bash login shell (see line 1).