你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

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

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

Azure 资源管理器模板是定义项目基础结构和配置的 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 容器实例教程。

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