Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Install node and npm with nvm using Docker.
#!/usr/bin/env bash
# confirm docker daemon is running and connected
docker version
# build the image based on the Dockerfile and name it `nvm`
docker build -t nvm .
# confirm image is present
docker images
# enter container terminal
docker run -it nvm bash
# set the base image to Debian
# https://hub.docker.com/_/debian/
FROM debian:latest
# replace shell with bash so we can source files
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
# update the repository sources list
# and install dependencies
RUN apt-get update \
&& apt-get install -y curl \
&& apt-get -y autoclean
# nvm environment variables
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 4.4.7
# install nvm
# https://github.com/creationix/nvm#install-script
RUN curl --silent -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.2/install.sh | bash
# install node and npm
RUN source $NVM_DIR/nvm.sh \
&& nvm install $NODE_VERSION \
&& nvm alias default $NODE_VERSION \
&& nvm use default
# add node and npm to path so the commands are available
ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
# confirm installation
RUN node -v
RUN npm -v
@blixit
Copy link

blixit commented Oct 18, 2018

The initial gist was not working for me. Here is how I updated it:

# Installation of NVM, NPM and packages
RUN mkdir /usr/local/nvm
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 10.12.0
ENV NVM_INSTALL_PATH $NVM_DIR/versions/node/v$NODE_VERSION
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
RUN curl --silent -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
RUN source $NVM_DIR/nvm.sh \
   && nvm install $NODE_VERSION \
   && nvm alias default $NODE_VERSION \
   && nvm use default
ENV NODE_PATH $NVM_INSTALL_PATH/lib/node_modules
ENV PATH $NVM_INSTALL_PATH/bin:$PATH
RUN npm -v  # <= works
RUN node -v  # <= works
RUN nvm -v  # <= NOT works :( 

@KristerV
Copy link

KristerV commented Oct 25, 2018

Turns out v8 and v10 have different paths.

# Add node and npm to path so the commands are available
# Newer
# ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
# ENV PATH $NVM_DIR/v$NODE_VERSION/bin:$PATH
# Older
ENV NODE_PATH $NVM_DIR/versions/node/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH

@ljharb
Copy link

ljharb commented Oct 25, 2018

@KristerV the “newer” paths you mention are actually older; 0.12 and below use it. Node v8 and v10 have identical formats - perhaps you mean 0.10?

@agdula
Copy link

agdula commented Oct 27, 2018

Here is as solution that does not mess with /bin/sh

ENV NVM_VERSION v0.33.11
ENV NODE_VERSION v7.5.0
ENV NVM_DIR /usr/local/nvm
RUN mkdir $NVM_DIR
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH

RUN echo "source $NVM_DIR/nvm.sh && \
    nvm install $NODE_VERSION && \
    nvm alias default $NODE_VERSION && \
    nvm use default" | bash

@begin29
Copy link

begin29 commented Nov 5, 2018

@agdula,
tnx for solution, it works with small corrections.
There is small typo here ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules

NVM installs to $NVM_DIR/$NODE_VERSION but path is added as $NVM_DIR/v$NODE_VERSION

v letter is already included in NODE_VERSION here.

@shivrajbisht
Copy link

shivrajbisht commented Dec 25, 2018

Thanks , really helpful

@mpahuja
Copy link

mpahuja commented Apr 5, 2019

Thanks, very helpful.

@PEZO19
Copy link

PEZO19 commented Apr 9, 2019

To me it a was also necessary to create symlinks:

RUN ln -sf NVM_DIR/versions/node/v$NODE_VERSION/bin/node /usr/bin/nodejs
RUN ln -sf NVM_DIR/versions/node/v$NODE_VERSION/bin/node /usr/bin/node
RUN ln -sf NVM_DIR/versions/node/v$NODE_VERSION/bin/npm /usr/bin/npm

@renatoargh
Copy link

renatoargh commented Apr 12, 2019

Thanks, @PEZO19! symlinks were the only thing that made it work! :)

@eugenet8k
Copy link

