您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

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

使用 Azure 容器实例在 Azure 中快速方便地运行无服务器 Docker 容器。Use Azure Container Instances to run serverless Docker containers in Azure with simplicity and speed. 开发云原生应用时,如果希望从本地部署无缝切换到云部署,可以按需部署到容器实例。Deploy to a container instance on-demand when you develop cloud-native apps and you want to switch seamlessly from local development to cloud deployment.

本快速入门介绍如何使用本机 Docker CLI 命令部署 Docker 容器,并使其应用程序在 Azure 容器实例中可用。In this quickstart, you use native Docker CLI commands to deploy a Docker container and make its application available in Azure Container Instances. 此功能是通过 Docker 与 Azure 之间的集成启用的。This capability is enabled by integration between Docker and Azure. 在执行 docker run 部署命令几秒钟之后,可以浏览到正在容器中运行的应用程序:A few seconds after you execute a docker run command, you can browse to the application running in the container:

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

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

对于本快速入门,你需要安装适用于 WindowsmacOS 的 Docker Desktop 2.3.0.5 或更高版本。For this quickstart, you need Docker Desktop version 2.3.0.5 or later, available for Windows or macOS. 或者安装适用于 Linux 的 Docker ACI Integration CLIOr install the Docker ACI Integration CLI for Linux.

重要

并非 Azure 容器实例的所有功能都受支持。Not all features of Azure Container Instances are supported. 如需提供有关 Docker 与 Azure 集成的反馈,可在 aci-integration-beta GitHub 存储库中创建问题。Provide feedback about the Docker-Azure integration by creating an issue in the aci-integration-beta GitHub repository.

创建 Azure 上下文Create Azure context

若要使用 Docker 命令在 Azure 容器实例中运行容器,请先登录到 Azure:To use Docker commands to run containers in Azure Container Instances, first log into Azure:

docker login azure

在系统提示时输入或选择 Azure 凭据。When prompted, enter or select your Azure credentials.

通过运行 docker context create aci 创建 ACI 上下文。Create an ACI context by running docker context create aci. 此上下文将 Docker 与 Azure 订阅和资源组相关联,以便你可以创建和管理容器实例。This context associates Docker with an Azure subscription and resource group so you can create and manage container instances. 例如,创建名为 myacicontext 的上下文:For example, to create a context called myacicontext:

docker context create aci myacicontext

出现提示时,选择你的 Azure 订阅 ID,然后选择现有资源组或“创建新的资源组”。When prompted, select your Azure subscription ID, then select an existing resource group or create a new resource group. 如果选择新的资源组,则创建的新资源组将使用系统生成的名称。If you choose a new resource group, it's created with a system-generated name. 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.

运行 docker context ls 以确认已将 ACI 上下文添加到 Docker 上下文:Run docker context ls to confirm that you added the ACI context to your Docker contexts:

docker context ls

创建容器Create a container

创建 Docker 上下文后,可以在 Azure 中创建容器。After creating a Docker context, you can create a container in Azure. 本快速入门将使用公共 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.

首先,切换到 ACI 上下文。First, change to the ACI context. 所有后续 Docker 命令都在此上下文中运行。All subsequent Docker commands run in this context.

docker context use myacicontext

运行以下 docker run 命令,通过向 Internet 公开的端口 80 创建 Azure 容器实例:Run the following docker run command to create the Azure container instance with port 80 exposed to the internet:

docker run -p 80:80 mcr.microsoft.com/azuredocs/aci-helloworld

成功部署的示例输出:Sample output for a successful deployment:

[+] Running 2/2
 ⠿ hungry-kirch            Created                                                                               5.1s
 ⠿ single--container--aci  Done                                                                                 11.3s
hungry-kirch

运行 docker ps 以获取有关正在运行的容器的详细信息,包括公共 IP 地址:Run docker ps to get details about the running container, including the public IP address:

docker ps

示例输出显示公共 IP 地址,在本例中为 52.230.225.232:Sample output shows a public IP address, in this case 52.230.225.232:

CONTAINER ID        IMAGE                                        COMMAND             STATUS              PORTS
hungry-kirch        mcr.microsoft.com/azuredocs/aci-helloworld                       Running             52.230.225.232:80->80/tcp

现在,在浏览器中转到该 IP 地址。Now go to the IP address 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 容器实例部署的应用

拉取容器日志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.

例如,运行 docker logs 命令以查看 ACI 上下文中 hungry-kirch 容器的日志:For example, run the docker logs command to see the logs of the hungry-kirch container in the ACI context:

docker logs hungry-kirch

此输出显示容器的日志,并应显示在浏览器中查看应用程序时生成的 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 - - [07/Jul/2020: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 - - [07/Jul/2020: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 - - [07/Jul/2020: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"

清理资源Clean up resources

对容器完成操作后,运行 docker rm 将其删除。When you're done with the container, run docker rm to remove it. 此命令停止并删除 Azure 容器实例。This command stops and deletes the Azure container instance.

docker rm hungry-kirch

后续步骤Next steps

在本快速入门中,你已使用 Docker 与 Azure 之间的集成从公共映像中创建了 Azure 容器实例。In this quickstart, you created an Azure container instance from a public image by using integration between Docker and Azure. 可以在 Docker 文档中详细了解集成方案。Learn more about integration scenarios in the Docker documentation.

还可以使用用于 Visual Studio Code 的 Docker 扩展来实现一种集成体验,以便开发、运行和管理容器、映像和上下文。You can also use the Docker extension for Visual Studio Code for an integrated experience to develop, run, and manage containers, images, and contexts.

若要使用 Azure 工具创建和管理容器实例,请参阅使用 Azure CLIAzure PowerShellAzure 门户Azure 资源管理器的其他快速入门。To use Azure tools to create and manage container instances, see other quickstarts using the Azure CLI, Azure PowerShell, Azure portal, and Azure Resource Manager template.

如果你希望使用 Docker Compose 在本地定义并运行多容器应用程序,然后再切换到 Azure 容器实例,请继续学习以下教程。If you'd like to use Docker Compose to define and run a multi-container application locally and then switch to Azure Container Instances, continue to the tutorial.