Swiftorial Logo
Home
Swift Lessons
Matchuup
CodeSnaps
Tutorials
Career
Resources

Docker on Azure

Deploying Docker containers on Azure allows you to leverage Azure's robust cloud infrastructure to manage and scale your applications. This guide covers key concepts, steps to deploy Docker containers on Azure, examples, and best practices for deploying Dockerized Express.js applications on Azure.

Key Concepts of Docker on Azure

  • Azure Container Instances (ACI): ACI provides a simple and quick way to run containers in Azure without managing virtual machines.
  • Azure Kubernetes Service (AKS): AKS is a managed Kubernetes service that simplifies deploying and managing Kubernetes clusters in Azure.
  • Azure Container Registry (ACR): ACR is a managed Docker registry service based on the open-source Docker Registry 2.0.
  • Azure Virtual Machines (VMs): VMs provide scalable computing resources in Azure.
  • Azure Resource Manager (ARM): ARM is the deployment and management service for Azure.

Setting Up the Project

Initialize a new Express.js project and create a Dockerfile:

// Initialize a new project
// npm init -y

// Install Express
// npm install express

// Create the project structure
// mkdir src
// touch src/index.js Dockerfile .dockerignore .gitignore

// .gitignore
node_modules
.env

// .dockerignore
node_modules
npm-debug.log

Creating an Express Application

Create a simple Express application:

Example: index.js

// src/index.js
const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
    res.send('Hello, Docker on Azure!');
});

app.listen(port, () => {
    console.log(`Server running at http://localhost:${port}/`);
});

Creating a Dockerfile

Create a Dockerfile to containerize your Express application:

Example: Dockerfile

// Dockerfile
FROM node:14

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
COPY package*.json ./
RUN npm install

# Bundle app source
COPY . .

# Expose port 3000 to the outside world
EXPOSE 3000

# Run app when the container launches
CMD ["node", "src/index.js"]

Building and Running the Docker Container

Build and run the Docker container for your Express application:

// Build the Docker image
docker build -t my-express-app .

// Run the Docker container
docker run -d -p 3000:3000 --name my-express-app my-express-app

// Open http://localhost:3000 in your browser to see the application running

Setting Up Azure

Install Azure CLI, create an ACR repository, and push your Docker image to ACR:

Install Azure CLI

// On a Linux or macOS system
// Install Azure CLI using a package manager or download from the official website
brew install azure-cli

// Log in to Azure
az login

Create an ACR Repository

// Create a resource group
az group create --name myResourceGroup --location eastus

// Create an ACR repository
az acr create --resource-group myResourceGroup --name myACR --sku Basic

Push Docker Image to ACR

// Log in to ACR
az acr login --name myACR

// Tag the Docker image
docker tag my-express-app:latest myacr.azurecr.io/my-express-app:latest

// Push the Docker image to ACR
docker push myacr.azurecr.io/my-express-app:latest

Deploying to Azure Container Instances (ACI)

Create a container instance to run your Docker container:

// Create a container instance
az container create --resource-group myResourceGroup --name myExpressApp --image myacr.azurecr.io/my-express-app:latest --cpu 1 --memory 1 --registry-login-server myacr.azurecr.io --registry-username  --registry-password  --dns-name-label myexpressapp --ports 3000

Deploying to Azure Kubernetes Service (AKS)

Create an AKS cluster, deploy your Docker container using Kubernetes manifests:

Create an AKS Cluster

// Create an AKS cluster
az aks create --resource-group myResourceGroup --name myAKSCluster --node-count 1 --enable-addons monitoring --generate-ssh-keys

// Get AKS credentials
az aks get-credentials --resource-group myResourceGroup --name myAKSCluster

Deploy Docker Container to AKS

// Create a Kubernetes deployment file
// deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-express-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-express-app
  template:
    metadata:
      labels:
        app: my-express-app
    spec:
      containers:
      - name: my-express-app
        image: myacr.azurecr.io/my-express-app:latest
        ports:
        - containerPort: 3000

// Create a Kubernetes service file
// service.yaml
apiVersion: v1
kind: Service
metadata:
  name: my-express-app
spec:
  type: LoadBalancer
  ports:
  - port: 3000
    targetPort: 3000
  selector:
    app: my-express-app

// Apply the deployment and service
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml

// Get the external IP of the service
kubectl get services

Best Practices for Docker on Azure

  • Use Managed Services: Leverage managed services like ACI and AKS to simplify container orchestration and management.
  • Enable Monitoring and Logging: Use Azure Monitor and Azure Log Analytics to monitor and log your container instances and clusters.
  • Secure Your Containers: Use Azure Security Center to secure your containerized applications and Azure Key Vault to manage secrets.
  • Auto Scale: Configure auto-scaling for your AKS clusters to handle varying loads.
  • Use CI/CD Pipelines: Implement CI/CD pipelines with Azure DevOps or GitHub Actions to automate the deployment process.

Testing Docker on Azure

Test your Dockerized application deployed on Azure to ensure it works correctly:

Example: Testing with Mocha and Chai

// Install Mocha and Chai
// npm install --save-dev mocha chai

// test/app.test.js
const chai = require('chai');
const expect = chai.expect;
const axios = require('axios');

describe('Express App', () => {
    it('should return Hello, Docker on Azure!', async () => {
        const response = await axios.get('http://.eastus.azurecontainer.io:3000');
        expect(response.data).to.equal('Hello, Docker on Azure!');
    });
});

// Add test script to package.json
// "scripts": {
//   "test": "mocha"
// }

// Run tests
// npm test

Key Points

  • Azure Container Instances (ACI): ACI provides a simple and quick way to run containers in Azure without managing virtual machines.
  • Azure Kubernetes Service (AKS): AKS is a managed Kubernetes service that simplifies deploying and managing Kubernetes clusters in Azure.
  • Azure Container Registry (ACR): ACR is a managed Docker registry service based on the open-source Docker Registry 2.0.
  • Azure Virtual Machines (VMs): VMs provide scalable computing resources in Azure.
  • Azure Resource Manager (ARM): ARM is the deployment and management service for Azure.
  • Follow best practices for Docker on Azure, such as using managed services, enabling monitoring and logging, securing your containers, auto-scaling, and implementing CI/CD pipelines.

Conclusion

Deploying Docker containers on Azure allows you to leverage Azure's robust cloud infrastructure to manage and scale your applications. By understanding and implementing the key concepts, steps, examples, and best practices covered in this guide, you can effectively deploy Dockerized Express.js applications on Azure. Happy coding!