eugenet8k commented Aug 29, 2019

Thanks , @PEZO19! Symlinks are required if a command is passed after docker run, like:

> docker run nvm node -v
v12.9.1

Otherwise, without synlinks

> docker run nvm node -v
docker: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"node\": executable file not found in $PATH": unknown.
ERRO[0001] error waiting for container: context canceled 

But if you TTY into container docker run -i -t nvm it would work regardless.

Also, if someone builds Docker Image using custom user, you got to create symlinks with a root account, therefore:

USER root
RUN ln -sf NVM_DIR/versions/node/v$NODE_VERSION/bin/node /usr/bin/nodejs
RUN ln -sf NVM_DIR/versions/node/v$NODE_VERSION/bin/node /usr/bin/node
RUN ln -sf NVM_DIR/versions/node/v$NODE_VERSION/bin/npm /usr/bin/npm
USER youruser

Make sure $NVM_DIR is not pointing to something like ~/.nvm because the link will be wrong, pointing to root's user home directory.

@bheisig
Copy link

bheisig commented Oct 28, 2019

Here is my solution which doesn't care about version numbers but installs latest npm and node LTS, doesn't use any symlinks, doesn't mess up with $PATH and /bin/sh but uses a login shell:

FROM debian:buster

ARG DEBIAN_FRONTEND=noninteractive

SHELL ["/bin/bash", "-l", "-euxo", "pipefail", "-c"]

