Bird
Raised Fist0
Azurecloud~5 mins

Azure Container Instances (ACI) for simple runs - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Sometimes you want to run a small app or task in the cloud without setting up a full server or complex system. Azure Container Instances lets you quickly run containers in the cloud without managing servers.
When you want to run a one-time script or job in a container without managing servers
When you need to test a containerized app quickly in the cloud
When you want to run a small web app or API without setting up a full Kubernetes cluster
When you want to run background tasks or batch jobs on demand
When you want to avoid the overhead of managing virtual machines for simple container runs
Config File - aci-deployment.yaml
aci-deployment.yaml
apiVersion: 2019-12-01
location: eastus
name: mycontainerinstance
properties:
  containers:
  - name: myapp
    properties:
      image: mcr.microsoft.com/azuredocs/aci-helloworld
      resources:
        requests:
          cpu: 1
          memoryInGb: 1.5
  osType: Linux
  restartPolicy: Never
  ipAddress:
    type: Public
    ports:
    - protocol: tcp
      port: 80
  tags: {}
type: Microsoft.ContainerInstance/containerGroups

This YAML file defines an Azure Container Instance named mycontainerinstance in the eastus region.

It runs a container called myapp using a simple hello world image.

The container requests 1 CPU and 1.5 GB memory.

The OS is Linux and the container will not restart automatically after it stops.

It exposes port 80 publicly so you can access the app from the internet.

Commands
Create a resource group in Azure to hold your container instance. This groups related resources together.
Terminal
az group create --name myResourceGroup --location eastus
Expected OutputExpected
{ "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup", "location": "eastus", "managedBy": null, "name": "myResourceGroup", "properties": { "provisioningState": "Succeeded" }, "tags": {}, "type": "Microsoft.Resources/resourceGroups" }
--name - Sets the name of the resource group
--location - Sets the Azure region for the resource group
Create the Azure Container Instance using the YAML file. This deploys the container to the cloud.
Terminal
az container create --resource-group myResourceGroup --file aci-deployment.yaml
Expected OutputExpected
{ "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.ContainerInstance/containerGroups/mycontainerinstance", "location": "eastus", "name": "mycontainerinstance", "properties": { "containers": [ { "name": "myapp", "properties": { "image": "mcr.microsoft.com/azuredocs/aci-helloworld", "resources": { "requests": { "cpu": 1, "memoryInGb": 1.5 } }, "instanceView": { "state": "Running" } } } ], "ipAddress": { "ip": "52.170.12.34", "ports": [ { "port": 80, "protocol": "TCP" } ], "type": "Public" }, "osType": "Linux", "provisioningState": "Succeeded", "restartPolicy": "Never" }, "tags": {}, "type": "Microsoft.ContainerInstance/containerGroups" }
--resource-group - Specifies the resource group to deploy to
--file - Specifies the YAML file with container configuration
Get the public IP address of the container instance so you can access the app in a browser.
Terminal
az container show --resource-group myResourceGroup --name mycontainerinstance --query ipAddress.ip --output tsv
Expected OutputExpected
52.170.12.34
--query - Filters output to show only the IP address
--output - Formats output as plain text
View the logs from the container to check its output or debug if needed.
Terminal
az container logs --resource-group myResourceGroup --name mycontainerinstance
Expected OutputExpected
Hello from Azure Container Instances!
Key Concept

If you remember nothing else from this pattern, remember: Azure Container Instances let you run containers quickly in the cloud without managing servers.

Common Mistakes
Not creating a resource group before deploying the container
Azure requires a resource group to organize resources; deployment will fail without it.
Always run 'az group create' first to create a resource group.
Using a container image that does not expose the port defined in the YAML
The container won't be reachable on the expected port, causing connection failures.
Ensure the container image listens on the port you expose in the configuration.
Forgetting to check the container's IP address after deployment
Without the IP, you cannot access the running container's app or service.
Use 'az container show' with query to get the public IP.
Summary
Create a resource group to hold your Azure resources.
Deploy the container instance using a YAML configuration file.
Retrieve the container's public IP to access the app.
Check container logs to verify it is running correctly.

Practice

(1/5)
1. What is the main benefit of using Azure Container Instances (ACI) for running containers?
easy
A. It only supports Windows containers.
B. You must manage virtual machines manually.
C. It requires setting up complex Kubernetes clusters.
D. You can run containers without managing servers or infrastructure.

Solution

  1. Step 1: Understand ACI purpose

    ACI is designed to let users run containers easily without managing servers or infrastructure.
  2. Step 2: Compare options

    Options B and C require manual management or complex setups, which ACI avoids. It only supports Windows containers. is incorrect because ACI supports Linux containers too.
  3. Final Answer:

    You can run containers without managing servers or infrastructure. -> Option D
  4. Quick Check:

    ACI = serverless container runs [OK]
