My personal notes
T

Operations on ACI with Cloud CLI

This page lists some scripts useful for working with [[azure-container-instances]].

Create ACI Container group

This command starts a container instance. It takes a few minutes to complete the operation.

az container create
    --resource-group az204-aci-rg
    --name mycontainer
    --image mcr.microsoft.com/azuredocs/aci-helloworld
    --ports 80
    --dns-name-label aci-example-8580:
    --location westeurope

Resource Group, Name, and Sku are mandatory.

Note:

  • name must be lowercase, between 5 and 50 chars
  • sku: Basic, Premium, Standard.

It returns the whole structure

{
  "confidentialComputeProperties": null,
  "containers": [
    {
      "command": null,
      "environmentVariables": [],
      "image": "mcr.microsoft.com/azuredocs/aci-helloworld",
      "instanceView": {
        "currentState": {
          "detailStatus": "",
          "exitCode": null,
          "finishTime": null,
          "startTime": "2024-04-08T14:31:47.904000+00:00",
          "state": "Running"
        },
        "events": [
          {
            "count": 1,
            "firstTimestamp": "2024-04-08T14:31:37+00:00",
            "lastTimestamp": "2024-04-08T14:31:37+00:00",
            "message": "pulling image \"mcr.microsoft.com/azuredocs/aci-helloworld@sha256:565dba8ce20ca1a311c2d9485089d7ddc935dd50140510050345a1b0ea4ffa6e\"",
            "name": "Pulling",
            "type": "Normal"
          },
          {
            "count": 1,
            "firstTimestamp": "2024-04-08T14:31:38+00:00",
            "lastTimestamp": "2024-04-08T14:31:38+00:00",
            "message": "Successfully pulled image \"mcr.microsoft.com/azuredocs/aci-helloworld@sha256:565dba8ce20ca1a311c2d9485089d7ddc935dd50140510050345a1b0ea4ffa6e\"",
            "name": "Pulled",
            "type": "Normal"
          },
          {
            "count": 1,
            "firstTimestamp": "2024-04-08T14:31:47+00:00",
            "lastTimestamp": "2024-04-08T14:31:47+00:00",
            "message": "Started container",
            "name": "Started",
            "type": "Normal"
          }
        ],
        "previousState": null,
        "restartCount": 0
      },
      "livenessProbe": null,
      "name": "mycontainer",
      "ports": [
        {
          "port": 80,
          "protocol": "TCP"
        }
      ],
      "readinessProbe": null,
      "resources": {
        "limits": null,
        "requests": {
          "cpu": 1.0,
          "gpu": null,
          "memoryInGb": 1.5
        }
      },
      "securityContext": null,
      "volumeMounts": null
    }
  ],
  "diagnostics": null,
  "dnsConfig": null,
  "encryptionProperties": null,
  "extensions": null,
  "id": "/subscriptions/9d91e956-5671-4675-b4ac-309a15956a53/resourceGroups/az204-aci-rg/providers/Microsoft.ContainerInstance/containerGroups/mycontainer",
  "identity": null,
  "imageRegistryCredentials": null,
  "initContainers": [],
  "instanceView": {
    "events": [],
    "state": "Running"
  },
  "ipAddress": {
    "autoGeneratedDomainNameLabelScope": "Unsecure",
    "dnsNameLabel": "aci-example-8580",
    "fqdn": "aci-example-8580.westeurope.azurecontainer.io",
    "ip": "20.238.174.61",
    "ports": [
      {
        "port": 80,
        "protocol": "TCP"
      }
    ],
    "type": "Public"
  },
  "location": "westeurope",
  "name": "mycontainer",
  "osType": "Linux",
  "priority": null,
  "provisioningState": "Succeeded",
  "resourceGroup": "az204-aci-rg",
  "restartPolicy": "Always",
  "sku": "Standard",
  "subnetIds": null,
  "tags": {},
  "type": "Microsoft.ContainerInstance/containerGroups",
  "volumes": null,
  "zones": null
}

Examples

Create a container in a container group with 1 core and 1Gb of memory.

az container create
  -g MyResourceGroup
  --name myapp
  --image myimage:latest
  --cpu 1
  --memory

Create a container in a container group that runs Windows, with 2 cores and 3.5Gb of memory.

az container create
  -g MyResourceGroup
  --name mywinapp
  --image winappimage:latest
  --os-type Windows
  --cpu 2
  --memory 3.5

Create a container in a container group with public IP address, ports and DNS name label.

az container create
  -g MyResourceGroup
  --name myapp
  --image myimage:latest
  --ports 80 443
  --dns-name-label contoso

Create a container in a container group that invokes a script upon start using [[azure-aci-restart-policies]].

az container create
  -g MyResourceGroup
  --name myapp
  --image myimage:latest
  --command-line "/bin/sh -c '/path to/myscript.sh'"

Create a container in a container group that runs a command and stop the container afterwards.

az container create
  -g MyResourceGroup
  --name myapp
  --image myimage:latest
  --command-line "echo hello"
  --restart-policy Never

Create a container in a container group with [[azure-aci-environment-variables]].

az container create
  -g MyResourceGroup
  --name myapp
  --image myimage:latest
  --environment-variables key1=value1 key2=value2

Create a container in a container group using container image from [[azure-container-registry]].

az container create
  -g MyResourceGroup
  --name myapp
  --image myAcrRegistry.azurecr.io/myimage:latest
  --registry-password password

Create a container in a container group that mounts an Azure File share as [[azure-aci-volumes]].

az container create
  -g MyResourceGroup
  --name myapp
  --image myimage:latest
  --command-line "cat /mnt/azfile/myfile"
  --azure-file-volume-share-name myshare
  --azure-file-volume-account-name mystorageaccount
  --azure-file-volume-account-key mystoragekey
  --azure-file-volume-mount-path /mnt/azfile

Create a container in a container group that mounts a git repo as volume.

az container create
  -g MyResourceGroup
  --name myapp
  --image myimage:latest
  --command-line "cat /mnt/gitrepo"
  --gitrepo-url https://github.com/user/myrepo.git
  --gitrepo-dir ./dir1
  --gitrepo-mount-path /mnt/gitrepo

Create a container in a container group using a yaml file.

az container create
  -g MyResourceGroup
  -f containerGroup.yaml

Create a container group using Log Analytics from a workspace name.

az container create
  -g MyResourceGroup
  --name myapp
  --log-analytics-workspace myworkspace

Create a container group with a system assigned identity.

az container create
  -g MyResourceGroup
  --name myapp
  --image myimage:latest
  --assign-identity

Create a container group with a system assigned identity. The group will have a 'Contributor' role with access to a storage account.

az container create
  -g MyResourceGroup
  --name myapp
  --image myimage:latest
  --assign-identity
  --scope /subscriptions/99999999-1bf0-4dda-aec3-cb9272f09590/MyResourceGroup/myRG/providers/Microsoft.Storage/storageAccounts/storage1

Create a container group with a user assigned identity.

az container create
  -g MyResourceGroup
  --name myapp
  --image myimage:latest
  --assign-identity /subscriptions/mySubscriptionId/resourcegroups/myRG/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myID

Create a container group with both system and user assigned identity.

az container create
  -g MyResourceGroup
  --name myapp
  --image myimage:latest
  --assign-identity  [system]/subscriptions/mySubscriptionId/resourcegroups/myRG/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myID

Show status of a container

To see the internal status, you can run

az container show
    --resource-group az204-aci-rg
    --name mycontainer
    --query "{FQDN:ipAddress.fqdn,ProvisioningState:provisioningState}"

The query is a [[JMESPath]] query string.

Show Graph Visualisation