Quantcast
Channel: nodejs – Sweetcode.io
Viewing all articles
Browse latest Browse all 14

Running containerized node.js on azure web app for containers.

$
0
0
app service plan

The goal of this article is to show how to run docker images on azure container for web apps. using the docker hub registry as the image repository

Microsoft Azure is a good place to run and work with containerized applications, there are multiple azure resources targeted at devops and deploying containerized scenarios. The container ecosystem built on azure is designed to provide all what you could needed including:

  1. CI/CD tools to develop, update, and manage containerized applications e.g. Azure DevOps
  2. Container Registry: to store images and deploy to your preferred targets in place of e.g. Azure Container Registry (Docker hub).
  3. Deployments: to run containers apps For running container instance For example, Microsoft Azure has at least three services for playing with containers:
  4. – Azure Container Instance
    – Azure Kubernetes Service
    – Azure Web App Service for containers

To demonstrate this, the example below shows how to deploy an instance of a simple node express applications image in the docker registry running to the azure web app for containers.

It shows how to build the image and push to docker hub which is deployed to azure.

Setting up the application

To get started the application is a simple node express app that return an hello world string.

Server.js - const express = require('express');
const PORT = 8080;
const HOST = '0.0.0.0';
const app = express();
app.get('/', (req, res) => {
  res.send('Hello world\n');
});
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

Dockerfile - FROM node:8
WORKDIR /usr/src/app
COPY package.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "npm", "start" ]

Build the Docker image

Using the docker view command – docker build . -t /node-azure-app

Push Image to Docker Registry

The docker registry host container images. In order to deploy the app to azure, you must push it to a registry.

docker push  /node-azure-app

It is recommended to test application locally to ensure it works well.

Publishing and deploying on azure

Azure CLI makes working with Azure resources a lot simpler, the CLI provides a great experience for managing Azure resources. making scripting easy. After downloading and installing the CLI.

Login from windows powershell using the az login then enter the necessary requirements. A sample output is shown below.

You have logged in. Now let us find all the subscriptions to which you have access…

[
  {
    "cloudName": "AzureCloud",
    "id": "269a4111-b6f2-4e2c-93b3-f73a9db02043",
    "isDefault": true,
    "name": "Visual Studio Enterprise: BizSpark",
    "state": "Enabled",
    "tenantId": "c44e27a0-aa2d-48c4-9f01-58d851fddfbe",
    "user": {
  	"name": "*********@outlook.com",
  	"type": "user"
    }
  }
]
 

Resource group: For this purpose, I created a new resource group to hold the app this step can be skipped if a resource group already exist

az group create --name nodeContainAzure --location "West Europe"

App service plan: For this purpose, I created a new App service plan to hold the app this step can be skipped if a service plan already exist

 
az appservice plan create --name nodeContainerService --resource-group nodeContainAzure --sku B1 --is-linux

Create a web app connected to the docker image: Using the az webapp create command, a new webapp is created, the docker flag is added to connected it to the published image

az webapp create --resource-group nodeContainAzure --plan nodeContainerService --name nodeDockerAzure --deployment-container-image-name dockerUsername/node-azure-app

Expose Port: To You tell Azure about the port that the image uses, a WEBSITES_PORT app setting is configured based on docker file above to expose port 8080.

>> az webapp config appsettings set --resource-group nodeContainAzure --name nodeDockerAzure --settings WEBSITES_PORT=8080
[
  {
    "name": "WEBSITES_ENABLE_APP_SERVICE_STORAGE",
    "slotSetting": false,
    "value": "false"
  },
  {
    "name": "WEBSITES_PORT",
    "slotSetting": false,
    "value": "8080"
  }
]

Test the web app

Verify that the web app works by browsing to it (http://nodedockerazure.azurewebsites.net).

Update the app

At every update the app is rebuilt and push the new Docker image. Then the webapp is restarted on the azure portal for the changes to be effected. Using the same build and push command stated above.

For example the index path of the app is set to return the index.html file bellow

app.get('/', (req, res) => {
 res.sendFile(path.join(__dirname,  'index.html'));
});




	
	
	
	Document
	


	

Here Now!

Conclusion

It is no news that container technology has become more popular as companies seek to develop more Microservice based applications in the Cloud. Cloud providers like Azure therefore see the need to support such technology has become critical in a very competitive market. As demonstrated above, Azure Container Service provides hosting of a selected container Orchestrator which will run the containerized application.
Essentially Web app for containers provides the recommended Azure infrastructure you will need to run the Container and running it on the app service plan allows for easy scalability that comes with Azure.

The post Running containerized node.js on azure web app for containers. appeared first on Sweetcode.io.


Viewing all articles
Browse latest Browse all 14

Trending Articles