快速入门:使用 Azure CLI 在 Azure 中部署容器实例Quickstart: Deploy a container instance in Azure using the Azure CLI

使用 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 CLI 部署一个独立的 Docker 容器,并使其应用程序可通过完全限定的域名 (FQDN) 使用。In this quickstart, you use the Azure CLI to deploy an isolated Docker container and make its application available with a fully qualified domain name (FQDN). 在执行单个部署命令几秒钟之后,可以浏览到正在容器中运行的应用程序:A few seconds after you execute a single deployment command, you can browse to the application running in the container:

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

如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅If you don't have an Azure trail subscription, create a trial subscription before you begin.

先决条件Prerequisites

  • 如果需要,请安装 Azure CLI 来运行 CLI 参考命令。If you prefer, install the Azure CLI to run CLI reference commands.

    • 如果使用的是本地安装,请使用 az login 命令登录到 Azure CLI。If you're using a local installation, sign in to the Azure CLI by using the az login command. 若要完成身份验证过程,请遵循终端中显示的步骤。To finish the authentication process, follow the steps displayed in your terminal. 有关其他登录选项,请参阅登录 Azure CLIFor additional sign-in options, see Sign in with the Azure CLI.

    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。When you're prompted, install Azure CLI extensions on first use. 有关扩展详细信息,请参阅使用 Azure CLI 的扩展For more information about extensions, see Use extensions with the Azure CLI.

    • 运行 az version 以查找安装的版本和依赖库。Run az version to find the version and dependent libraries that are installed. 若要升级到最新版本,请运行 az upgradeTo upgrade to the latest version, run az upgrade.

备注

请先运行 az cloud set -n AzureChinaCloud 更改云环境,然后才能在 Azure 中国中使用 Azure CLI。Before you can use Azure CLI in Azure China , please run az cloud set -n AzureChinaCloud first to change the cloud environment. 若要切换回 Azure 公有云,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Azure Public Cloud, run az cloud set -n AzureCloud again.

  • 本快速入门需要 Azure CLI 2.0.55 或更高版本。This quickstart requires version 2.0.55 or later of the Azure CLI.

创建资源组Create a resource group

Azure 容器实例(例如所有 Azure 资源)都必须部署到资源组中。Azure container instances, like all Azure resources, must be deployed into a resource group. 使用资源组可以组织和管理相关的 Azure 资源。Resource groups allow you to organize and manage related Azure resources.

首先,使用以下 az group create 命令在 chinaeast2 位置中创建一个名为“myResourceGroup”的资源组:First, create a resource group named myResourceGroup in the chinaeast2 location with the following az group create command:

az group create --name myResourceGroup --location chinaeast2

创建容器Create a container

创建资源组后,可在 Azure 中运行容器。Now that you have a resource group, you can run a container in Azure. 若要使用 Azure CLI 创建容器实例,请在 az container create 命令中提供资源组名称、容器实例名称和 Docker 容器映像。To create a container instance with the Azure CLI, provide a resource group name, container instance name, and Docker container image to the az container create command. 本快速入门将使用公共 mcr.microsoft.com/azuredocs/aci-helloworld 映像。In this quickstart, you use the public mcr.microsoft.com/azuredocs/aci-helloworld image. 此映像打包了一个用 Node.js 编写的小型 Web 应用程序,该应用程序提供静态 HTML 页面。This image packages a small web app written in Node.js that serves a static HTML page.

可以通过指定要打开的一个或多个端口、一个 DNS 名称标签(或同时指定两者)来向 Internet 公开容器。You can expose your containers to the internet by specifying one or more ports to open, a DNS name label, or both. 在本快速入门中,你将部署一个具有 DNS 名称标签的容器,以便 Web 应用可供公开访问。In this quickstart, you deploy a container with a DNS name label so that the web app is publicly reachable.

执行类似于以下的命令以启动容器实例。Execute a command similar to the following to start a container instance. 设置在创建实例的 Azure 区域中唯一的 --dns-name-label 值。Set a --dns-name-label value that's unique within the Azure region where you create the instance. 如果收到“DNS 名称标签不可用”错误消息,请尝试使用一个不同的 DNS 名称标签。If you receive a "DNS name label not available" error message, try a different DNS name label.

az container create --resource-group myResourceGroup --name mycontainer --image mcr.microsoft.com/azuredocs/aci-helloworld --dns-name-label aci-demo --ports 80

在几秒钟内,你应当会从 Azure CLI 收到响应,它指出部署已完成。Within a few seconds, you should get a response from the Azure CLI indicating that the deployment has completed. 使用 az container show 命令检查其状态:Check its status with the az container show command:

az container show --resource-group myResourceGroup --name mycontainer --query "{FQDN:ipAddress.fqdn,ProvisioningState:provisioningState}" --out table

运行此命令时,会显示容器的完全限定域名 (FQDN) 及其预配状态。When you run the command, the container's fully qualified domain name (FQDN) and its provisioning state are displayed.

FQDN                                                  ProvisioningState
---------------------------------                     -------------------
aci-demo.chinaeast 2.azurecontainer.console.azure.cn  Succeeded

如果容器的 ProvisioningStateSucceeded,则在浏览器中转到其 FQDN。If the container's ProvisioningState is Succeeded, go to its FQDN in your browser. 如果看到类似于下图的网页,那么恭喜你!If you see a web page similar to the following, congratulations! 现已成功将 Docker 容器中运行的应用程序部署到 Azure。You've successfully deployed an application running in a Docker container to Azure.

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

如果应用程序起初未显示,你可能需要在 DNS 传播时等待几秒钟,然后刷新浏览器。If at first the application isn't displayed, you might need to wait a few seconds while DNS propagates, then try refreshing your browser.

拉取容器日志Pull the container logs

当需要对容器或它运行的应用程序进行故障排除时(或者只是查看其输出),请首先查看容器实例的日志。When you need to troubleshoot a container or the application it runs (or just see its output), start by viewing the container instance's logs.

使用 az container logs 命令拉取容器实例日志:Pull the container instance logs with the az container logs command:

az container logs --resource-group myResourceGroup --name mycontainer

此输出显示容器的日志,并应显示在浏览器中查看应用程序时生成的 HTTP GET 请求。The output displays the logs for the container, and should show the HTTP GET requests generated when you viewed the application in your browser.

listening on port 80
::ffff:10.240.255.55 - - [21/Mar/2019:17:43:53 +0000] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"
::ffff:10.240.255.55 - - [21/Mar/2019:17:44:36 +0000] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"
::ffff:10.240.255.55 - - [21/Mar/2019:17:44:36 +0000] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"

附加输出流Attach output streams

除了查看日志之外,还可将本地标准输出和标准错误流附加到容器的数据流中。In addition to viewing the logs, you can attach your local standard out and standard error streams to that of the container.

首先,执行 az container attach 命令来将本地控制台附加到容器的输出流:First, execute the az container attach command to attach your local console to the container's output streams:

az container attach --resource-group myResourceGroup --name mycontainer

附加后,刷新浏览器数次,以生成其他一些输出。Once attached, refresh your browser a few times to generate some additional output. 完成后,使用 Control+C 分离控制台。When you're done, detach your console with Control+C. 应该会看到与下面类似的输出:You should see output similar to the following:

Container 'mycontainer' is in state 'Running'...
(count: 1) (last timestamp: 2019-03-21 17:27:20+00:00) pulling image "mcr.microsoft.com/azuredocs/aci-helloworld"
(count: 1) (last timestamp: 2019-03-21 17:27:24+00:00) Successfully pulled image "mcr.microsoft.com/azuredocs/aci-helloworld"
(count: 1) (last timestamp: 2019-03-21 17:27:27+00:00) Created container
(count: 1) (last timestamp: 2019-03-21 17:27:27+00:00) Started container

Start streaming logs:
listening on port 80

::ffff:10.240.255.55 - - [21/Mar/2019:17:43:53 +0000] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"
::ffff:10.240.255.55 - - [21/Mar/2019:17:44:36 +0000] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"
::ffff:10.240.255.55 - - [21/Mar/2019:17:44:36 +0000] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"
::ffff:10.240.255.55 - - [21/Mar/2019:17:47:01 +0000] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"
::ffff:10.240.255.56 - - [21/Mar/2019:17:47:12 +0000] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"

清理资源Clean up resources

完成容器的操作后,可使用 az container delete 命令将其删除:When you're done with the container, remove it using the az container delete command:

az container delete --resource-group myResourceGroup --name mycontainer

若要验证已删除该容器,请执行 az container list 命令:To verify that the container has been deleted, execute the az container list command:

az container list --resource-group myResourceGroup --output table

mycontainer 容器不应出现在命令的输出中。The mycontainer container should not appear in the command's output. 如果资源组中没有任何其他容器,则不会显示任何输出。If you have no other containers in the resource group, no output is displayed.

如果已使用完 myResourceGroup 资源组及其包含的所有资源,请使用 az group delete 命令将其删除:If you're done with the myResourceGroup resource group and all the resources it contains, delete it with the az group delete command:

az group delete --name myResourceGroup

后续步骤Next steps

在本快速入门中,你已使用公共 Microsoft 映像创建了一个 Azure 容器实例。In this quickstart, you created an Azure container instance by using a public Microsoft 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.

若要试用在 Azure 上的业务流程系统中运行容器的选项,请参阅 Azure Kubernetes Service (AKS) 快速入门。To try out options for running containers in an orchestration system on Azure, see the Azure Kubernetes Service (AKS) quickstarts.