将容器化应用部署到 Azure

本指南将教你如何

  • 为你的应用程序创建容器镜像。
  • 将镜像推送至容器注册表。
  • 将镜像部署到 Azure App Service 或 Azure Container Apps。

前提条件

创建应用程序镜像

如果你已经有了一个镜像,请跳过此步骤,直接前往 将镜像推送至容器注册表 步骤。

  1. 在 VS Code 中打开应用程序文件夹。

  2. 打开命令面板(⇧⌘P (Windows, Linux Ctrl+Shift+P))并使用 Container Images: Build Image... 命令来构建镜像。

    Build container image

    你可以在 Build Image 命令的输出中找到镜像名称,同样也可以在 Container Explorer 的 Images 窗格中找到它。

    Build image output

将镜像推送至容器注册表

在将镜像部署到 App Service 或 Container App 之前,必须先将镜像上传到容器注册表。镜像可以上传到 Azure 容器注册表 (ACR)Docker Hub

  1. 打开 Container Explorer,在 Registries 组下选择 Connect Registry... 图标,并按照提示操作。选择提供商(Azure 或 Docker Hub)并提供凭据以连接到注册表。

    Connect to Registry

  2. 现在,该注册表将显示在 Registries 下方。

    Registries

  3. 可选:为镜像打标签。为了将镜像上传到注册表,需要使用注册表名称为镜像打标签,以便 docker push 命令能将其上传到正确的注册表。

    • 要在 Azure ACR 中创建注册表,请打开 Container Explorer 的 Registries 部分(如果尚未登录,请先登录 Azure),然后右键单击要使用的订阅,并选择 Create Registry

    • 在上一节中构建的镜像将显示在 Container Explorer 的 Images 部分下。右键单击它并选择 Tag...

      Tag image

    • 指定新名称 <your registry or username>/<image name>:<tag> 并完成打标签操作。例如,对于名为 WebApp6 的 ACR,新镜像名称将是 'webapp6.azurecr.io/webapp6:latest',对于 Docker Hub,将是 'myusername/webapp6:latest'。

  4. 该镜像将显示在 Container Explorer 中镜像标签所指向的注册表下方。选择此镜像并选择 Push。如果尚未为镜像打标签,系统将提示你选择要推送到的注册表,并且镜像将根据你的选择自动打标签。

    Push image

  5. 推送命令完成后,刷新推送了镜像的注册表节点,上传的镜像将会显示出来。

    Refresh registry

将镜像部署到 Azure

在上一节中,镜像已被推送至远程容器注册表。现在,将此镜像部署到 Azure App Service 或 Azure Container Apps。

  1. 在 Container Explorer 中,导航到 Registries 下的镜像,右键单击标签,然后选择 Deploy Image To Azure App Service...Deploy Image to Azure Container Apps...

    Deploy to Azure App Service

  2. 出现提示时,提供 App Service 或 Container App 的值。

    • 新 Web 应用名称:该名称在整个 Azure 中必须是唯一的。
    • 资源组:选择现有的资源组或创建一个新资源组。
    • App Service 计划:选择现有的 App Service 计划或创建一个新计划。(App Service 计划定义了托管网站的物理资源;对于本教程,你可以使用基本层或免费层计划)。
  3. 部署完成后,Visual Studio Code 会显示一条包含网站 URL 的通知。

    Deployment complete notification

  4. 你还可以在 Visual Studio Code 的 Output 面板的 Container Tools 部分中查看结果。

    Deployment complete output

  5. 要浏览已部署的网站,可以使用 Ctrl+click 在 Output 面板中打开 URL。你可能需要等待一会儿,以便应用在 Azure 上正式运行。新的 App Service 或 Container App 也会出现在 Visual Studio Code 的 Azure 视图中,你可以在其中右键单击网站并选择 Browse Website

    Web Application

下一步计划

阅读下文以了解更多关于以下内容的信息:

  • Azure 扩展 - VS Code 扩展市场拥有数百个适用于 Azure 和云服务的扩展。
  • 部署到 Azure - 逐步学习如何将应用程序部署到 Azure。
  • 使用 MongoDB - 在 VS Code 内部创建、管理和查询 MongoDB 数据库。
English 한국어 中文(简体) 中文(繁體)
© . This website operates independently and is not affiliated with or endorsed by Microsoft. All brand names, logos, and trademarks are the property of their respective owners.