使用 Docker Compose 部署多个容器

本文介绍如何部署多个 Azure AI 容器。 具体而言,其中将会介绍如何使用 Docker Compose 来协调多个 Docker 容器映像。

Docker Compose 是用于定义和运行多容器 Docker 应用程序的工具。 在 Compose 中,可以使用 YAML 文件来配置应用程序的服务。 然后,运行一条命令,即可从配置中创建并启动所有服务。

使用 Compose 可在一台主计算机上方便地协调多个容器映像。 在本文中,我们会将“读取”和“文档智能”容器组合到一起。

先决条件

此过程要求必须在本地安装和运行多个工具:

  • Azure 订阅。 如果没有订阅,请在开始之前创建一个试用帐户
  • Docker 引擎。 确认 Docker CLI 是否可在控制台窗口中工作。
  • 具有适当定价层的 Azure 资源。 只有以下定价层适用于此容器:
    • 仅限使用 F0 或标准定价层的“Azure AI 视觉”资源。
    • 仅限使用 F0 或标准定价层的“文档智能”资源。
    • 具有 S0 定价层的 Azure AI 服务资源
  • 如果使用的是封闭预览版容器,则需要填写在线请求表单才能使用它。

Docker Compose 文件

YAML 文件定义要部署的所有服务。 这些服务依赖于 DockerFile 或现有的容器映像。 在本例中,我们将使用两个预览映像。 复制并粘贴以下 YAML 文件,并将其保存为 docker-compose.yaml。 在文件中提供适当的 apikeybillingEndpointUri 值。

version: '3.7'
services:
  forms:
    image: "mcr.microsoft.com/azure-cognitive-services/form-recognizer/layout"
    environment:
       eula: accept
       billing: # < Your Document Intelligence billing URL >
       apikey: # < Your Document Intelligence API key >
       FormRecognizer__ComputerVisionApiKey: # < Your Document Intelligence API key >
       FormRecognizer__ComputerVisionEndpointUri: # < Your Document Intelligence URI >
    volumes:
       - type: bind
         source: E:\publicpreview\output
         target: /output
       - type: bind
         source: E:\publicpreview\input
         target: /input
    ports:
      - "5010:5000"

  ocr:
    image: "mcr.microsoft.com/azure-cognitive-services/vision/read:3.1-preview"
    environment:
      eula: accept
      apikey: # < Your Azure AI Vision API key >
      billing: # < Your Azure AI Vision billing URL >
    ports:
      - "5021:5000"

重要

volumes 节点下指定的主计算机上创建目录。 之所以需要这样做,是因为在尝试使用卷绑定装载映像之前,目录必须存在。

启动已配置的 Docker Compose 服务

使用 Docker Compose 文件可以管理所定义服务的生命周期中的所有阶段:启动、停止和重新生成服务;查看服务状态;记录流。 从项目目录(docker-compose.yaml 文件所在的位置)打开命令行接口。

注意

为了避免出错,请确保主计算机与 Docker 引擎正确共享驱动器。 例如,如果在 docker-compose.yaml 文件中将 E:\publicpreview 用作目录,请与 Docker 共享驱动器 E 。

在命令行接口中执行以下命令,以启动(或重启)docker-compose.yaml 文件中定义的所有服务:

docker-compose up

当 Docker 首次使用此配置执行 docker-compose up 命令时,它将提取 services 节点下配置的映像,然后下载并装载这些映像:

Pulling forms (mcr.microsoft.com/azure-cognitive-services/form-recognizer/layout:)...
latest: Pulling from azure-cognitive-services/form-recognizer/layout
743f2d6c1f65: Pull complete
72befba99561: Pull complete
2a40b9192d02: Pull complete
c7715c9d5c33: Pull complete
f0b33959f1c4: Pull complete
b8ab86c6ab26: Pull complete
41940c21ed3c: Pull complete
e3d37dd258d4: Pull complete
cdb5eb761109: Pull complete
fd93b5f95865: Pull complete
ef41dcbc5857: Pull complete
4d05c86a4178: Pull complete
34e811d37201: Pull complete
Pulling ocr (mcr.microsoft.com/azure-cognitive-services/vision/read:3.1-preview:)...
latest: Pulling from /azure-cognitive-services/vision/read:3.1-preview
f476d66f5408: Already exists
8882c27f669e: Already exists
d9af21273955: Already exists
f5029279ec12: Already exists
1a578849dcd1: Pull complete
45064b1ab0bf: Download complete
4bb846705268: Downloading [=========================================>         ]  187.1MB/222.8MB
c56511552241: Waiting
e91d2aa0f1ad: Downloading [==============================================>    ]  162.2MB/176.1MB

