快速入门:使用 ARM 模板在 Azure 中部署容器实例Quickstart: Deploy a container instance in Azure using an ARM template

使用 Azure 容器实例在 Azure 中快速方便地运行无服务器 Docker 容器。Use Azure Container Instances to run serverless Docker containers in Azure with simplicity and speed. 当你不需要像 AzureKubernetes 服务这样的完整容器业务流程平台时,可以按需将应用程序部署到容器实例。Deploy an application to a container instance on-demand when you don't need a full container orchestration platform like Azure Kubernetes Service. 本快速入门将使用 Azure 资源管理器模板(ARM 模板)部署一个独立的 Docker 容器,并使其 Web 应用可通过公共 IP 地址使用。In this quickstart, you use an Azure Resource Manager template (ARM template) to deploy an isolated Docker container and make its web application available with a public IP address.

ARM 模板是定义项目基础结构和配置的 JavaScript 对象表示法 (JSON) 文件。An ARM template is a JavaScript Object Notation (JSON) file that defines the infrastructure and configuration for your project. 该模板使用声明性语法,使你可以声明要部署的内容,而不需要编写一系列编程命令来进行创建。The template uses declarative syntax, which lets you state what you intend to deploy without having to write the sequence of programming commands to create it.

如果你的环境满足先决条件,并且你熟悉如何使用 ARM 模板,请选择“部署到 Azure”按钮。If your environment meets the prerequisites and you're familiar with using ARM templates, select the Deploy to Azure button. Azure 门户中会打开模板。The template will open in the Azure portal.

部署到 Azure Deploy to Azure

先决条件Prerequisites

如果没有 Azure 订阅,请在开始之前创建一个免费帐户。If you don't have an Azure subscription, create a free account before you begin.

查看模板Review the template

本快速入门中使用的模板来自 Azure 快速启动模板The template used in this quickstart is from Azure Quickstart Templates.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "name": {
      "type": "string",
      "defaultValue": "acilinuxpublicipcontainergroup",
      "metadata": {
        "description": "Name for the container group"
      }
    },
    "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": "string",
      "defaultValue": "80",
      "metadata": {
        "description": "Port to open on the container and the public IP address."
      }
    },
    "cpuCores": {
      "type": "string",
      "defaultValue": "1.0",
      "metadata": {
        "description": "The number of CPU cores to allocate to the container."
      }
    },
    "memoryInGb": {
      "type": "string",
      "defaultValue": "1.5",
      "metadata": {
        "description": "The amount of memory to allocate to the container in gigabytes."
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    },
    "restartPolicy": {
      "type": "string",
      "defaultValue": "always",
      "allowedValues": [
        "never",
        "always",
        "onfailure"
      ],
      "metadata": {
        "description": "The behavior of Azure runtime if container has stopped."
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.ContainerInstance/containerGroups",
      "apiVersion": "2019-12-01",
      "name": "[parameters('name')]",
      "location": "[parameters('location')]",
      "properties": {
        "containers": [
          {
            "name": "[parameters('name')]",
            "properties": {
              "image": "[parameters('image')]",
              "ports": [
                {
                  "port": "[parameters('port')]"
                }
              ],
              "resources": {
                "requests": {
                  "cpu": "[parameters('cpuCores')]",
                  "memoryInGb": "[parameters('memoryInGb')]"
                }
              }
            }
          }
        ],
        "osType": "Linux",
        "restartPolicy": "[parameters('restartPolicy')]",
        "ipAddress": {
          "type": "Public",
          "ports": [
            {
              "protocol": "Tcp",
              "port": "[parameters('port')]"
            }
          ]
        }
      }
    }
  ],
  "outputs": {
    "containerIPv4Address": {
      "type": "string",
      "value": "[reference(resourceId('Microsoft.ContainerInstance/containerGroups/', parameters('name'))).ipAddress.ip]"
    }
  }
}

