教程:将容器应用程序部署到 Azure 容器实例Tutorial: Deploy a container application to Azure Container Instances

这是由三个部分组成的系列教程的最后一个教程。This is the final tutorial in a three-part series. 在本系列教程的前几篇文章中,我们已创建一个容器映像并将其推送到 Azure 容器注册表Earlier in the series, a container image was created and pushed to Azure Container Registry. 本文是本系列教程的最后一篇,介绍如何将容器部署到 Azure 容器实例。This article completes the series by deploying the container to Azure Container Instances.

本教程介绍以下操作:In this tutorial, you:

  • 将容器从 Azure 容器注册表部署到 Azure 容器实例Deploy the container from Azure Container Registry to Azure Container Instances
  • 在浏览器中查看正在运行的应用程序View the running application in the browser
  • 显示容器的日志Display the container's logs

开始之前Before you begin

必须满足以下要求才能完成本教程:You must satisfy the following requirements to complete this tutorial:

Azure CLI:必须在本地计算机上安装 Azure CLI 2.0.29 或更高版本。Azure CLI: You must have Azure CLI version 2.0.29 or later installed on your local computer. 运行 az --version 即可查找版本。Run az --version to find the version. 如需进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install the Azure CLI.

Docker:本教程假设读者基本了解核心 Docker 概念,如容器、容器映像和基本的 docker 命令。Docker: This tutorial assumes a basic understanding of core Docker concepts like containers, container images, and basic docker commands. 有关 Docker 和容器的基础知识,请参阅 Docker 概述For a primer on Docker and container basics, see the Docker overview.

Docker:要完成本教程,需在本地安装 Docker。Docker: To complete this tutorial, you need Docker installed locally. Docker 提供用于在 macOSWindowsLinux 上配置 Docker 环境的包。Docker provides packages that configure the Docker environment on macOS, Windows, and Linux.

重要

必须 在本地计算机 上同时安装 Azure CLI 和 Docker 引擎才能完成本教程。You must install both the Azure CLI and Docker Engine on your local computer to complete this tutorial.

使用 Azure CLI 部署容器Deploy the container using the Azure CLI

在本部分,我们将使用 Azure CLI 来部署第一篇教程中生成的,并已在第二篇教程中推送到 Azure 容器注册表的映像。In this section, you use the Azure CLI to deploy the image built in the first tutorial and pushed to Azure Container Registry in the second tutorial. 在继续之前,请务必先完成这些教程。Be sure you've completed those tutorials before proceeding.

获取注册表凭据Get registry credentials

部署托管在专用 Azure 容器注册表(例如第二篇教程中创建的注册表)中的映像时,必须提供用于访问该注册表的凭据。When you deploy an image that's hosted in a private Azure container registry like the one created in the second tutorial, you must supply credentials to access the registry.

对于许多方案来说,最佳做法是创建和配置对注册表具有“拉取”权限的 Azure Active Directory 服务主体。A best practice for many scenarios is to create and configure an Azure Active Directory service principal with pull permissions to your registry. 请参阅使用 Azure 容器注册表从 Azure 容器实例进行身份验证,获取示例脚本以创建具有必要权限的服务主体。See Authenticate with Azure Container Registry from Azure Container Instances for sample scripts to create a service principal with the necessary permissions. 请记下服务主体 ID 和服务主体密码 。Take note of the service principal ID and service principal password. 部署容器时将使用这些凭据访问注册表。You use these credentials to access the registry when you deploy the container.

还需要容器注册表登录服务器的完整名称(请将 <acrName> 替换为注册表的名称):You also need the full name of the container registry login server (replace <acrName> with the name of your registry):

az acr show --name <acrName> --query loginServer

部署容器Deploy container

现在,使用 az container create 部署容器。Now, use the az container create command to deploy the container. <acrLoginServer> 替换为从前一命令中获得的值。Replace <acrLoginServer> with the value you obtained from the previous command. <service-principal-ID><service-principal-password> 替换为你为访问注册表而创建的服务主体 ID 和密码。Replace <service-principal-ID> and <service-principal-password> with the service principal ID and password that you created to access the registry. <aciDnsLabel> 替换为所需的 DNS 名称。Replace <aciDnsLabel> with a desired DNS name.

az container create --resource-group myResourceGroup --name aci-tutorial-app --image <acrLoginServer>/aci-tutorial-app:v1 --cpu 1 --memory 1 --registry-login-server <acrLoginServer> --registry-username <service-principal-ID> --registry-password <service-principal-password> --dns-name-label <aciDnsLabel> --ports 80

将在几秒钟内收到来自 Azure 的初始响应。Within a few seconds, you should receive an initial response from Azure. 在创建容器实例时所在的 Azure 区域中,--dns-name-label 值必须是唯一的。The --dns-name-label value must be unique within the Azure region you create the container instance. 如果在执行命令时收到 DNS 名称标签错误消息,请修改前一命令中的值。Modify the value in the preceding command if you receive a DNS name label error message when you execute the command.

检查部署进度Verify deployment progress

若要查看部署状态,请使用 az container showTo view the state of the deployment, use az container show:

az container show --resource-group myResourceGroup --name aci-tutorial-app --query instanceView.state

重复 az container show 命令,直到状态从“挂起”更改为“正在运行”为止,此过程应不到 1 分钟 。Repeat the az container show command until the state changes from Pending to Running, which should take under a minute. 当容器状态为“正在运行”时,请继续执行下一步 。When the container is Running, proceed to the next step.

查看应用程序和容器日志View the application and container logs

部署成功后,使用 az container show 命令显示容器的完全限定域名 (FQDN):Once the deployment succeeds, display the container's fully qualified domain name (FQDN) with the az container show command:

az container show --resource-group myResourceGroup --name aci-tutorial-app --query ipAddress.fqdn

例如:For example:

"aci-demo.chinaeast 2.azurecontainer.console.azure.cn"

若要查看正在运行的应用程序,请从喜欢的浏览器中导航到此限定的 DNS 名称:To see the running application, navigate to the displayed DNS name in your favorite browser:

浏览器中的 Hello World 应用

还可查看容器的日志输出:You can also view the log output of the container:

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

示例输出:Example output:

listening on port 80
::ffff:10.240.0.4 - - [21/Jul/2017:06:00:02 +0000] "GET / HTTP/1.1" 200 1663 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"
::ffff:10.240.0.4 - - [21/Jul/2017:06:00:02 +0000] "GET /favicon.ico HTTP/1.1" 404 150 "http://aci-demo.chinaeast2.azurecontainer.console.azure.cn/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"

清理资源Clean up resources

如果不再需要在本系列教程中创建的任何资源,可以执行 az group delete 命令删除资源组和其中包含的所有资源。If you no longer need any of the resources you created in this tutorial series, you can execute the az group delete command to remove the resource group and all resources it contains. 此命令将删除创建的容器注册表、正在运行的容器和所有相关资源。This command deletes the container registry you created, as well as the running container, and all related resources.

az group delete --name myResourceGroup

后续步骤Next steps

本教程已完整演示将容器部署到 Azure 容器实例的过程。In this tutorial, you completed the process of deploying your container to Azure Container Instances. 已完成以下步骤:The following steps were completed:

  • 使用 Azure CLI 从 Azure 容器注册表部署了容器Deployed the container from Azure Container Registry using the Azure CLI
  • 已在浏览器中查看了应用程序Viewed the application in the browser
  • 已查看容器日志Viewed the container logs

有了一定的基础知识后,请继续深入了解 Azure 容器实例,例如容器组的工作原理:Now that you have the basics down, move on to learning more about Azure Container Instances, such as how container groups work: