Deploy a container group

Azure Container Instances support the deployment of multiple containers onto a single host using a container group. This is useful when building an application sidecar for logging, monitoring, or any other configuration where a service needs a second attached process.

This document walks through running a simple multi-container sidecar configuration using an Azure Resource Manager template.

Configure the template

Create a file named azuredeploy.json and copy the following json into it.

In this sample, a container group with two containers and a public IP address is defined. The first container of the group runs an internet facing application. The second container, the sidecar, makes an HTTP request to the main web application via the group's local network.

This sidecar example could be expanded to trigger an alert if it received an HTTP response code other than 200 OK.

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
  },
  "variables": {
    "container1name": "aci-tutorial-app",
    "container1image": "microsoft/aci-helloworld:latest",
    "container2name": "aci-tutorial-sidecar",    
    "container2image": "microsoft/aci-tutorial-sidecar"
  },
    "resources": [
      {
        "name": "myContainerGroup",
        "type": "Microsoft.ContainerInstance/containerGroups",
        "apiVersion": "2017-08-01-preview",
        "location": "[resourceGroup().location]",
        "properties": {
          "containers": [
            {
              "name": "[variables('container1name')]",
              "properties": {
                "image": "[variables('container1image')]",
                "resources": {
                  "requests": {
                    "cpu": 1,
                    "memoryInGb": 1.5
                    }
                },
                "ports": [
                  {
                    "port": 80
                  }
                ]
              }
            },
            {
              "name": "[variables('container2name')]",
              "properties": {
                "image": "[variables('container2image')]",
                "resources": {
                  "requests": {
                    "cpu": 1,
                    "memoryInGb": 1.5
                    }
                }
              }
            }
          ],
          "osType": "Linux",
          "ipAddress": {
            "type": "Public",
            "ports": [
              {
                "protocol": "tcp",
                "port": "80"
              }
            ]
          }
        }
      }
    ],
    "outputs": {
      "containerIPv4Address": {
        "type": "string",
        "value": "[reference(resourceId('Microsoft.ContainerInstance/containerGroups/', 'myContainerGroup')).ipAddress.ip]"
      }
    }
  }

To use a private container image registry, add an object to the json document with the following format.

"imageRegistryCredentials": [
    {
    "server": "[parameters('imageRegistryLoginServer')]",
    "username": "[parameters('imageRegistryUsername')]",
    "password": "[parameters('imageRegistryPassword')]"
    }
]

Deploy the template

Create a resource group with the az group create command.

az group create --name myResourceGroup --location westus

Deploy the template with the az group deployment create command.

az group deployment create --name myContainerGroup --resource-group myResourceGroup --template-file azuredeploy.json

Within a few seconds, you will receive an initial response from Azure.

View deployment state

To view the state of the deployment, use the az container show command. This returns the provisioned public IP address over which the application can be accessed.

az container show --name myContainerGroup --resource-group myResourceGroup -o table

Output:

Name              ResourceGroup    ProvisioningState    Image                                                             IP:ports           CPU/Memory    OsType    Location
----------------  ---------------  -------------------  ----------------------------------------------------------------  -----------------  ------------  --------  ----------
myContainerGroup  myResourceGrou2  Succeeded            microsoft/aci-tutorial-sidecar,microsoft/aci-tutorial-app:v1      40.118.253.154:80  1.0 core/1.5 gb   Linux     westus

View logs

View the log output of a container using the az container logs command. The --container-name argument specifies the container from which to pull logs. In this example, the first container is specified.

az container logs --name myContainerGroup --container-name aci-tutorial-app --resource-group myResourceGroup

Output:

istening on port 80
::1 - - [27/Jul/2017:17:35:29 +0000] "HEAD / HTTP/1.1" 200 1663 "-" "curl/7.54.0"
::1 - - [27/Jul/2017:17:35:32 +0000] "HEAD / HTTP/1.1" 200 1663 "-" "curl/7.54.0"
::1 - - [27/Jul/2017:17:35:35 +0000] "HEAD / HTTP/1.1" 200 1663 "-" "curl/7.54.0"
::1 - - [27/Jul/2017:17:35:38 +0000] "HEAD / HTTP/1.1" 200 1663 "-" "curl/7.54.0"

To see the logs for the side-car container, run the same command specifying the second container name.

az container logs --name myContainerGroup --container-name aci-tutorial-sidecar --resource-group myResourceGroup

Output:

Every 3.0s: curl -I http://localhost                                                                                                                       Mon Jul 17 11:27:36 2017

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0  0  1663    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 1663
Content-Type: text/html; charset=utf-8
Last-Modified: Sun, 16 Jul 2017 02:08:22 GMT
Date: Mon, 17 Jul 2017 18:27:36 GMT

As you can see, the sidecar is periodically making an HTTP request to the main web application via the group's local network to ensure that it is running.

Next steps

This document covered the steps needed for deploying a multi-container Azure container instance. For an end to end Azure Container Instances experience, see the Azure Container Instances tutorial.