How to set up Node.js in Docker

Last updated on May 27 2022
Raigiri Patil

Table of Contents

How to set up Node.js in Docker

Docker – Setting Node.js

Node.js is a JavaScript framework that is employed for developing server-side applications. It is an open source framework that is developed to run on a variety of operating systems. Since Node.js is a popular framework for development, Docker has also ensured it has support for Node.js applications.

We will now see the various steps for getting the Docker container for Node.js up and running.

Step 1 − The primary step is to pull the image from Docker Hub. When you log into Docker Hub, you’ll be able to search and see the image for Node.js as shown below. Just type in Node within the search box and click on the node (official) link which comes up within the search results.

Page 1 Image 1 57
login

Step 2 − You’ll see that the Docker pull command for node within the details of the repository in Docker Hub.

Page 2 Image 2 51
pull

Step 3 − On the Docker Host, use the Docker pull command as shown above to download the latest node image from Docker Hub.

Page 2 Image 3 26
node image

Once the pull is complete, we can then proceed with the next step.

Page 2 Image 4 7
next step

Step 4 − On the Docker Host, let’s use the vim editor and create one Node.js example file. In this file, we will add a simple command to display “HelloWorld” to the command prompt.

Page 3 Image 5 19
command

Within the Node.js file, let’s add the subsequent statement −

Console.log(‘Hello World’);

This will output the “Hello World” phrase when we run it through Node.js.

Page 3 Image 6 8
statement

Ensure that you save the file and then proceed to the next step.

Step 5 − To run our Node.js script using the Node Docker container, we need to execute the subsequent statement −

sudo docker run –it –rm –name = HelloWorld –v “$PWD”:/usr/src/app    –w /usr/src/app node node HelloWorld.js

The subsequent points need to be noted about the above command −

  • The –rm option is employed to remove the container after it is run.
  • We are giving a name to the container called “HelloWorld”.
  • We are mentioning to map the volume within the container which is /usr/src/app to our current present working directory. This is done so that the node container will pick up our HelloWorld.js script which is present in our working directory on the Docker Host.
  • The –w option is employed to specify the working directory employed by Node.js.
  • The primary node option is employed to specify to run the node image.
  • The second node option is employed to mention to run the node command within the node container.
  • And finally we mention the name of our script.

We will then get the subsequent output. And from the output, we can clearly see that the Node container ran as a container and executed the HelloWorld.js script.

Page 3 Image 7 2
hello world

So, this brings us to the end of blog. This Tecklearn ‘How to set up Node’ blog helps you with commonly asked questions if you are looking out for a job in DevOps. If you wish to learn Docker and build a career in DevOps domain, then check out our interactive, Containerization using Docker Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/containerization-using-docker/

Containerization using Docker Training

About the Course

Tecklearn has specially designed this Containerization using Docker Training Course to advance your skills for a successful career in this domain. his Docker training online course will help you learn Docker containerization, running Docker containers, Docker image creation, Dockerfile, Docker orchestration, security best practices and more through hands-on projects and case studies. Upon completion of this online training, you will hold a solid understanding and hands-on experience with Docker.

Why Should you take Containerization using Docker Training?

  • Average salary of Docker Expert is $110k – Indeed.com
  • According to Grand View Research, the DevOps market size is estimated to be worth $12.85 billion by 2025. DevOps professionals are highly paid and in-demand throughout industries including retail, eCommerce, finance, and technology.
  • Intuit, PayPal, Splunk, Uber & many other MNC’s worldwide use Docker across industries

What you will Learn in this Course?

Introduction to DevOps

  • What is Software Development
  • Software Development Life Cycle
  • Why DevOps?
  • What is DevOps?
  • DevOps Lifecycle
  • DevOps Tools
  • Benefits of DevOps
  • How DevOps is related to Agile Delivery
  • DevOps Implementation

Containerization using Docker – Part 1

  • Introduction to Docker
  • Understanding Docker Lifecycle
  • Docker Architecture
  • Components of Docker Ecosystem
  • Common Docker Operations
  • Committing changes in a Container
  • Hands On

Containerization using Docker – Part 2

  • Building Custom Docker Images
  • Docker Image
  • Introduction to Docker Swarm
  • Deploying a 2-Node Cluster using Docker Swarm
  • Hands on

 

0 responses on "How to set up Node.js in Docker"

Leave a Message

Your email address will not be published. Required fields are marked *