Categories: Dockers

Docker – How to Create Javascript Runtime using NodeJS

This article represents information on how to install Javascript runtime in order to compile/interpret JS file for the purpose of testing. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.
Following are the key points described later in this article:
  • Install NodeJS Runtime with Docker Container
  • Script to Create NodeJS Container
  • Test JS script execution
Many a times, we come across the need to write the Javascript file and run it using a Runtime without the need to test the Javascript code using an HTML page. We could achieve this objective using NodeJS runtime. We shall use Docker to create NodeJS container within which we could write and test our JS files.
Install NodeJS Runtime with Docker Container

Following code could be used to build the NodeJS image:

# Use centos6 base image
FROM    centos:centos6

# Enable Extra Packages for Enterprise Linux (EPEL) for CentOS
RUN     yum install -y epel-release

# Install Node.js and npm
RUN     yum install -y nodejs npm
Script to Create NodeJS Container

Following script (name it as installNode.js) could be used to create NodeJS container:

#!/bin/sh

if [ $# == 0 ]; then
  echo "This script expect container name argument. Example: ./installNode.sh node1"
  exit 100
fi

# Build NodeJS image if it does not exists
#
nb_image="nodejs_base"
nb_df="nodejs_base.df"
if [ `docker images $nb_image | wc -l` -lt 2 ]; then
  echo "Docker Image $nb_image do not exist..."
  echo "Builing docker image $nb_image"
  if [ -f $nb_df ]; then
    docker build -t $nb_image -f $nb_df .
  else
    echo "Can't find Dockerfile $nb_df in the current location"
    exit 200
  fi
fi

docker run -ti -dP --name $1 -v /c/Users:/mnt/Users $nb_image /bin/bash

Execute the above script using following code to start the container with name as “node1”

./installNode.js node1
Test JS File Execution

Login inside the NodeJS container with following code:

docker exec -ti node1 bash

Once logged in, create a sample file, namely hello.js, such as following:

function hello(user) {
    return "Hello, " + user;
}

console.log(hello("Calvin"));

Execute using Node runtime in following way and “Hello, Calvin” will be printed.

node hello.js

 

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking. Check out my other blog, Revive-n-Thrive.com

Share
Published by
Ajitesh Kumar

Recent Posts

Feature Engineering in Machine Learning: Python Examples

Last updated: 3rd May, 2024 Have you ever wondered why some machine learning models perform…

1 day ago

Feature Selection vs Feature Extraction: Machine Learning

Last updated: 2nd May, 2024 The success of machine learning models often depends on the…

2 days ago

Model Selection by Evaluating Bias & Variance: Example

When working on a machine learning project, one of the key challenges faced by data…

2 days ago

Bias-Variance Trade-off in Machine Learning: Examples

Last updated: 1st May, 2024 The bias-variance trade-off is a fundamental concept in machine learning…

3 days ago

Mean Squared Error vs Cross Entropy Loss Function

Last updated: 1st May, 2024 As a data scientist, understanding the nuances of various cost…

3 days ago

Cross Entropy Loss Explained with Python Examples

Last updated: 1st May, 2024 In this post, you will learn the concepts related to…

3 days ago