Let's Talk Cloud: Creating and Configuring Containers in Azure

Hey cloud explorers! Welcome back to our "Let's Talk Cloud" series. Today we're diving into one of my favorite topics “Containers!” If you've been in the tech world for more than five minutes, you've probably heard the buzz about containers, but you might be wondering how to actually use them in Azure.

Well, you're in luck because that's exactly what we're covering today!

Why Containers?

Before we jump into the Azure specifics, let's quickly touch on why containers have taken the tech world by storm:

  • Consistency: That age-old excuse "but it works on my machine" becomes a thing of the past

  • Portability: Build once, run anywhere—from your laptop to any cloud

  • Efficiency: Lighter and faster than virtual machines

  • Scalability: Spin up in seconds, perfect for variable workloads

  • Isolation: Keep applications and their dependencies neatly packaged

If VMs are like moving houses (heavy, taking everything with you), containers are like taking just your backpack with exactly what you need.

Container Options in Azure

Azure offers several ways to run containers, each suited for different scenarios:

Azure Container Instances (ACI): The Simplest Way to Run Containers

If you're just getting started with containers or need to run something quickly without managing infrastructure, Azure Container Instances is your friend. It's the serverless option for containers—no VMs to manage, no clusters to configure.

ACI is perfect for:

  • Simple applications

  • Batch jobs

  • CI/CD workloads

  • Testing and development

You can deploy a container with just a few clicks in the portal or a simple Azure CLI command:

az container create --resource-group myResourceGroup \
    --name mycontainer --image myimage:latest \
    --dns-name-label mydnsname --ports 80

Azure Kubernetes Service (AKS): For Production and Complex Apps

When your containerized applications grow more complex or need advanced orchestration features, Azure Kubernetes Service (AKS) is the way to go. AKS provides a managed Kubernetes environment where Azure handles much of the management overhead.

AKS excels for:

  • Production workloads

  • Microservices architectures

  • Applications requiring auto-scaling

  • Apps needing advanced networking and storage integration

The learning curve is steeper than ACI, but the capabilities are much more powerful.

Azure App Service: PaaS with Container Support

Did you know Azure App Service also supports containers? If you like the PaaS experience but want to use your own containers, this is a great middle ground. You get features like easy deployments, scaling, and SSL management without having to deal with the underlying infrastructure.

Azure Container Registry (ACR): Storing Your Images

No matter which compute option you choose, you'll need somewhere to store your container images. Azure Container Registry integrates seamlessly with all Azure container services and adds features like:

  • Geo-replication for global deployments

  • Vulnerability scanning

  • Automated builds

  • Private networks support

Creating Your First Container

Let's get practical! Here's how to create and deploy your first container in Azure:

Step 1: Create a Dockerfile

Every container starts with a Dockerfile—a set of instructions that define what goes in your container. Here's a simple example for a Node.js web app:

FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD ["node", "server.js"]

This tells Docker to:

  1. Start with the official Node.js 14 image

  2. Set the working directory to /app

  3. Copy package files and install dependencies

  4. Copy the rest of your application code

  5. Expose port 8080

  6. Run your server.js file when the container starts

Step 2: Build Your Container Image

With Docker installed on your development machine, you can build your image:

docker build -t myapp:latest .

Step 3: Create an Azure Container Registry

Now let's create a place to store your image in Azure:

az acr create --resource-group myResourceGroup \
    --name myacr --sku Basic

Step 4: Push Your Image to ACR

First, log in to your registry:

az acr login --name myacr

Tag your image for your registry:

docker tag myapp:latest myacr.azurecr.io/myapp:latest

Push the image:

docker push myacr.azurecr.io/myapp:latest

Step 5: Deploy to Azure Container Instances

Now the fun part—deploying your container:

