0
0
Azurecloud~5 mins

Why containers on Azure matter - Why It Works

Choose your learning style9 modes available
Introduction
Containers help you package your app and everything it needs to run in one box. Azure lets you run these containers easily in the cloud, so your app works the same everywhere and can grow when many people use it.
When you want to move your app to the cloud without changing how it works on your computer.
When you need to run many copies of your app to handle lots of users at the same time.
When you want to update your app often without stopping it for a long time.
When you want to save money by using only the cloud resources your app needs at the moment.
When you want to keep your app safe and separate from other apps running on the same cloud.
Commands
This command creates a container registry in Azure where you can store your container images safely.
Terminal
az acr create --resource-group example-group --name exampleRegistry --sku Basic
Expected OutputExpected
{ "adminUserEnabled": false, "creationDate": "2024-06-01T12:00:00Z", "id": "/subscriptions/xxxx/resourceGroups/example-group/providers/Microsoft.ContainerRegistry/registries/exampleRegistry", "location": "eastus", "loginServer": "exampleRegistry.azurecr.io", "name": "exampleRegistry", "resourceGroup": "example-group", "sku": { "name": "Basic", "tier": "Basic" }, "status": "Succeeded", "type": "Microsoft.ContainerRegistry/registries" }
--resource-group - Specifies the Azure group where the registry will be created
--name - Sets the unique name for your container registry
--sku - Chooses the pricing tier for the registry
This command logs you into the Azure container registry so you can push and pull container images.
Terminal
az acr login --name exampleRegistry
Expected OutputExpected
Login Succeeded
--name - Specifies which container registry to log into
This command builds your container image from your app code and tags it with the registry address and version.
Terminal
docker build -t exampleRegistry.azurecr.io/my-app:v1 .
Expected OutputExpected
Sending build context to Docker daemon 10.24kB Step 1/5 : FROM node:18-alpine ---> 123abc456def Step 2/5 : WORKDIR /app ---> Using cache ---> 789ghi012jkl Step 3/5 : COPY package*.json ./ ---> Using cache ---> mno345pqr678 Step 4/5 : RUN npm install ---> Running in abc123def456 Removing intermediate container abc123def456 ---> stu901vwx234 Step 5/5 : COPY . . ---> Using cache ---> yz567abc890 Successfully built yz567abc890 Successfully tagged exampleRegistry.azurecr.io/my-app:v1
This command uploads your container image to the Azure container registry so it can be used in the cloud.
Terminal
docker push exampleRegistry.azurecr.io/my-app:v1
Expected OutputExpected
The push refers to repository [exampleRegistry.azurecr.io/my-app] 123abc456def: Pushed 789ghi012jkl: Pushed mno345pqr678: Pushed stu901vwx234: Pushed yz567abc890: Pushed v1: digest: sha256:abcdef1234567890 size: 1578
This command creates a container instance in Azure to run your app with 1 CPU, 1.5 GB memory, and makes it reachable on the internet with a DNS name.
Terminal
az container create --resource-group example-group --name myAppContainer --image exampleRegistry.azurecr.io/my-app:v1 --cpu 1 --memory 1.5 --dns-name-label myappdnslabel --ports 80
Expected OutputExpected
{ "id": "/subscriptions/xxxx/resourceGroups/example-group/providers/Microsoft.ContainerInstance/containerGroups/myAppContainer", "name": "myAppContainer", "location": "eastus", "properties": { "provisioningState": "Succeeded", "containers": [ { "name": "myAppContainer", "properties": { "image": "exampleRegistry.azurecr.io/my-app:v1", "resources": { "requests": { "cpu": 1.0, "memoryInGb": 1.5 } }, "ports": [ { "port": 80 } ] } } ], "ipAddress": { "dnsNameLabel": "myappdnslabel", "ports": [ { "port": 80, "protocol": "TCP" } ], "type": "Public" } }, "type": "Microsoft.ContainerInstance/containerGroups" }
--cpu - Sets how many CPUs the container can use
--memory - Sets how much memory the container can use
--dns-name-label - Creates a public DNS name to access the container
Key Concept

If you remember nothing else from this pattern, remember: containers let you package your app once and run it anywhere in Azure with the same behavior and easy scaling.

Common Mistakes
Trying to push a container image without logging into the Azure container registry first
The push will fail because Docker does not have permission to upload the image
Always run 'az acr login --name exampleRegistry' before pushing images
Not tagging the container image with the full registry login server address before pushing
Docker will try to push to Docker Hub or another default registry and fail
Tag images with 'exampleRegistry.azurecr.io/my-app:tag' before pushing
Creating a container instance without specifying ports or DNS name
Your app will run but you won't be able to access it from the internet
Use '--ports' and '--dns-name-label' flags to expose your app publicly
Summary
Create an Azure container registry to store your container images securely.
Build and tag your container image with the registry address.
Log in to the registry and push your container image.
Create a container instance in Azure to run your app with specified resources and public access.