快速入门:使用 ARM 模板在 Azure 中部署容器实例

使用 Azure 容器实例在 Azure 中快速方便地运行无服务器 Docker 容器。 当你不需要像 AzureKubernetes 服务这样的完整容器业务流程平台时,可以按需将应用程序部署到容器实例。 本快速入门将使用 Azure 资源管理器模板(ARM 模板)部署一个独立的 Docker 容器,并使其 Web 应用可通过公共 IP 地址使用。

资源管理器模板是定义项目基础结构和配置的 JavaScript 对象表示法 (JSON) 文件。 模板使用声明性语法。 在声明性语法中,你可以在不编写创建部署的编程命令序列的情况下,描述预期部署。

如果你的环境满足先决条件,并且你熟悉如何使用 ARM 模板,请选择“部署到 Azure”按钮。 Azure 门户中会打开模板。

用于将资源管理器模板部署到 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.5.6.12127",
      "templateHash": "17016281914347876853"
    }
  },
  "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": "2021-09-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": {
    "containerIPv4Address": {
      "type": "string",
      "value": "[reference(resourceId('Microsoft.ContainerInstance/containerGroups', parameters('name'))).ipAddress.ip]"
    }
  }
}

模板中定义了以下资源:

可以在快速入门模板库中找到更多 Azure 容器实例模板示例。

部署模板

  1. 选择下图登录到 Azure 并打开一个模板。 该模板将在另一位置创建注册表和副本。

    用于将资源管理器模板部署到 Azure 的按钮。

  2. 选择或输入以下值。

    • 订阅:选择一个 Azure 订阅。
    • 资源组:选择“新建”,为资源组输入一个独一无二的名称,然后选择“确定”。
    • 位置:选择资源组的位置。 示例:中国东部 2
    • 名称:接受为实例生成的名称,或者输入一个名称。
    • 映像:接受默认映像名称。 此示例 Linux 映像打包了一个用 Node.js 编写的小型 Web 应用,该应用提供静态 HTML 页面。

    对于剩余的属性,请接受默认值。

    查看条款和条件。 如果你同意,请选择“我同意上述条款和条件”。

    模板属性

  3. 成功创建实例后,你会收到通知:

    门户通知

使用 Azure 门户部署模板。 除了 Azure 门户之外,还可以使用 Azure PowerShell、Azure CLI 和 REST API。 若要了解其他部署方法,请参阅部署模板

查看已部署的资源

使用 Azure 门户或诸如 Azure CLI 之类的工具来查看容器实例的属性。

  1. 在门户中,搜索“容器实例”,然后选择你创建的容器实例。

  2. 在“概览”页上,记下实例的“状态”及其“IP 地址” 。

    实例概览

  3. 在其状态为“正在运行”后,在浏览器中导航到 IP 地址。

    在浏览器中显示的使用 Azure 容器实例部署的应用

查看容器日志

当排查容器或其运行的应用程序的问题时,查看容器实例的日志非常有用。

若要查看容器的日志,请在“设置”下选择“容器”>“日志”。 应当会看到在浏览器中查看应用程序时生成的 HTTP GET 请求。

Azure 门户中的容器日志

清理资源

使用完容器后,在容器实例的“概览”页上选择“删除”。 出现提示时,确认删除。

后续步骤

在本快速入门中,你已基于公共 Microsoft 映像创建了一个 Azure 容器实例。 若要基于专用 Azure 容器注册表生成容器映像并部署它,请继续学习 Azure 容器实例教程。

有关引导你完成模板创建过程的分步教程,请参阅: