DEV Community

Cover image for Dockerize Node.Js Application
Dulya Perera
Dulya Perera

Posted on • Originally published at Medium

Dockerize Node.Js Application

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

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

  1. 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.

  2. 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

  1. Create a new file named index.js and copy & paste the following code

  2. 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 / .
Replace the &  .
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 /:
Replace the & .
Replace the 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 /

  1. Then, run the application using following command. docker run -it -d -p 3000:3000 /
  2. 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.

Top comments (2)

Collapse
 
naucode profile image
Al - Naucode

That was a nice read! Liked, bookmarked and followed, keep the good work!

Collapse
 
dulyaaa profile image
Dulya Perera

Thank you so much!!😊😇✨️