How to run Node.js Docker Container

Share on Social Media

Learn how to run a Node.js Docker container with our step-by-step guide. Discover how to create a Dockerfile, build an image, and deploy your Node.js application efficiently. Perfect for developers aiming to streamline their development and deployment process. #centlinux #linux #docker

What is Node.js?

Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to execute JavaScript code outside of a web browser. Built on Chrome’s V8 JavaScript engine, Node.js is designed for building scalable network applications, particularly web servers and real-time applications like chat programs.

Key Features

  1. Asynchronous and Event-Driven: Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. This is particularly useful for handling multiple simultaneous connections, making it ideal for real-time applications.
  2. Single-Threaded: Despite being single-threaded, Node.js can handle many concurrent connections with high throughput, thanks to its non-blocking architecture.
  3. NPM (Node Package Manager): Node.js comes with NPM, a powerful package manager that provides access to thousands of reusable modules, libraries, and tools that can be easily integrated into applications.
  4. Cross-Platform: Node.js runs on various platforms, including Windows, Linux, Unix, and macOS, making it highly versatile for different development environments.
  5. Rich Ecosystem: The vast ecosystem of Node.js includes frameworks like Express.js, Koa.js, and Nest.js, which simplify the development of web applications and APIs.

Common Use Cases

  • Web Servers: Node.js is commonly used to build web servers that can handle numerous client requests efficiently.
  • APIs: Creating RESTful APIs and microservices that can interact with databases and other services.
  • Real-Time Applications: Applications requiring real-time interactions, such as chat applications, online gaming, and collaborative tools.
  • Command-Line Tools: Developing command-line tools and scripts for various automation tasks.

Advantages of Using Node.js

  • Performance: High performance due to the non-blocking I/O operations and the fast V8 engine.
  • Scalability: Suitable for scalable applications that need to handle many concurrent connections.
  • Developer Productivity: JavaScript is used both on the client and server sides, promoting code reuse and simplifying the development process.
  • Active Community: Strong community support and continuous updates and improvements.

Node.js is a popular choice for modern web development, offering efficiency and scalability for a wide range of applications.

Recommended Online Training: Docker and Containers Essentials

2496480 675bshow?id=oLRJ54lcVEg&offerid=1606991.2496480&bids=1606991

Docker Host Specification

We are using a preconfigured Docker Host with following specification.

  • CPU – 3.4 Ghz (2 cores)
  • Memory – 4 GB
  • Storage – 20 GB
  • Operating System – Ubuntu Server 18.04 LTS
  • Hostname – docker-01.centlinux.com
  • IP Address – 192.168.116.206 /24

If you want to see how to configure a Docker Host, the you can follow our following articles.

Create a Directory for Node.js Docker Project

Connect with Docker Host (docker-01.centlinux.com) as an admin user by using a ssh tool.

Create a directory for your Node.js Docker Project.

ahmer@docker-01:~$ mkdir ~/nodejs_docker
ahmer@docker-01:~$ cd nodejs_docker

Develop Node Web App

To keep things simple, we are writing a “HelloWorld” node web application for our Node.js Docker container.

ahmer@docker-01:~/nodejs_docker$ nano myapp.js

And add following lines of code to create a simple “Hello World” application.

'use strict';

const express = require('express');

// Constants
const PORT = 3000;
const HOST = '0.0.0.0';

// App
const app = express();
app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(PORT, HOST);
console.log(`Running at http://${HOST}:${PORT}`);

Create the Node.js package.json file. This file provides the meta data information for our Node.js Docker application.

{
"name": "myapp",
"version": "1.0.0",
"description": "Node.js on Docker",
"author": "Alaric Bird <alaric.bird@gmail.com>",
"main": "myapp.js",
"scripts": {
"start": "node myapp.js"
},
"dependencies": {
"express": "^4.16.1"
}
}

Create a Docker file for Node.js Docker Project:

To define the deployment procedure such as docker image to be used, dependencies, service port, etc. We are required to create a Dockerfile for our Nodejs Docker container.

ahmer@docker-01:~/nodejs_docker$ nano Dockerfile

Add following directives therein (each directive is briefly described in the comments before it).

# Docker Image to be used
FROM node:13-alpine

# Create a working directory within NodeJS Docker container
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied where available (npm@5+)
COPY package*.json ./

RUN npm install

# Bundle the App source
COPY . .

# Expose the port 3000 to network computers
EXPOSE 3000

#Run the node command and start our web application
CMD [ "node", "myapp.js" ]

We are using the node:13-alpine image here, because it is the lightest image that will fulfill our project’s requirement.

However, there are many variants of the node images are available at the Docker Hub. To get the complete list, you can visit the Official Docker Images for Nodejs Page.

Build the Node.js Docker Image

We have created the Dockerfile and the Node.js web application.

Now build the Docker image for our Node.js web application by using the Dockerfile.

ahmer@docker-01:~/nodejs_docker$ sudo docker build .
Sending build context to Docker daemon  4.096kB
Step 1/7 : FROM node:13-alpine
 ---> 483343d6c5f5
Step 2/7 : WORKDIR /usr/src/app
 ---> Using cache
 ---> bad78ca928c6
Step 3/7 : COPY package*.json ./
 ---> Using cache
 ---> fdd7ac2c1274
Step 4/7 : RUN npm install
 ---> Using cache
 ---> 8a34ec80bd87
Step 5/7 : COPY . .
 ---> 873f9ddd4360
Step 6/7 : EXPOSE 3000
 ---> Running in 7d567e0f4da3
Removing intermediate container 7d567e0f4da3
 ---> b3e087682594
Step 7/7 : CMD [ "node", "myapp.js" ]
 ---> Running in 4e170d3a1567
Removing intermediate container 4e170d3a1567
 ---> 7be386175292
Successfully built 7be386175292

We have successfully built our Node.js Docker image.

We have built this image locally, however you can build and store this image on Docker Hub by using your Docker Hub credentials.

Run Node.js Docker Container

Start a container by using above Docker image.

ahmer@docker-01:~/nodejs_docker$ sudo docker run 
> --name nodejs-docker-app 
> -p 80:3000 
> -d 
> 7be386175292
87f61f85cd0546786a2af8a5eb6da1402e753a1239a0f1088a735217130a603b

We have started our Node.js Docker container. Check its log now.

ahmer@docker-01:~/nodejs_docker$ sudo docker logs nodejs-docker-app
Running at http://0.0.0.0:3000

Test the Dockerized Node.js Application

Access the Dockerized Nodejs application using the curl command.

ahmer@docker-01:~/nodejs_docker$ curl localhost:80
Hello World!

Our Node.js application has been deployed in a Docker container.

If you are new to Docker platform, then you should read Docker in Action (PAID LINK) by Manning Publications.

Final Thoughts

Running a Node.js application within a Docker container can significantly enhance your development and deployment process. By containerizing your application, you ensure consistency across different environments, ease scalability, and simplify dependency management. Docker provides a robust platform for deploying Node.js applications seamlessly, whether for development, testing, or production.

If you need expert assistance in running your Node.js Docker container or any other Docker-related services, I’m here to help. With my extensive experience in Docker and Node.js, I can provide tailored solutions to meet your specific needs.

Check out my Fiverr profile for more information on my services and to get started. Let’s streamline your development process and deploy your applications efficiently with Docker!

Feel free to reach out, and let’s take your project to the next level!

Leave a Comment