az container create --resource-group myResourceGroup \
    --name mycontainer \
    --image myacr.azurecr.io/myapp:latest \
    --registry-login-server myacr.azurecr.io \
    --registry-username $(az acr credential show --name myacr --query username -o tsv) \
    --registry-password $(az acr credential show --name myacr --query passwords[0].value -o tsv) \
    --dns-name-label mydnsname \
    --ports 8080

Voilà! Your container is running in Azure. You can access it at http://mydnsname.region.azurecontainer.io:8080.

Container Configuration Best Practices

Now that you know the basics, let's talk about configuring your containers effectively:

Environment Variables: Configuration Without Rebuilding

Instead of hardcoding configuration in your container, use environment variables:

az container create --resource-group myResourceGroup \
    --name mycontainer \
    --image myacr.azurecr.io/myapp:latest \
    --environment-variables \
        DB_HOST=mydbserver.database.windows.net \
        API_KEY=myapikey \
        ENVIRONMENT=production

For sensitive values, use the --secure-environment-variables parameter.

Persistent Storage: When Containers Need State

Containers are ephemeral by default—any data written inside the container is lost when it restarts. For persistent data, mount Azure File Shares:

az container create --resource-group myResourceGroup \
    --name mycontainer \
    --image myacr.azurecr.io/myapp:latest \
    --azure-file-volume-share-name myfileshare \
    --azure-file-volume-account-name mystorageaccount \
    --azure-file-volume-account-key mystoragekey \
    --azure-file-volume-mount-path /data

Container Health Probes: Ensuring Availability

For containers running in AKS, configure liveness and readiness probes:

livenessProbe:
  httpGet:
    path: /health
    port: 8080
  initialDelaySeconds: 30
  periodSeconds: 10
readinessProbe:
  httpGet:
    path: /ready
    port: 8080
  initialDelaySeconds: 5
  periodSeconds: 5

Liveness probes check if your container is running properly, while readiness probes determine if it's ready to accept traffic.

Resource Limits: Preventing Noisy Neighbors

Always set CPU and memory limits for your containers:

az container create --resource-group myResourceGroup \
    --name mycontainer \
    --image myacr.azurecr.io/myapp:latest \
    --cpu 1 \
    --memory 1.5

Or in Kubernetes:

resources:
  limits:
    cpu: "1"
    memory: "1.5Gi"
  requests:
    cpu: "0.5"
    memory: "1Gi"

Network Configuration: Connecting Containers

For simple container groups in ACI, you can enable container-to-container communication:

az container create --resource-group myResourceGroup \
    --name mycontainergroup \
    --image myacr.azurecr.io/frontend:latest \
    --image myacr.azurecr.io/backend:latest \
    --ports 80 8080

For more complex networking, especially in AKS, you might use:

  • Service meshes like Istio

  • Network policies for security

  • Ingress controllers for HTTP routing

Advanced Container Scenarios

Ready to take your container skills to the next level? Here are some advanced scenarios:

Multi-Container Applications with Docker Compose

For applications with multiple containers, you can use Docker Compose with ACI:

az container create --resource-group myResourceGroup \
    --file docker-compose.yml

CI/CD for Containers

Automate your container builds and deployments with Azure DevOps or GitHub Actions:

  1. Trigger builds on code changes

  2. Automatically build and push container images

  3. Deploy to staging environments

  4. Run automated tests

  5. Promote to production

Container Monitoring

Keep an eye on your containers with:

  • Azure Monitor for container insights

  • Log Analytics for centralized logging

  • Application Insights for application performance monitoring

Have you started using containers in your Azure environment? What challenges have you faced? Drop a comment below—I'd love to hear about your experiences!

Until next time, keep containerizing!

0
Subscribe to my newsletter

Read articles from Samuel Happiness directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Samuel Happiness
Samuel Happiness

I'm a passionate and innovative software developer, I thrive on crafting elegant solutions that drive real-world impact. With a strong foundation in hands-on experience in mobile and web development, I am adept at turning complex problems into user-friendly applications.