Skip to content

NodeJS Under Docker

In this section will be detailed how to install NodeJS image under Docker.

Due to space constraints, Docker is only recommended to be installed on eManager Pro.

NodeJS image installation

NodeJS images are publicly available in the Docker Hub container image library. In this example 20.10.0-bullseye-slim image will be used.

First, create directories in your eManager that we will use to generate the image and give to it the needed permissions:

mkdir -p ~/dockers/node-<VERSION>/data
chmod 777 ~/dockers/node-<VERSION>/data

where <VERSION> indicates the NodeJS version that is being installed. In this example, the folder name must be node-20.10, as we are going to install the 20.10 version of NodeJS.

Then, create a docker-compose.yml file in the ~/dockers/node-20.10/ folder:

nano ~/dockers/node-20.10/docker-compose.yml

with the following content:

version: "3.0"
services:
  node-20.10:
    image: node:20.10.0-bullseye-slim
    container_name: node-20.10
    environment:
      - TZ=Europe/Madrid
    volumes:
      - ~/dockers/node-20.10/data:/data
    command: tail -f /dev/null

It is strongly recommended to mount the data content of docker images in /data partition. This will make you save space in /dev/root partition. It is important to note that by default, Docker images are already installed on the /data partition.

The above compose file:

  • Pulls the 20.10.0-bullseye-slim nodeJS image
  • Defines a nodeJS container called node-20.10, which will run in a service called node-20.10
  • Sets the timezone to Europe/Madrid
  • Persists the /data dir inside the container to the user local ~/dockers/node-20.10/data directory
  • tail -f /dev/null command specifies not to stop the container once created.

And finally, create the NodeJS container and run it in the background:

cd ~/dockers/node-20.10/
docker-compose up -d

Access NodeJS image using shell commands

NodeJS container has been built and created. Now, it is possible to use the new version of NodeJS that have been installed.

To do so, you can access to the container shell command:

docker-compose exec node-20.10 /bin/bash

After that, you are inside the NodeJS image. Now, it is possible to verify NodeJS version by running:

node --version

Once finished if it is needed to stop the container, run docker-compose stop.