RUN apt-get update; \
    apt-get full-upgrade -y; \
    apt-get install -y --no-install-recommends \
        ca-certificates \
        curl \
    ; \
    apt-get clean; \
    rm -rf /var/lib/apt/lists/*

ENV NVM_DIR /usr/local/nvm

RUN mkdir -p "$NVM_DIR"; \
    curl -o- \
        "https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh" | \
        bash \
    ; \
    source $NVM_DIR/nvm.sh; \
    nvm install --lts --latest-npm

RUN command -v nvm; \
    command -v node; \
    node --version; \
    command -v npm; \
    npm --version

Thanks to https://www.theguild.nl/nvm-in-docker/ to show a more elegant way by using a login shell.

@smilelikeshit
Copy link

smilelikeshit commented Dec 10, 2019

the best ! thank you

@zaqx1994
Copy link

zaqx1994 commented Jan 10, 2020

thank u , this help me a lot.

@adam-roth
Copy link

adam-roth commented Jan 11, 2020

Fixed some cryptic errors I was getting around the source $NVM_DIR/nvm.sh step with my own attempt.

👍

@akshaydegaonkar
Copy link

akshaydegaonkar commented Apr 1, 2020

source $NVM_DIR/nvm.sh this step in my docker file throws me an error that
/bin/sh: /usr/local/nvm/nvm.sh: No such file or directory , I have nvm installed .
Any suggestions

@beamsies
Copy link

beamsies commented Aug 2, 2020

worked for me, thanks

@vidyli
Copy link

vidyli commented Dec 18, 2020

source $NVM_DIR/nvm.sh this step in my docker file throws me an error that
/bin/sh: /usr/local/nvm/nvm.sh: No such file or directory , I have nvm installed .
Any suggestions

I also have this error even on debian:latest

@rmtsrc
Copy link

rmtsrc commented Dec 28, 2020

source $NVM_DIR/nvm.sh this step in my docker file throws me an error that
/bin/sh: /usr/local/nvm/nvm.sh: No such file or directory , I have nvm installed .
Any suggestions

I also have this error even on debian:latest

I had a similar issue, but solved it by updating nvm and the path environment variables.

@Elte156
Copy link

Elte156 commented Jan 20, 2021

The initial gist was not working for me. Here is how I updated it:

# Installation of NVM, NPM and packages
RUN mkdir /usr/local/nvm
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 10.12.0
ENV NVM_INSTALL_PATH $NVM_DIR/versions/node/v$NODE_VERSION
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
RUN curl --silent -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
RUN source $NVM_DIR/nvm.sh \
   && nvm install $NODE_VERSION \
   && nvm alias default $NODE_VERSION \
   && nvm use default
ENV NODE_PATH $NVM_INSTALL_PATH/lib/node_modules
ENV PATH $NVM_INSTALL_PATH/bin:$PATH
RUN npm -v  # <= works
RUN node -v  # <= works
RUN nvm -v  # <= NOT works :( 

I had the same issue with trying to run RUN nvm -v in the dockerfile as well.

I'm running nvm 0.37.2 and I noticed that if you source your ~/.bashrc right before calling nvm, it'll work.
RUN source ~/.bashrc && nvm --version

Of course, this is dependant on where your .bashrc file is. I believe it has to do with docker RUN opening its own "sub-shell" and it does not automatically load your contents of .bashrc.

@vpaka
Copy link

vpaka commented Feb 11, 2021

Thanks heaps! Saved me a lot of time.
This is how I modified it. Since it fails with the directory not being created by the script. So I get to install old versions and also the latest

  ENV NVM_DIR /usr/local/nvm
  RUN mkdir -p /usr/local/nvm/ && \
          curl --silent -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash 
  RUN /bin/bash -c "source $NVM_DIR/nvm.sh \
        && nvm install 6.17 .1 \
        && nvm install 10.1.0 \
        && nvm install node"
  ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
  ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH```

@lucascheles
Copy link

lucascheles commented Mar 20, 2021

This helped me a lot.
Thank you!

@joeynguyen
Copy link

joeynguyen commented Aug 18, 2021

The initial gist was not working for me. Here is how I updated it:

# Installation of NVM, NPM and packages
RUN mkdir /usr/local/nvm
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 10.12.0
ENV NVM_INSTALL_PATH $NVM_DIR/versions/node/v$NODE_VERSION
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
RUN curl --silent -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
RUN source $NVM_DIR/nvm.sh \
   && nvm install $NODE_VERSION \
   && nvm alias default $NODE_VERSION \
   && nvm use default
ENV NODE_PATH $NVM_INSTALL_PATH/lib/node_modules
ENV PATH $NVM_INSTALL_PATH/bin:$PATH
RUN npm -v  # <= works
RUN node -v  # <= works
RUN nvm -v  # <= NOT works :( 

I had the same issue with trying to run RUN nvm -v in the dockerfile as well.

I'm running nvm 0.37.2 and I noticed that if you source your ~/.bashrc right before calling nvm, it'll work.
RUN source ~/.bashrc && nvm --version

Of course, this is dependant on where your .bashrc file is. I believe it has to do with docker RUN opening its own "sub-shell" and it does not automatically load your contents of .bashrc.

This worked for me! Thanks for the explanation about how Docker RUN opens its own "sub-shell" on each RUN command. That clears up the confusion I was having on why I kept nvm not found. Just curious, is that sub-shell behavior documented in their docs?

@Dejulia489
Copy link

Dejulia489 commented Oct 4, 2021

Got the following error.
/bin/sh: 1: source: not found

Resolved by replacing 'source' with '.'
RUN . ~/.bashrc

@CB-GavinZhao
Copy link

CB-GavinZhao commented Oct 22, 2021

The initial gist was not working for me. Here is how I updated it:

# Installation of NVM, NPM and packages
RUN mkdir /usr/local/nvm
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 10.12.0
ENV NVM_INSTALL_PATH $NVM_DIR/versions/node/v$NODE_VERSION
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
RUN curl --silent -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
RUN source $NVM_DIR/nvm.sh \
   && nvm install $NODE_VERSION \
   && nvm alias default $NODE_VERSION \
   && nvm use default
ENV NODE_PATH $NVM_INSTALL_PATH/lib/node_modules
ENV PATH $NVM_INSTALL_PATH/bin:$PATH
RUN npm -v  # <= works
RUN node -v  # <= works
RUN nvm -v  # <= NOT works :( 

I had the same issue with trying to run RUN nvm -v in the dockerfile as well.
I'm running nvm 0.37.2 and I noticed that if you source your ~/.bashrc right before calling nvm, it'll work.
RUN source ~/.bashrc && nvm --version
Of course, this is dependant on where your .bashrc file is. I believe it has to do with docker RUN opening its own "sub-shell" and it does not automatically load your contents of .bashrc.

This worked for me! Thanks for the explanation about how Docker RUN opens its own "sub-shell" on each RUN command. That clears up the confusion I was having on why I kept nvm not found. Just curious, is that sub-shell behavior documented in their docs?

Works for me.
RUN source $NVM_DIR/nvm.sh \ && nvm --version works as well.

@choehn-signogy
Copy link

choehn-signogy commented Nov 11, 2021

amazonlinux:latest

except I had to add 'RUN mkdir -p /usr/local/nvm' it works well.

Thank you, Sir

@alexanderdavide
Copy link

alexanderdavide commented Feb 11, 2022

nvm not found can result from it being installed for a different user than the one who is executing the container. You may need to prefix the installation with the custom user who is executing the container. The last USER statement defines the container user.

USER $USERNAME

RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

Reason

Diving into a nvm install script, e. g. v0.39.1, one can see that is installed into $HOME of the current user. If you have not changed it, the default user of a ubuntu image is root. When starting the container with a different user however, nvm won't be found; hence make sure user of installation and execution align.

nvm_default_install_dir() {
  [ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm"
}

@majari7514
Copy link

majari7514 commented Jun 16, 2022

I'm using the .nvmrc file to automatically install and use the node version, but after building my dockerfile, it is using always the Default version as default -> 18.3.0 (-> v18.3.0 *) even though using this version "v16.14.1". Could someone have any idea about this issue? please find the docker file and image building log below.

Dockerfile

FROM node:slim
RUN rm /bin/sh && ln -s /bin/bash /bin/sh 
RUN apt-get update >/dev/null \
    && apt-get install -y curl >/dev/null \
    && apt-get -y autoclean >/dev/null
ENV NVM_DIR /usr/local/nvm
RUN mkdir -p /usr/local/nvm
COPY .nvmrc .   # Copy local .nvmrc file to dockerimage, .nvmrc file contains string "16.14.1" which is the node_version i need to run 
COPY .testfile .  # this file contains deeper shell integration -->https://github.com/nvm-sh/nvm/tree/v0.39.1#bash
RUN curl https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
RUN source $NVM_DIR/nvm.sh \  
            && nvm install \
            && nvm use
RUN cat .testfile >> $HOME/.bashrc
RUN source $HOME/.bashrc
RUN echo $NODE_VERSION
ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/v$NODE_VERSION/bin:$PATH
RUN node -v
RUN npm -v

Building docker image log

Building Docker image for: nvm-docker-image
INFO[0000] Retrieving image manifest node:slim          
INFO[0000] Retrieving image node:slim from registry index.docker.io 
INFO[0002] Built cross stage deps: map[]                
INFO[0002] Retrieving image manifest node:slim          
INFO[0002] Returning cached image manifest              
INFO[0002] Executing 0 build triggers                   
INFO[0002] Unpacking rootfs as cmd RUN rm /bin/sh && ln -s /bin/bash /bin/sh requires it. 
INFO[0007] RUN rm /bin/sh && ln -s /bin/bash /bin/sh    
INFO[0007] Taking snapshot of full filesystem...        
INFO[0009] cmd: /bin/sh                                 
INFO[0009] args: [-c rm /bin/sh && ln -s /bin/bash /bin/sh] 
INFO[0009] Running: [/bin/sh -c rm /bin/sh && ln -s /bin/bash /bin/sh] 
INFO[0009] Taking snapshot of full filesystem...        
INFO[0009] RUN apt-get update >/dev/null     && apt-get install -y curl >/dev/null     && apt-get -y autoclean >/dev/null 
INFO[0009] cmd: /bin/sh                                 
INFO[0009] args: [-c apt-get update >/dev/null     && apt-get install -y curl >/dev/null     && apt-get -y autoclean >/dev/null] 
INFO[0009] Running: [/bin/sh -c apt-get update >/dev/null     && apt-get install -y curl >/dev/null     && apt-get -y autoclean >/dev/null] 
debconf: delaying package configuration, since apt-utils is not installed
INFO[0016] Taking snapshot of full filesystem...        
INFO[0017] ENV NVM_DIR /usr/local/nvm                   
INFO[0017] RUN mkdir -p /usr/local/nvm                  
INFO[0017] cmd: /bin/sh                                 
INFO[0017] args: [-c mkdir -p /usr/local/nvm]           
INFO[0017] Running: [/bin/sh -c mkdir -p /usr/local/nvm] 
INFO[0017] Taking snapshot of full filesystem...        
INFO[0018] COPY .nvmrc .                                
INFO[0018] Taking snapshot of files...                  
INFO[0018] COPY .testfile .                             
INFO[0018] Taking snapshot of files...                  
INFO[0018] RUN curl https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash 
INFO[0018] cmd: /bin/sh                                 
INFO[0018] args: [-c curl https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash] 
INFO[0018] Running: [/bin/sh -c curl https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash] 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 15037  100 15037    0     0   734k      0 --:--:-- --:--:-- --:--:--  734k
=> Downloading nvm as script to '/usr/local/nvm'
=> Appending nvm source string to /root/.bashrc
=> Appending bash_completion source string to /root/.bashrc
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
=> You currently have modules installed globally with `npm`. These will no
=> longer be linked to the active version of Node when you install a new node
=> with `nvm`; and they may (depending on how you construct your `$PATH`)
=> override the binaries of modules installed with `nvm`:
/usr/local/lib
+-- corepack@0.10.0
=> If you wish to uninstall them at a later point (or re-install them under your
=> `nvm` Nodes), you can remove them from the system Node as follows:
     $ nvm use system
     $ npm uninstall -g a_module
=> Installing Node.js version 18.3.0
Downloading and installing node v18.3.0...
Downloading https://nodejs.org/dist/v18.3.0/node-v18.3.0-linux-x64.tar.gz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v18.3.0 (npm v8.11.0)
Creating default alias: default -> 18.3.0 (-> v18.3.0 *)
=> Node.js version 18.3.0 has been successfully installed
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="/usr/local/nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
INFO[00[24](https://gitlab.com/t8822/sandbox/docker-build/-/jobs/2598875120#L24)] Taking snapshot of full filesystem...        
INFO[0035] RUN source $NVM_DIR/nvm.sh             && nvm install             && nvm use 
INFO[0035] cmd: /bin/sh                                 
INFO[0035] args: [-c source $NVM_DIR/nvm.sh             && nvm install             && nvm use] 
INFO[0035] Running: [/bin/sh -c source $NVM_DIR/nvm.sh             && nvm install             && nvm use] 
Found '//.nvmrc' with version <16.14.1>
Downloading and installing node v16.14.1...
Downloading https://nodejs.org/dist/v16.14.1/node-v16.14.1-linux-x64.tar.gz...
######################################################################## 100.0%
Computing checksum with sha[25](https://gitlab.com/t8822/sandbox/docker-build/-/jobs/2598875120#L25)6sum
Checksums matched!
Now using node v16.14.1 (npm v8.5.0)
Found '//.nvmrc' with version <16.14.1>
Now using node v16.14.1 (npm v8.5.0)
INFO[0039] Taking snapshot of full filesystem...        
INFO[0053] RUN cat .testfile >> $HOME/.bashrc           
INFO[0053] cmd: /bin/sh                                 
INFO[0053] args: [-c cat .testfile >> $HOME/.bashrc]    
INFO[0053] Running: [/bin/sh -c cat .testfile >> $HOME/.bashrc] 
INFO[0053] Taking snapshot of full filesystem...        
INFO[0065] RUN source $HOME/.bashrc                     
INFO[0065] cmd: /bin/sh                                 
INFO[0065] args: [-c source $HOME/.bashrc]              
INFO[0065] Running: [/bin/sh -c source $HOME/.bashrc]   
Now using node v16.14.1 (npm v8.5.0)
INFO[0066] Taking snapshot of full filesystem...        
INFO[0076] RUN echo $NODE_VERSION                       
INFO[0076] cmd: /bin/sh                                 
INFO[0076] args: [-c echo $NODE_VERSION]                
INFO[0076] Running: [/bin/sh -c echo $NODE_VERSION]     
18.3.0
INFO[0076] Taking snapshot of full filesystem...        
INFO[0087] No files were changed, appending empty layer to config. No layer added to image. 
INFO[0087] ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules 
INFO[0087] ENV PATH $NVM_DIR/v$NODE_VERSION/bin:$PATH   
INFO[0087] RUN node -v                                  
INFO[0087] cmd: /bin/sh                                 
INFO[0087] args: [-c node -v]                           
INFO[0087] Running: [/bin/sh -c node -v]                
v18.3.0
INFO[0087] Taking snapshot of full filesystem...        
INFO[0097] No files were changed, appending empty layer to config. No layer added to image. 
INFO[0097] RUN npm -v                                   
INFO[0097] cmd: /bin/sh                                 
INFO[0097] args: [-c npm -v]                            
INFO[0097] Running: [/bin/sh -c npm -v]                 
8.11.0

@manish-2014
Copy link

manish-2014 commented Jun 18, 2022

There is apparently an offical docker container for NVM. Here is the docker file link

I have created a Dockerfile for nvm v0.39.1 (latest today) and Ubuntu 22.04 by copying and pasting commands from the official Dockerfile into my custom container's Dockerfile

FROM ubuntu:22.04
# removed docker command for my stuff


USER ${user}

# we need a .bashrc  for install.sh to work
RUN touch ~/.bashrc && chmod +x ~/.bashrc

RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash ;

# this node version is old but I need it for my use case
ENV NODE_VERSION=12.16.1

# relevant code I pasted from above link
# nvm
RUN echo 'export NVM_DIR="$HOME/.nvm"'                                       >> "$HOME/.bashrc"
RUN echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm' >> "$HOME/.bashrc"
RUN echo '[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion" # This loads nvm bash_completion' >> "$HOME/.bashrc"


# nodejs and tools
RUN bash -c 'source $HOME/.nvm/nvm.sh   && \
    nvm install $NODE_VERSION && nvm use $NODE_VERSION && nvm alias default $NODE_VERSION       && \
    npm install -g doctoc urchin eclint dockerfile_lint && \
    npm install --prefix "$HOME/.nvm/" && \
    npm install --global gulp-cli';
 # I added gulp for my use case 

# then  the rest of the dockerfile
WORKDIR /home/${user}

# add your entrypoint
ENTRYPOINT ["path-to-my-shell-file"]

@grinsteindavid
Copy link

grinsteindavid commented Aug 25, 2022

How to install nvm in Docker / devcontainer

FROM mcr.microsoft.com/vscode/devcontainers/cpp:ubuntu-20.04

RUN mkdir /usr/local/nvm
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 14.18.1
RUN curl https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash \
    && . $NVM_DIR/nvm.sh \
    && nvm install $NODE_VERSION \
    && nvm alias default $NODE_VERSION \
    && nvm use default

ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH

it worked for me

@Ihor-Kokhan
Copy link

Ihor-Kokhan commented Sep 1, 2022

grinsteindavid commented 7 days ago
How to install nvm in Docker / devcontainer
FROM mcr.microsoft.com/vscode/devcontainers/cpp:ubuntu-20.04
RUN mkdir /usr/local/nvm
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 14.18.1
RUN curl https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
&& . $NVM_DIR/nvm.sh
&& nvm install $NODE_VERSION
&& nvm alias default $NODE_VERSION
&& nvm use default
ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
it worked for me

+1 Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment