Baeldung Pro – Ops – NPI EA (cat = Baeldung on Ops)
announcement - icon

It's finally here:

>> The Road to Membership and Baeldung Pro.

Going into ads, no-ads reading, and bit about how Baeldung works if you're curious :)

Partner – Orkes – NPI EA (cat=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Overview

Node Version Manager (NVM) is a tool for managing multiple Node.js versions on a machine.

In this tutorial, we’ll learn how to install NVM in a Docker container. We’ll discuss three methods:

  • Using an NVM Docker image, for when Node.js is the primary or only software to be used
  • Running commands to install, for when we are installing in a pre-existing or new Docker container
  • Building from a Dockerfile, for when we are automating the installation process

2. Using an NVM Docker Image

We can choose from a few Docker images for NVM that are available on Docker Hub. Let’s pull the guolin/node-nvm-docker image:

$ docker pull guolin/node-nvm-docker

And now, we can see it in our list of images:

$ docker images
REPOSITORY               TAG       IMAGE ID       CREATED       SIZE
guolin/node-nvm-docker   latest    6dc6207658b5   8 years ago   703MB

Having downloaded the image, let’s run a container based on it in interactive mode:

$ docker run 
--name node-nvm 
-it  
guolin/node-nvm-docker 
bash

This starts a Bash shell in the container:

root@0787786b89f9:/#

The NVM version manager is pre-installed in the container; therefore, we can directly confirm its version:

root@0787786b89f9:/# nvm --version
0.30.1

Furthermore, Node.js is also pre-installed in the container, so let’s verify that version, too:

root@0787786b89f9:/# nvm current
v4.2.1

3. Running Commands to Install NVM

We can install NVM in an existing or new Docker container by running a few commands.

Let’s launch an interactive Ubuntu container to install NVM:

$ docker run 
-it 
--name ubuntu-demo 
ubuntu 
/bin/bash -c "echo 'Hello World'; /bin/bash"

This launches an interactive Bash shell after outputting a “Hello World” message:

root@8cc33fbb81e0:/#

First, we need to download the curl command-line tool:

root@8cc33fbb81e0:/# apt update && apt install curl -y

Afterward, we can download and install NVM using curl:

root@8cc33fbb81e0:/# curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.2/install.sh | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 16631  100 16631    0     0   406k      0 --:--:-- --:--:-- --:--:--  406k
=> Downloading nvm as script to '/root/.nvm'

=> Appending nvm source string to /root/.bashrc
=> Appending bash_completion source string to /root/.bashrc

We could close and reopen the terminal to start using NVM. Alternatively, let’s run an export command to load NVM and start using it:

export NVM_DIR="$HOME/.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

With NVM loaded, we can find its usage with the nvm command:

root@8cc33fbb81e0:/# nvm

Node Version Manager (v0.40.2)

Usage:
  nvm --help                         Show this message
  nvm --version                      Print out the installed version of nvm

Let’s use it to install and use the latest Node version:

root@8cc33fbb81e0:/# nvm install node
Downloading and installing node v23.11.0...
Downloading https://nodejs.org/dist/v23.11.0/node-v23.11.0-linux-x64.tar.gz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v23.11.0 (npm v10.9.2)

And then, we can confirm the Node version reported by the installation:

root@8cc33fbb81e0:/# nvm current
v23.11.0

Interestingly, we can run this command even if Node is not yet installed.

4. Building From a Dockerfile

Perhaps the most suitable option when we want to automate the installation process is to use a Dockerfile. We’ll do this in three steps:

  1. Create a Dockerfile
  2. Build a Docker Image
  3. Run a Docker Container

4.1. Creating a Dockerfile

We can place much of what we did manually earlier into a Dockerfile:

FROM ubuntu:latest
ARG NODE_VERSION=20

# install curl
RUN apt update && apt install curl -y

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

# set env
ENV NVM_DIR=/root/.nvm

# install node
RUN bash -c "source $NVM_DIR/nvm.sh && nvm install $NODE_VERSION"

# set ENTRYPOINT for reloading nvm-environment
ENTRYPOINT ["bash", "-c", "source $NVM_DIR/nvm.sh && exec \"$@\"", "--"]

# set cmd to bash
CMD ["/bin/bash"]

We can see that we’re installing cURL, NVM, and Node just as before onto an Ubuntu base.

4.2. Building a Docker Image

Now, it’s time to build a Docker image from our Dockerfile. To do so, we need to be in the directory containing the Dockerfile.

Let’s run a docker build command to create a Docker image called nvm:latest:

$ docker build 
-t nvm:latest .

docker images shows us we successfully created it:

$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nvm          latest    7383b5cdf243   6 minutes ago   345MB
ubuntu       latest    602eb6fb314b   2 days ago      78.1MB

4.3. Running a Docker Container

Now, we can build a container that’s much like the one we built earlier from the Docker Hub image that had Node.js and NVM already installed.

Let’s run an example container from the image we built:

$ docker run --rm -it nvm
root@c012e7d835d9:/#

And now, we can again verify the NVM, Node, and NPM versions installed in the Docker container:

root@c012e7d835d9:/# nvm -v
0.40.2
root@c012e7d835d9:/# nvm current
v20.19.0
root@c012e7d835d9:/# npm -v
10.8.2

However, we aren’t limited to the version configured in the Dockerfile. To demonstrate, let’s install a new version of Node in the same container:

root@c012e7d835d9:/# nvm install 23.11.0
Downloading and installing node v23.11.0...
Downloading https://nodejs.org/dist/v23.11.0/node-v23.11.0-linux-x64.tar.gz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v23.11.0 (npm v10.9.2)

We can list all the installed versions with the nvm ls command:

root@c012e7d835d9:/# root@c012e7d835d9:/# nvm ls
       v20.19.0
->     v23.11.0
default -> 20 (-> v20.19.0)

The -> indicates the active Node version.

5. Conclusion

In this article, we learned about the different methods to install NVM within a Docker container. We can install NVM in an existing container or a new container. Or, when we want to automate the installation process, we can use a Dockerfile that has all the installation instructions configured to build our own image and container.

As always, the Docker commands used in this article are available over on GitHub.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest
Inline Feedbacks
View all comments