下载映像后,映像服务将会启动:

Starting docker_ocr_1   ... done
Starting docker_forms_1 ... doneAttaching to docker_ocr_1, docker_forms_1forms_1  | forms_1  | forms_1  | Notice: This Preview is made available to you on the condition that you agree to the Supplemental Terms of Use for Azure Previews [https://go.microsoft.com/fwlink/?linkid=2018815], which supplement your agreement [https://go.microsoft.com/fwlink/?linkid=2018657] governing your use of Azure. If you do not have an existing agreement governing your use of Azure, you agree that your agreement governing use of Azure is the Microsoft Online Subscription Agreement [https://go.microsoft.com/fwlink/?linkid=2018755] (which incorporates the Online Services Terms [https://go.microsoft.com/fwlink/?linkid=2018760]). By using the Preview you agree to these terms.
forms_1  | 
forms_1  | 
forms_1  | Using '/input' for reading models and other read-only data.
forms_1  | Using '/output/forms/812d811d1bcc' for writing logs and other output data.
forms_1  | Logging to console.
forms_1  | Submitting metering to 'https://api.cognitive.azure.cn/'.
forms_1  | WARNING: No access control enabled!
forms_1  | warn: Microsoft.AspNetCore.Server.Kestrel[0]
forms_1  |       Overriding address(es) 'http://+:80'. Binding to endpoints defined in UseKestrel() instead.
forms_1  | Hosting environment: Production
forms_1  | Content root path: /app/forms
forms_1  | Now listening on: http://0.0.0.0:5000
forms_1  | Application started. Press Ctrl+C to shut down.
ocr_1    | 
ocr_1    | 
ocr_1    | Notice: This Preview is made available to you on the condition that you agree to the Supplemental Terms of Use for Azure Previews [https://go.microsoft.com/fwlink/?linkid=2018815], which supplement your agreement [https://go.microsoft.com/fwlink/?linkid=2018657] governing your use of Azure. If you do not have an existing agreement governing your use of Azure, you agree that your agreement governing use of Azure is the Microsoft Online Subscription Agreement [https://go.microsoft.com/fwlink/?linkid=2018755] (which incorporates the Online Services Terms [https://go.microsoft.com/fwlink/?linkid=2018760]). By using the Preview you agree to these terms.
ocr_1    |
ocr_1    | 
ocr_1    | Logging to console.
ocr_1    | Submitting metering to 'https://api.cognitive.azure.cn/'.
ocr_1    | WARNING: No access control enabled!
ocr_1    | Hosting environment: Production
ocr_1    | Content root path: /
ocr_1    | Now listening on: http://0.0.0.0:5000
ocr_1    | Application started. Press Ctrl+C to shut down.

验证服务可用性

提示

可以使用 docker images 命令列出下载的容器映像。 例如,以下命令以表格列出每个下载的容器映像的 ID、存储库和标记:

docker images --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}"

IMAGE ID            REPOSITORY              TAG
<image-id>       <repository-path/name>     <tag-name>

下面是一些示例输出:

IMAGE ID            REPOSITORY                                                                 TAG
2ce533f88e80        mcr.microsoft.com/azure-cognitive-services/form-recognizer/layout          latest
4be104c126c5        mcr.microsoft.com/azure-cognitive-services/vision/read:3.1-preview         latest

测试容器

在主机上打开浏览器,使用 docker-compose.yaml 文件中指定的端口(例如 http://localhost:5021/swagger/index.html.)转到 localhost 例如,可以使用 API 中的“试用”功能来测试“文档智能”终结点。 这两个容器 swagger 页面应可用且可测试。

Document Intelligence Container

后续步骤

Azure AI 容器