Dockerize Node.Js Application
Tags: docker, nodej, javascript
Published: 2022-09-15 19:57:56

## What’s & Why Docker?
Docker is an open source containerization platform that allows developers to package applications into containers-standardized executable components combining application source code with operating system(OS) libraries and dependencies required to run that code in any environment.
![Containers and Virtual Machines Together](https://miro.medium.com/max/788/1*m7Ru_0cIWsU865qctpPuzg.png)

Containers and Virtual Machines Together
Docker containerization capabilities with technologies that enable:
* Cost-effective scalability
* Disposability
* Lighter weight
* Greater resource efficiency
* Improved developer productivity (CI/CD)

## Get started 🤘…!
 
1. Check prerequisite
2. Create node.js application
3. Creating the Dockerfile
4. Build the docker image
5. Push the docker image into Dockerhub repository

---

Prerequisite
Install Docker

Install the Docker into your machine using below link.

Get Docker
Update to the Docker Desktop terms Commercial use of Docker Desktop in larger enterprises (more than 250 employees OR…docs.docker.com
Or else use Google Cloud Platform Console by creating a free trial account using below link. (No need to download the docker)

Cloud Computing Services | Google Cloud
Meet your business challenges head on with cloud computing services from Google, including data management, hybrid…cloud.google.com
2. Node.js
Since my application is node.js application, need to install node.js
Or else again you can use Google Cloud Platform Console to create your application by using same commands.

3. Create Docker hub account
Follow bellow link to create new account

Docker Hub Container Image Library | App Containerization
Edit descriptionhub.docker.com

---

Step 1: Check prerequisite
If you installed the docker successfully then you can give following command to check the version.

docker --version
Same for node.js; check the version using following command.

node -v
If you can't see the version that means it's not installed successfully. Reinstall it if so.
Step 2: Create node.js application
Create a new folder and go inside
Initialize the project using below command

npm init -y
3. Create a new file named index.js and copy & paste the following code

4. Install dependencies
npm install
Step 3: Creating the Dockerfile
Create a new file named Dockerfile
Copy & paste the following code

Step 4: Build the docker image
Here, the docker image will build.
docker build -t <dockerhub-username>/<repo-name> .
Replace the <dockerhub-username>& <repo-name> .
Step 5: Push the Docker image into Dockerhub repository
You can view the built image by running the following code.

docker images
Then, image can push to dockerhub using following code.

docker push <dockerhub-username>/<repo-name>:<tag>
Replace the <dockerhub-username>& <repo-name>.
Replace the <tag> what ever you like. Usually it is the version of built image.
Go to your dockerhub account repository tab. There you can see created new docker image with tag.

---

Run
We need to pull the docker image before run.

docker pull <dockerhub-username>/<repo-name>
2. Then, run the application using following command.
docker run -it -d -p 3000:3000 <dockerhub-username>/<repo-name>
3. Access using http://localhost:3000/ & http://localhost:3000/ready
Conclusion:
Now we have our application dockerized and pushed to a docker hub repository. Any system with a docker client installed can pull this docker image and run the application instantly.