December 22, 2024
Tutorials

How to Build Node.js Application With Docker on Ubuntu 22.04

How to Build Node.js Application With Docker on Ubuntu 22

Node.js is widely recognized for its powerful functionalities, including the development of server-side applications using JavaScript. While, Docker is utilized for containerization, packaging, and deploying applications.

By integrating Node.js with Docker, you can create portable and consistent environments for your applications across various platforms.

This comprehensive guide illustrates the complete process of building a Node.js application and deploying it using Docker on Ubuntu 22.04.

How to Build Node.js Application With Docker on Ubuntu 22.04?

Follow these step-by-step instructions to build a Node.js application with Docker on your Linux systems, including Ubuntu 22.04.

Step 1: Verify Docker Status

Without Docker, you cannot build a Docker image on your system. Therefore, ensure that you have already installed Docker and it is properly running:

sudo systemctl status docker

Step 2: Confirm Node.js Installation

To create a Node.js application, you must have Node.js installed on your Ubuntu 22.04 machine. Confirm the installed version of Node.js by executing the following command:

node --version

The Node.js version number (v21.6.2) will also be used in the Dockerfile to ensure compatibility.

How to Build the Node.js Application?

Using these few steps leads to building a Node.js application on your system.

Step 1: Create Project Directory

To build a Node.js application, begin by creating a separate project directory, such as “node-docker-app”:

mkdir node-docker-app

Step 2: Navigate to Project Directory

After creating a new project directory, use the cd command to navigate inside the directory:

cd node-docker-app

Step 3: Initialize Node.js Project

To initialize a new Node.js project with default settings, run the command from your terminal:

npm init -y

Step 4: Create Application File (App.js)

Now, you can create the application file (app.js) for your Node.js application using a text editor like Nano:

sudo nano app.js

This command will open a blank page on your screen.

Simply, copy the following script and save the file:

const http = require('http'); 

const hostname = '0.0.0.0'; 
const port = 3000; 

const server = http.createServer((req, res) => { 
res.statusCode = 200; 
res.setHeader('Content-Type', 'text/plain'); 
res.end('Congratulations! You have successfully built a Node.js application with Docker\n'); 
}); 

server.listen(port, hostname, () => { 
console.log(`Server running at http://${hostname}:${port}/`); 
});

Note: You can provide the port number (i.e. 3000) of your own choice.

So far, you have created a Node.js application. Now, let’s jump to the next step to create a Docker file.

How to Create Dockerfile for Node.js Application?

Utilize the below commands to create and deploy your Node.js application with Docker.

Step 1: Create a “Dockerfile” for Your Application

To define the configuration for your Docker image, create a new file like “Dockerfile” through the command:

sudo nano Dockerfile

Within the Dockerfile, specify the setup and dependencies based on your application requirements:

# Use the Node.js version

FROM node:21

# Set the directory

WORKDIR /usr/src/app

COPY package*.json ./

# Dependencies

RUN npm install

COPY . .

# Set port

EXPOSE 3000

# Run the application

CMD ["node", "app.js"]

Make sure that you have entered the correct values, especially for the FROM node: and EXPOSE, as the mismatched values may cause your application to fail.

Step 2: Build Docker Image

Run the command to create a Docker image of your Node.js application (node-docker-app) based on the instructions in the Dockerfile:

sudo docker build -t node-docker-app .

Step 3: Run Docker Container

Now, run a Docker container based on the specified Docker image, such as “node-docker-app”:

sudo docker run -d -p 3000:3000 node-docker-app

The -p 3000:3000 is a dedicated port number, you can use a custom port number as per your app.js and Dockerfile.

Step 4: Verify Container Status

Once you have completed the above steps, let’s verify that your “node-docker-app” container is running correctly, use the provided command:

sudo docker ps

You will see the Docker image of your Node.js application listed in your terminal, confirming that the image has been successfully created and is accessible on port 3000.

Step 5: Access Your Node.js Application

To access your Node.js application, open your web browser and copy the following URL:

localhost:3000

You have successfully built and containerized your Node.js application using Docker on Ubuntu 22.04.

Conclusion

You can use simple steps to build a Node.js application with Docker on your Linux systems like Ubuntu 22.04. First, you must have installed the Docker and Node.js packages on your system. After initializing the Node.js project, create an application file with the Node.js script.

Next, create a Dockerfile and build a docker image via “sudo docker build -t node-docker-app”. Finally, run the command “sudo docker run -d -p 3000:3000 node-docker-app” to containerize your Node.js application.

This comprehensive guide explored the easiest way to build Node.js application with Docker on Ubuntu 22.04.

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video