DEV Community

Cover image for Dockerize a Nodejs Application
Abhishek Kumar Verma
Abhishek Kumar Verma

Posted on • Updated on

Dockerize a Nodejs Application

I feel very embarrassed when I claim to be a backend developer without basic Docker knowledge. Doesn't it feel the same to you?

Prerequisites
Before I start, make sure you have the following installed:

  1. Node.js and npm:.You can download and install it from the Node.js official website.

2.Docker: Download and install form Docker's Official Website.

Begin Image

Step1: Create a Nodejs App or use my sample Nodejs App by cloning it

git clone https://github.com/AbhishekCS3459/Node_Docker_Demo
cd Node_Docker_Demo
npm install
Enter fullscreen mode Exit fullscreen mode

Step2 (Optional): Run the below command to run the application demo

npm run start
Enter fullscreen mode Exit fullscreen mode

Step3: Run the below command to build the docker image

docker build YOUR_IMAGE_NAME .
Enter fullscreen mode Exit fullscreen mode

Note: . represents you are running the above command in the current directory where your node application exists.

Step4: Check whether your image has build or not by running the below command

docker images
Enter fullscreen mode Exit fullscreen mode

Step5: Run the container using the following command

docker run -it -p 3000:3000 YOUR_IMAGE_NAME
Enter fullscreen mode Exit fullscreen mode

Note: Here -it is a flag to run the container in interactive mode and -p for mapping the container port with the external port

Container:

A Docker container is a lightweight, standalone, executable package that includes everything needed to run a piece of software, including the code, runtime, system tools, libraries, and settings. Containers isolate the application from its environment, ensuring consistent behavior across different environments.

Docker Container Image

Deploy the Image to Docker Hub

To share your Docker image with others, you can deploy it to Docker Hub.

Step 1: Login to Docker Hub

First, log in to Docker Hub using the command:

docker login
Enter fullscreen mode Exit fullscreen mode

You will be prompted to enter your Docker Hub username and password.

Step 2: Tag Your Image

Tag your Docker image with your Docker Hub repository name. Replace YOUR_DOCKERHUB_USERNAME and YOUR_IMAGE_NAME with your Docker Hub username and the name of your image:

docker tag YOUR_IMAGE_NAME YOUR_DOCKERHUB_USERNAME/YOUR_IMAGE_NAME
Enter fullscreen mode Exit fullscreen mode

Step 3: Push Your Image to Docker Hub
Push the tagged image to Docker Hub:

docker push YOUR_DOCKERHUB_USERNAME/YOUR_IMAGE_NAME
Enter fullscreen mode Exit fullscreen mode

You can now see your image on Docker Hub and share it with others!

Dev Image

If you want to ask, ping me below.
Connect with me on Linkedin:
linkedin/abhishekverman.
Further Reading: Dockerize a Golang Application

Top comments (3)

Collapse
 
abhishekcs3459 profile image
Abhishek Kumar Verma • Edited

If you want to ask, ping me below.
Connect with me on Linkedin:
linkedin/abhishekverman

Collapse
 
thexdev profile image
M. Akbar Nugroho

I made this also! But, for bundled React application. Go check it out...

Collapse
 
urzahmed profile image
Urooj Ahmad

That's insightful