快速入門:使用ARM範本在 Azure 中部署容器實例

使用 Azure 容器執行個體在 Azure 中簡潔且快速地執行無伺服器 Docker 容器。 當您不需要像 Azure Kubernetes Service 的完整容器協調流程平台時,請視需要將應用程式部署至容器執行個體。 在本快速入門中,您會使用 Azure Resource Manager 範本 (ARM 範本) 來部署隔離的 Docker 容器,並使其 Web 應用程式可供公用 IP 位址使用。

Azure Resource Manager 範本是 JavaScript 物件表示法 (JSON) 檔案,可定義專案的基礎結構和組態。 範本使用宣告式語法。 您不需要撰寫程式設計命令順序來建立部署,即可描述預定的部署。

如果您的環境符合必要條件,而且您很熟悉 ARM 範本,請選取 [部署至 Azure] 按鈕。 範本會在 Azure 入口網站中開啟。

Button to deploy the Resource Manager template to Azure.

必要條件

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶

檢閱範本

本快速入門中使用的範本是來自 Azure 快速入門範本

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.26.54.24096",
      "templateHash": "15999594271314721416"
    }
  },
  "parameters": {
    "name": {
      "type": "string",
      "defaultValue": "acilinuxpublicipcontainergroup",
      "metadata": {
        "description": "Name for the container group"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    },
    "image": {
      "type": "string",
      "defaultValue": "mcr.microsoft.com/azuredocs/aci-helloworld",
      "metadata": {
        "description": "Container image to deploy. Should be of the form repoName/imagename:tag for images stored in public Docker Hub, or a fully qualified URI for other registries. Images from private registries require additional registry credentials."
      }
    },
    "port": {
      "type": "int",
      "defaultValue": 80,
      "metadata": {
        "description": "Port to open on the container and the public IP address."
      }
    },
    "cpuCores": {
      "type": "int",
      "defaultValue": 1,
      "metadata": {
        "description": "The number of CPU cores to allocate to the container."
      }
    },
    "memoryInGb": {
      "type": "int",
      "defaultValue": 2,
      "metadata": {
        "description": "The amount of memory to allocate to the container in gigabytes."
      }
    },
    "restartPolicy": {
      "type": "string",
      "defaultValue": "Always",
      "allowedValues": [
        "Always",
        "Never",
        "OnFailure"
      ],
      "metadata": {
        "description": "The behavior of Azure runtime if container has stopped."
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.ContainerInstance/containerGroups",
      "apiVersion": "2023-05-01",
      "name": "[parameters('name')]",
      "location": "[parameters('location')]",
      "properties": {
        "containers": [
          {
            "name": "[parameters('name')]",
            "properties": {
              "image": "[parameters('image')]",
              "ports": [
                {
                  "port": "[parameters('port')]",
                  "protocol": "TCP"
                }
              ],
              "resources": {
                "requests": {
                  "cpu": "[parameters('cpuCores')]",
                  "memoryInGB": "[parameters('memoryInGb')]"
                }
              }
            }
          }
        ],
        "osType": "Linux",
        "restartPolicy": "[parameters('restartPolicy')]",
        "ipAddress": {
          "type": "Public",
          "ports": [
            {
              "port": "[parameters('port')]",
              "protocol": "TCP"
            }
          ]
        }
      }
    }
  ],
  "outputs": {
    "name": {
      "type": "string",
      "value": "[parameters('name')]"
    },
    "resourceGroupName": {
      "type": "string",
      "value": "[resourceGroup().name]"
    },
    "resourceId": {
      "type": "string",
      "value": "[resourceId('Microsoft.ContainerInstance/containerGroups', parameters('name'))]"
    },
    "containerIPv4Address": {
      "type": "string",
      "value": "[reference(resourceId('Microsoft.ContainerInstance/containerGroups', parameters('name')), '2023-05-01').ipAddress.ip]"
    },
    "location": {
      "type": "string",
      "value": "[parameters('location')]"
    }
  }
}

樣本中定義了下列資源:

如需更多 Azure 容器執行個體 範例,請參閱快速入門範本資源庫

部署範本

  1. 選取以下影像來登入 Azure 並開啟範本。 範本會在另一個位置建立登錄和複本。

    Button to deploy the Resource Manager template to Azure.

  2. 選取或輸入下列值。

    • 訂用帳戶:選取 Azure 訂用帳戶。
    • 資源群組:選取 [ 新建],輸入資源群組的唯一名稱,然後選取 [ 確定]。
    • 位置:選取資源群組的位置。 範例: 美國中部。
    • 名稱:接受實例產生的名稱,或輸入名稱。
    • 映射:接受預設映像名稱。 此範例 Linux 映像會封裝以Node.js撰寫的小型 Web 應用程式,以提供靜態 HTML 頁面。

    接受其餘屬性的預設值。

    檢閱條款及條件。 如果您同意,請選取 [我同意上述條款及條件]。

    Template properties

  3. 成功建立實例之後,您會收到通知:

    Portal notification

Azure 入口網站用於部署範本。 除了 Azure 入口網站 之外,您還可以使用 Azure PowerShell、Azure CLI 和 REST API。 若要了解其他部署方法,請參閱<部署範本>。

檢閱已部署的資源

使用 Azure 入口網站 或 Azure CLI 之類的工具來檢閱容器實例的屬性。

  1. 在入口網站中,搜尋 容器執行個體,然後選取您所建立的容器實例。

  2. 在 [概觀] 頁面上,記下實例的狀態及其IP位址

    Instance overview

  3. 一旦其狀態為 [正在執行],請瀏覽至瀏覽器中的IP位址。

    App deployed using Azure Container Instances viewed in browser

檢視容器記錄

檢視容器實例的記錄有助於針對容器或其執行的應用程式問題進行疑難解答。

若要檢視容器的記錄,請在 [設定] 下,選取 [容器>記錄]。 您應該會在瀏覽器中檢視應用程式時看到產生的 HTTP GET 要求。

Container logs in the Azure portal

清除資源

當您完成容器時, 請在容器實例的 [概觀 ] 頁面上,選取 [ 刪除]。 出現提示時,確認刪除。

下一步

在本快速入門中,您已從公用 Microsoft 映射建立 Azure 容器實例。 如果您想要建置容器映像,並從私人的 Azure 容器登錄進行部署,請繼續進行 Azure Container Instances 教學課程。

如需逐步教學課程,以引導您完成建立範本的流程,請參閱: