DevOps using Azure and Docker with Nodejs

Raghav Jindal
3 min readMay 24, 2021

In this tutorial, we are going to be looking at how you can dockerize an existing NodeJS application and ultimately leverage the benefits of Docker. Then we will create Ci-Cd Pipeline using Azure DevOps for automating Docker image creation and push docker image into Docker Hub 🤘

The automated flow of Azure DevOps and Docker image.

Dockerizing our Nodejs application

We are going to first Dockerize our full-stack application. I will be using my existing nodejs application Typoo — git URL

The application will listen on a port 3000 for any incoming requests and will map those requests against the corresponding route.

Docker should be installed in your system, if not refer to these commands, ignore them if already installed

1)sudo apt update
2)sudo apt install apt-transport-https ca-certificates curl software-properties-common
3)curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
4)sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
5)sudo apt update
6)apt-cache policy docker-ce
7)sudo apt install docker-ce
8)sudo systemctl status docker

Step 1: Project Setup

You can clone my repository or you can make any other nodejs application with a basic setup.If you are familiar with Node.js development, you’ll know that the standard practice for this is to declare that your application requires express.js within the package.json in your application’s directory.

// package.json
{
"name": "Typoo",
"version": "1.0.0",
"main": "server.js",
"dependencies": {
"express": "^4.16.3"
},
"scripts": {
"start": "node server.js"
}
}

We can test if our application works locally by calling npm install within our project directory and then subsequently calling npm run start. You should then be able to navigate to http://localhost:3000/ and be greeted with the frontend in your browser.

Step 2: Creating Dockerfile

The aim of the game here is to get a docker image that is as thin as possible in terms of size and is still able to provide our application with everything it needs in order to run successfully.

Step 3: Build the image

Now that we have defined our Dockerfile within our application’s directory, we can go about building our Docker image. We can do this by running the following docker command within our terminal.

sudo docker build -t node-docker .

This will subsequently run through the 6 steps outlined within our Dockerfile and build our complete Docker image.

Step 4: Running our Docker Image

Once our Docker image has been successfully built, we can then go about running one or more Docker containers based on this image by running the following command:

sudo docker run -d -p 9000:3000 node-docker

This will start up a Docker container-based off our node-docker docker image and expose it on port 9000 on our machine.

Step 4: Pushing to Docker Hub

First, you need to create an account on the Docker hub and make sure you remember the login credentials as it is needed further.

sudo docker login provide you credentials here

sudo docker ps -a — Note down the container id

sudo docker commit 25fdwe1q3rd raghav131/node-docker

sudo docker images

sudo docker push raghav131/node-docker

--

--

Raghav Jindal

Cloud Computing || MERN Stack Development || AWS || Penetration testing || AI & ML || Virtualization||Data Science