Dockerize Node.Js Application

Dulya Kemali Perera
SLIIT Women In FOSS Community
3 min readJun 5, 2022

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

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

  1. Install Docker
  • Install the Docker into your machine using below link.
  • Or else use Google Cloud Platform Console by creating a free trial account using below link. (No need to download the docker)

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

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

  1. Create a new folder and go inside
  2. 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

  1. Create a new file named Dockerfile
  2. 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

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

--

--