Creating Containers on Azure

This guide will walk you through the various methods and services available on Azure for creating and deploying containerized applications.

Azure Container Options

Azure offers several robust services to host and manage your containers, catering to different needs and complexities:

Creating Containers with Azure Container Instances (ACI)

ACI is a great starting point for running individual containers. You can create a container group using the Azure CLI or Azure Portal.

Using Azure CLI

First, ensure you have the Azure CLI installed and logged in:

az login

Then, create a container instance. This example deploys a simple Nginx container:

az container create \
    --resource-group MyResourceGroup \
    --name mynginxcontainer \
    --image nginx \
    --dns-name-label mynginxapp \
    --ports 80

Replace MyResourceGroup with your desired resource group name and mynginxcontainer with your container name.

Using Azure Portal

  1. Navigate to the Azure Portal (portal.azure.com).
  2. Search for "Container Instances" and select it.
  3. Click "Create".
  4. Fill in the required details: Subscription, Resource Group, Container Name, Image Source (e.g., Docker Hub), Image, OS Type, Size, and Networking details (like DNS name).
  5. Review and create the container instance.
Tip: For quick testing or running single applications, ACI is incredibly efficient.

Creating Containers with Azure Kubernetes Service (AKS)

AKS is designed for orchestrating complex, multi-container applications. The process involves creating an AKS cluster and then deploying your containerized applications to it using Kubernetes manifests.

Creating an AKS Cluster

You can create an AKS cluster using the Azure CLI:

az aks create \
    --resource-group MyResourceGroup \
    --name MyAKSCluster \
    --node-count 1 \
    --enable-addons monitoring \
    --generate-ssh-keys

This command creates a cluster with one node. You can adjust --node-count as needed.

Connecting to the Cluster

Once the cluster is created, you need to configure your `kubectl` command-line tool to connect to it:

az aks get-credentials --resource-group MyResourceGroup --name MyAKSCluster

Deploying a Container to AKS

You typically deploy applications to AKS using YAML manifest files. Here's a simple example for deploying a single Nginx deployment and exposing it via a LoadBalancer service:

Create a file named nginx-deployment.yaml with the following content:


apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer

            

Apply this manifest to your AKS cluster:

kubectl apply -f nginx-deployment.yaml
Note: For production environments, consider using Helm charts for managing Kubernetes applications, which offers templating and dependency management.

Creating Containers with Azure App Service

App Service allows you to run web applications from custom container images. This is a good option when you need a PaaS solution for your web applications and want to leverage Docker.

Using Azure CLI

Create a Web App resource and specify your container image:

az group create --name MyResourceGroup --location eastus
az appservice plan create --name MyPlan --resource-group MyResourceGroup --is-linux
az webapp create \
    --resource-group MyResourceGroup \
    --plan MyPlan \
    --name mywebappcontainer \
    --deployment-container-image-name nginx:latest
Tip: App Service for Containers simplifies container deployments by abstracting away much of the underlying infrastructure management.

Next Steps

Once your containers are created, you'll likely want to explore: