Docker
nvm
Node.js
installation guide
developer tools

How to install nvm in docker?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Docker is an excellent tool for containerizing applications, providing an isolated and consistent environment. However, managing multiple versions of Node.js can be tricky inside Docker containers. NVM (Node Version Manager) helps tackle this problem by allowing you to easily switch between Node.js versions. This article will guide you through installing and using NVM inside a Docker container.

Prerequisites

Before we start with the installation process, ensure you have the following:

  • Docker installed on your host machine.
  • Basic understanding of Docker and Dockerfiles.
  • A working internet connection to fetch the necessary files from the web.

Step-by-step Installation Guide

Step 1: Create a Dockerfile

Start by creating a Dockerfile for your project. This file will serve as the blueprint to build your Docker image. Here's a basic Dockerfile setup:

dockerfile
1# Use an official base image of Ubuntu
2FROM ubuntu:20.04
3
4# Install Node.js prerequisites
5RUN apt-get update && apt-get install -y \
6    curl \
7    build-essential \
8    libssl-dev
9
10# Install NVM
11RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
12
13# Load NVM and install a specific Node.js version
14RUN ["/bin/bash", "-c", "source ~/.nvm/nvm.sh && nvm install node"]
15
16# Set the entry point to bash for interactive containers
17ENTRYPOINT ["/bin/bash"]

Step 2: Build the Docker Image

Once your Dockerfile is ready, build the Docker image using the docker build command. Ensure you are in the same directory as your Dockerfile:

bash
docker build -t my-nvm-node-image .

Step 3: Run the Docker Container

With your image built, run it in a container:

bash
docker run -it my-nvm-node-image

Step 4: Verify NVM Installation

Once inside the container, verify that NVM is installed correctly:

bash
nvm --version

If NVM is installed properly, you should see the version number.

Step 5: Using NVM Inside Docker

You can now use NVM inside the Docker container to switch between Node.js versions. Some common NVM commands include:

  • nvm install <version> - Install a specific Node.js version.
  • nvm use <version> - Switch to a different Node.js version.
  • nvm ls - List all installed Node.js versions.

Summary Table of Key Points

Key StepDescription
Dockerfile SetupCreate a Dockerfile to install pre-requisites and NVM.
Build ImageUse docker build to create a Docker image.
Run ContainerStart a container using docker run.
Verify InstallationCheck the NVM version to verify successful installation.
Use NVMCommands like nvm install, nvm use for managing versions.

Additional Considerations

Persistence of NVM Changes

Any installed Node.js versions or changes made in a running Docker container will not persist after the container is stopped or removed. To make persistent changes, consider using Docker volumes or updating the Dockerfile to install the necessary Node.js version during the build process.

Automating Node.js Version Selection

You can automate the default Node.js version for your Docker container by adding command lines to your Dockerfile:

dockerfile
RUN ["/bin/bash", "-c", "source ~/.nvm/nvm.sh && nvm alias default <version>"]

Use Cases

NVM inside Docker is particularly useful for:

  1. Development - Allows developers to mimic their local environment.
  2. Testing – Enables running tests across multiple Node.js versions.
  3. Application Portability - Offers flexibility to adapt node-based applications for varied environments.

Conclusion

Integrating NVM into your Docker setup allows for versatile Node.js version management and establishes a seamless bridge between development environments and production systems. With this guide, you now possess the knowledge to install and use NVM within Docker, significantly enhancing your workflow around Node.js applications.


Course illustration
Course illustration

All Rights Reserved.