1

I am trying to install nvm which requires you to source a file

I found that this does not work:

RUN . /root/.nvm/nvm.sh
RUN nvm use

Says nvm not found

But this does work:

RUN . /root/.nvm/nvm.sh && nvm use

Any idea how I can persist the sourcing of the nvm.sh file across RUN commands?

My full set of commands

# Persist NVM
RUN mkdir /root/.nvm
ENV NVM_DIR /root/.nvm
ENV NODE_VERSION 9.11.2
ENV NODE_PATH $NVM_DIR/versions/node/v$NODE_VERSION/lib/node_modules
ENV PATH      $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH

# NVM So that we can get the latest node
# @todo Fix this to not require sourcing in the same step @tolicodes and should happen in main Dockerfile
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
RUN . $NVM_DIR/nvm.sh && \
    nvm install $NODE_VERSION && \
    nvm alias default $NODE_VERSION && \
    nvm use default

The main thing is the ENV variables. Other instructions get the path incorrect for the latest nvm.

Toli
  • 5,547
  • 8
  • 36
  • 56
  • 1
    You'd need to change the environment that docker starts the container with, using `ENV` commands. You may also be able to trigger it with something like a .bashrc entry, but that requires you to run a shell for the command being launched. – BMitch Oct 02 '18 at 20:19
  • Does https://stackoverflow.com/a/50958281/6309 help? – VonC Oct 02 '18 at 20:26
  • @vonc Yeah that was one of the first things I tried...no dice. I'm using https://github.com/cypress-io/cypress-docker-images/blob/master/base/8/Dockerfile as a base btw which is essentially just the default node 8 image – Toli Oct 02 '18 at 20:30
  • I wouldn't bother using `nvm` in a Docker container. Just use a base image like `node:10` that has the version of Node you need already installed. (You'd never have more than one version in an image, so you don't need a special tool to support multiple concurrent installations.) – David Maze Oct 02 '18 at 23:13

0 Answers0