DevOps

Installing NVM and Node.js in a Docker Container

Node Version Manager (NVM) is a powerful tool that allows developers to install and switch between multiple versions of Node.js with ease. In this article, we’ll walk you through how to install and use NVM in a Docker container. This is particularly useful if you’re building Node.js-based applications in Docker and want flexibility over the Node.js version. Let us delve into understanding how to use a Docker container with nvm install to manage Node.js versions efficiently.

1. Introduction

By default, Docker containers are stateless and non-interactive, which means any shell-specific configuration (like environment variables set by NVM) does not persist across sessions unless explicitly defined. This can make using tools like NVM, which modify the shell environment dynamically, a bit challenging. However, by carefully structuring the Dockerfile—setting environment variables, sourcing the NVM script correctly, and updating the PATH—we can successfully install and use NVM to manage the Node.js version of our choice inside a Docker container.

2. Using an NVM Docker Image

NVM (Node Version Manager) is a powerful tool that allows developers to install and switch between different versions of Node.js with ease. In containerized environments like Docker, it’s often useful to have flexibility in managing Node.js versions—especially for CI/CD pipelines, development containers, or when maintaining apps that rely on legacy or experimental Node versions.

Several community-maintained Docker images come with NVM already pre-installed. These images simplify setup and reduce the need to manually install and configure NVM. Some popular options include:

  • mhart/alpine-node: A lightweight Alpine-based image that includes Node.js and optionally NVM. Ideal for fast boot times and smaller image sizes.
  • node: The official Node.js Docker image which can be used as a base to install NVM manually.

While these images are helpful, they may not always align with your project’s exact requirements. You might want to:

  • Use a different Linux distribution (e.g., Ubuntu instead of Alpine).
  • Install a specific version of NVM or Node.js that isn’t pre-configured in the image.
  • Customize the installation process for better caching or script optimization.

In such cases, it’s beneficial to install NVM manually inside your Docker container. This not only gives you full control over the environment but also helps you understand what’s happening under the hood—a valuable learning experience.

2.1 Running Commands to Install NVM

To manually install NVM, we can run a series of shell commands in a container. Here’s a basic example of how you can try it interactively:

# Run the Docker container
docker run -it --rm ubuntu:22.04 bash

# Inside the container
apt update && apt install -y curl bash build-essential
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

# Load nvm (needed in every shell unless added to profile)
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

# Verify installation
nvm --version

# Install Node.js
nvm install 18
node -v

The above commands demonstrate how to manually install NVM (Node Version Manager) and Node.js inside an Ubuntu 22.04 Docker container. First, the docker run command launches an interactive Ubuntu container. Inside the container, it updates the package list and installs essential tools like curl, bash, and build-essential. The NVM install script is then downloaded and executed using curl. Since NVM needs to be loaded in each shell session, the environment variable NVM_DIR is set, and the NVM script is sourced. The nvm --version command verifies the installation. Finally, Node.js version 18 is installed using nvm install 18, and its installation is confirmed by running node -v. This approach is useful for learning and testing the installation process in an isolated environment.

After executing the node -v command inside the Docker container, you will see an output similar to the following:

v0.39.7
v18.20.2

2.2 Building From a Dockerfile

To automate this process, you can create a Dockerfile that installs NVM and sets up Node.js using it.

# syntax=docker/dockerfile:1
FROM ubuntu:22.04

ENV DEBIAN_FRONTEND=noninteractive

# Install dependencies
RUN apt-get update && apt-get install -y \
    curl \
    build-essential \
    bash \
    git \
    ca-certificates

# Install NVM
ENV NVM_DIR=/root/.nvm
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

# Load NVM and install Node.js
RUN bash -c "source $NVM_DIR/nvm.sh && \
             nvm install 18 && \
             nvm alias default 18 && \
             nvm use default"

# Add nvm, node, and npm to PATH
ENV NODE_VERSION=18
ENV PATH=$NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH

# Verify Node and NPM installation
RUN node -v && npm -v

CMD ["node"]

This Dockerfile creates a custom Docker image based on Ubuntu 22.04 with NVM and Node.js version 18 pre-installed. It sets the DEBIAN_FRONTEND environment variable to noninteractive to suppress interactive prompts during package installation. It then installs essential dependencies such as curl, git, bash, and build tools using apt-get. The NVM_DIR environment variable is defined to specify where NVM will be installed. The NVM install script is downloaded and executed via curl. After that, the Dockerfile uses a shell to source NVM, install Node.js version 18, set it as the default version, and ensure it’s used by default. The PATH is updated to include the directory containing Node.js and npm binaries. Finally, the image verifies the Node and npm installations and sets the container’s default command to run node.

2.2.1 Build and Run the Docker Image

Once the Dockerfile is ready with all the necessary instructions to install NVM and Node.js, the next step is to build the Docker image and run a container from it. This process packages your environment into a reusable and portable image, ensuring consistent behavior across different systems and development setups.

# Build the Docker image
docker build -t nvm-node .

# Run a container
docker run -it --rm nvm-node bash

# Inside container
node -v
npm -v

After running these commands, you should see output similar to:

v18.20.2
9.6.7

Sign up

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button