Hint: ACI means no server management needed [OK]
Common Mistakes:
  • Thinking ACI requires managing VMs
  • Confusing ACI with Kubernetes
  • Assuming ACI supports only Windows containers
2. Which of the following is the correct Azure CLI command to create a container instance named mycontainer with image nginx in resource group mygroup?
easy
A. az container deploy --group mygroup --container mycontainer --image nginx
B. az container create --resource-group mygroup --name mycontainer --image nginx
C. az container start --resource-group mygroup --name mycontainer --image nginx
D. az container run --resource-group mygroup --name mycontainer --image nginx

Solution

  1. Step 1: Identify correct command for creating ACI

    The Azure CLI command to create a container instance is az container create.
  2. Step 2: Check parameters

    Correct parameters include --resource-group, --name, and --image. az container create --resource-group mygroup --name mycontainer --image nginx matches this syntax exactly.
  3. Final Answer:

    az container create --resource-group mygroup --name mycontainer --image nginx -> Option B
  4. Quick Check:

    Use az container create to create containers [OK]
Hint: Create containers with 'az container create' command [OK]
Common Mistakes:
  • Using 'az container deploy' which is invalid
  • Confusing 'start' with 'create'
  • Using 'az container run' which does not exist
3. Given this Azure CLI command:
az container create --resource-group mygroup --name testcontainer --image busybox --command-line "sleep 30" --cpu 1 --memory 1.5 --restart-policy Never

What will happen when you run this container?
medium
A. The container runs the sleep command for 30 seconds and then stops without restarting.
B. The container runs indefinitely and restarts automatically if it stops.
C. The container fails because the restart policy is invalid.
D. The container runs the sleep command but restarts immediately after finishing.

Solution

  1. Step 1: Analyze command parameters

    The command runs sleep 30, so the container will pause for 30 seconds. The restart policy is set to Never, so it will not restart after finishing.
  2. Step 2: Understand restart behavior

    Since restart policy is Never, the container stops after the command completes and does not restart.
  3. Final Answer:

    The container runs the sleep command for 30 seconds and then stops without restarting. -> Option A
  4. Quick Check:

    Restart policy 'Never' means no restart after exit [OK]
Hint: Restart policy 'Never' stops container after command ends [OK]
Common Mistakes:
  • Assuming container restarts automatically
  • Confusing restart policies like 'Always' vs 'Never'
  • Thinking the container runs indefinitely
4. You tried to create an Azure Container Instance with this command:
az container create --resource-group mygroup --name myapp --image nginx --cpu two --memory 1.5

But it failed. What is the most likely cause?
medium
A. The resource group 'mygroup' does not exist.
B. The memory value 1.5 is too low for nginx containers.
C. The CPU value 'two' is invalid; it must be a number like 1 or 2.
D. The image name 'nginx' is incorrect and must include a version tag.

Solution

  1. Step 1: Check CPU parameter format

    The CPU parameter expects a numeric value (like 1 or 2). Using the word 'two' is invalid syntax.
  2. Step 2: Validate other parameters

    Memory 1.5 is valid, 'nginx' image defaults to latest tag, and resource group existence is not guaranteed but error message usually specifies that.
  3. Final Answer:

    The CPU value 'two' is invalid; it must be a number like 1 or 2. -> Option C
  4. Quick Check:

    CPU must be numeric, not words [OK]
Hint: CPU must be a number, not text [OK]
Common Mistakes:
  • Using words instead of numbers for CPU
  • Assuming memory 1.5 is invalid
  • Thinking image must always have version tag
5. You want to run a batch job that processes data and then stops automatically. Which ACI configuration is best to ensure the container runs once and does not restart after completion?
hard
A. Set --restart-policy Never and specify the batch job command.
B. Set --restart-policy Always to keep the container running.
C. Use --restart-policy OnFailure to restart only on errors.
D. Do not specify a restart policy; the default will stop the container.

Solution

  1. Step 1: Understand restart policies for batch jobs

    For batch jobs that run once and stop, --restart-policy Never ensures the container does not restart after finishing.
  2. Step 2: Evaluate other options

    Always restarts indefinitely, OnFailure restarts only on errors, and no restart policy defaults to Always, which is not suitable.
  3. Final Answer:

    Set --restart-policy Never and specify the batch job command. -> Option A
  4. Quick Check:

    Batch jobs use restart policy 'Never' to stop after run [OK]
Hint: Use restart policy 'Never' for one-time batch jobs [OK]
Common Mistakes:
  • Using 'Always' causing infinite restarts
  • Assuming default restart policy stops container
  • Choosing 'OnFailure' which restarts on errors only