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.
Docker containerization capabilities with technologies that enable:
- Cost-effective scalability
- Lighter weight
- Greater resource efficiency
- Improved developer productivity (CI/CD)
Get started 🤘…
- Check prerequisite
- Create node.js application
- Creating the Dockerfile
- Build the docker image
- Push the docker image into Dockerhub repository
- Install Docker
- Install the Docker into your machine using below link.
Update to the Docker Desktop terms Commercial use of Docker Desktop in larger enterprises (more than 250 employees OR…
- 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…
- 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
Step 1: Check prerequisite
- If you installed the docker successfully then you can give following command to check the version.
- Same for node.js; check the version using following command.
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
Step 3: Creating the Dockerfile
- Create a new file named
- Copy & paste the following code
Step 4: Build the docker image
Here, the docker image will build.
docker build -t <dockerhub-username>/<repo-name> .
Step 5: Push the Docker image into Dockerhub repository
- You can view the built image by running the following code.
- Then, image can push to dockerhub using following code.
docker push <dockerhub-username>/<repo-name>:<tag>
<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.
- 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
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.