模板中定义了以下资源:The following resource is defined in the template:

  • Microsoft.ContainerInstance/containerGroups:创建 Azure 容器组。Microsoft.ContainerInstance/containerGroups: create an Azure container group. 此模板定义一个组,其中包含单个容器实例。This template defines a group consisting of a single container instance.

可以在快速入门模板库中找到更多 Azure 容器实例模板示例。More Azure Container Instances template samples can be found in the quickstart template gallery.

部署模板Deploy the template

  1. 选择下图登录到 Azure 并打开一个模板。Select the following image to sign in to Azure and open a template. 该模板将在另一位置创建注册表和副本。The template creates a registry and a replica in another location.

    部署到 Azure Deploy to Azure

  2. 选择或输入以下值。Select or enter the following values.

    • 订阅:选择一个 Azure 订阅。Subscription: select an Azure subscription.
    • 资源组:选择“新建”,为资源组输入一个独一无二的名称,然后选择“确定”。 Resource group: select Create new, enter a unique name for the resource group, and then select OK.
    • 位置:选择资源组的位置。Location: select a location for the resource group. 示例:中国东部 2Example: China East 2.
    • 名称:接受为实例生成的名称,或者输入一个名称。Name: accept the generated name for the instance, or enter a name.
    • 映像:接受默认映像名称。Image: accept the default image name. 此示例 Linux 映像打包了一个用 Node.js 编写的小型 Web 应用,该应用提供静态 HTML 页面。This sample Linux image packages a small web app written in Node.js that serves a static HTML page.

    对于剩余的属性,请接受默认值。Accept default values for the remaining properties.

    查看条款和条件。Review the terms and conditions. 如果你同意,请选择“我同意上述条款和条件”。If you agree, select I agree to the terms and conditions stated above.

    模板属性

  3. 成功创建实例后,你会收到通知:After the instance has been created successfully, you get a notification:

    门户通知

使用 Azure 门户部署模板。The Azure portal is used to deploy the template. 除了 Azure 门户之外,还可以使用 Azure PowerShell、Azure CLI 和 REST API。In addition to the Azure portal, you can use the Azure PowerShell, Azure CLI, and REST API. 若要了解其他部署方法,请参阅部署模板To learn other deployment methods, see Deploy templates.

查看已部署的资源Review deployed resources

使用 Azure 门户或诸如 Azure CLI 之类的工具来查看容器实例的属性。Use the Azure portal or a tool such as the Azure CLI to review the properties of the container instance.

  1. 在门户中,搜索“容器实例”,然后选择你创建的容器实例。In the portal, search for Container Instances, and select the container instance you created.

  2. 在“概览”页上,记下实例的“状态”及其“IP 地址” 。On the Overview page, note the Status of the instance and its IP address.

    实例概览

  3. 在其状态为“正在运行”后,在浏览器中导航到 IP 地址。Once its status is Running, navigate to the IP address in your browser.

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

查看容器日志View container logs

当排查容器或其运行的应用程序的问题时,查看容器实例的日志非常有用。Viewing the logs for a container instance is helpful when troubleshooting issues with your container or the application it runs.

若要查看容器的日志,请在“设置”下选择“容器” > “日志”。 To view the container's logs, under Settings, select Containers > Logs. 应当会看到在浏览器中查看应用程序时生成的 HTTP GET 请求。You should see the HTTP GET request generated when you viewed the application in your browser.

Azure 门户中的容器日志

清理资源Clean up resources

使用完容器后,在容器实例的“概览”页上选择“删除”。 When you're done with the container, on the Overview page for the container instance, select Delete. 出现提示时,确认删除。When prompted, confirm the deletion.

后续步骤Next steps

在本快速入门中,你已基于公共 Azure 映像创建了一个 Azure 容器实例。In this quickstart, you created an Azure container instance from a public Azure image. 若要基于专用 Azure 容器注册表生成容器映像并部署它,请继续学习 Azure 容器实例教程。If you'd like to build a container image and deploy it from a private Azure container registry, continue to the Azure Container Instances tutorial.

有关引导你完成模板创建过程的分步教程,请参阅:For a step-by-step tutorial that guides you through the process of creating a template, see: