Installing NodeJS in Docker: A Lightweight Approach
Published: 2023-12-15 14:09:48

---
title: Installing NodeJS in Docker: A Lightweight Approach
published: true
date: 2023-10-25 08:52:17 UTC
tags: 
canonical_url: https://blog.mandraketech.in/installing-nodejs-in-docker-a-lightweight-approach
---

Node.js is a versatile runtime that allows you to execute JavaScript code on the server-side. It's a popular choice for web development, and many applications rely on it to power their backend services.

The official Node Docker images are good, but have a challenge for the security obsessed engineer. We need the latest OS patches, and the latest Node patched version, and control the distribution of the OS, all at the same time.

If you're working with Docker and need to install Node, I have a lightweight and efficient method that can work with any Linux distribution classified as "Linux" by the Node distribution system.

In this blog post, I'll guide you through a `Dockerfile` snippet that installs the latest supported version of Node v20, the current LTS. This approach has several advantages, such as using binary downloads, this avoiding the use of `apt` or `yum`. It also gives the ability to clean up unnecessary elements in the distribution, thus ensuring a smaller disk footprint.

## Prerequisites

Before we dive into the Dockerfile snippet, you'll need to make sure you have a few prerequisites installed on your system. These include `jq`, `curl`, `tar`, `unzip`, and `xz-utils`. These tools are essential for fetching and processing the necessary Node.js binaries. You can remove the `xz` dependency by using `.tar.gz`. I have used it to reduce the download size.

## The Dockerfile Snippet

Now, let's take a closer look at the Dockerfile snippet that automates the installation of Node.js:

```
# NodeJS is needed for VSCode remote runtime. Install latest supported LTS
# install NodeJS from NodeSource using binary download
RUN \
  NODE_LATEST=$(curl -s https://api.github.com/repos/nodejs/node/releases | jq -r '.[].tag_name | select(startswith("v20."))' | head -1) \
  && if [-z "$NODE_LATEST"]; then echo "Error: NODE_LATEST is empty"; exit 1; fi \
  && echo "Installing Node ${NODE_LATEST}" \
  # Set download URL based on architecture
  && NODE_DL_BASE="https://www.nodejs.org/dist/${NODE_LATEST}/node-${NODE_LATEST}-linux" \
  && if ["$(uname -m)" = "x86_64"]; then \
    DL_ARCH="x64"; \
  elif ["$(uname -m)" = "aarch64"]; then \
    DL_ARCH="arm64"; \
  else \
    echo "Unsupported architecture"; exit 1; \
  fi \
  && NODE_DL_URL="${NODE_DL_BASE}-${DL_ARCH}.tar.xz" \
  && echo "Downloading Node.js binary from ${NODE_DL_URL}" \
  && curl -fSL ${NODE_DL_URL} -o /tmp/nodejs.tar.xz \
  # extract to /opt
  && mkdir /opt/nodejs \
  && tar -xf /tmp/nodejs.tar.xz -C /opt/nodejs --strip-components=1 \
  && rm -rf /opt/nodejs/include \
  && rm -f /tmp/nodejs.tar.xz \
  # install the binaries in the path \
  && ln -s /opt/nodejs/bin/* /usr/local/bin/ \
  && echo "Done installing NodeJS"

```

This Dockerfile snippet is designed to be flexible and work with any Linux distribution that is classified as "Linux" by the Node.js distribution system. It leverages the GitHub API to fetch the latest available version of Node.js, and it installs the latest release in the "v20" series as mentioned in the code. You can easily update the Node.js version as needed.

### Benefits of This Approach

- **Distribution-Agnostic** : The method described here is not tied to any specific Linux distribution, making it highly versatile.
- **Lightweight** : By using binary downloads and avoiding the use of apt, this approach ensures a smaller disk footprint, which is essential for Docker containers.
- **Latest LTS Version** : It automatically fetches and installs the latest supported LTS version of Node.js.
- **Automated Updates** : You can easily update Node.js by modifying the version in the code, and the Docker image will stay up to date.

In conclusion, this Dockerfile snippet provides a reliable and efficient way to install Node.js within your Docker containers. It simplifies the process and ensures that you are working with the latest supported LTS version of Node.js, all while maintaining a lightweight footprint.

By following this approach, you can seamlessly integrate Node.js into your Docker-based projects, empowering you to build and run Node.js applications with ease.

Give it a try, and let us know how it works for you! If you have any questions or feedback, feel free to share them in the comments below.