使用 Docker Compose 部署多个容器Use Docker Compose to deploy multiple containers

本文介绍如何部署多个 Azure 认知服务容器。This article shows you how to deploy multiple Azure Cognitive Services containers. 具体而言,其中将会介绍如何使用 Docker Compose 来协调多个 Docker 容器映像。Specifically, you'll learn how to use Docker Compose to orchestrate multiple Docker container images.

Docker Compose 是用于定义和运行多容器 Docker 应用程序的工具。Docker Compose is a tool for defining and running multi-container Docker applications. 在 Compose 中,可以使用 YAML 文件来配置应用程序的服务。In Compose, you use a YAML file to configure your application’s services. 然后,运行一条命令,即可从配置中创建并启动所有服务。Then, you create and start all the services from your configuration by running a single command.

使用 Compose 可在一台主计算机上方便地协调多个容器映像。It can be useful to orchestrate multiple container images on a single host computer. 在本文中,我们会将“识别文本”和“表单识别器”容器组合到一起。In this article, we'll pull together the Recognize Text and Form Recognizer containers.

先决条件Prerequisites

此过程要求必须在本地安装和运行多个工具:This procedure requires several tools that must be installed and run locally:

  • Azure 订阅。An Azure subscription. 如果没有订阅,请在开始之前创建一个试用帐户If you don't have one, create a trial account before you begin.
  • Docker 引擎Docker Engine. 确认 Docker CLI 是否可在控制台窗口中工作。Confirm that the Docker CLI works in a console window.
  • 具有适当定价层的 Azure 资源。An Azure resource with the correct pricing tier. 只有以下定价层适用于此容器:Only the following pricing tiers work with this container:
    • 仅限使用 F0 或标准定价层的“计算机视觉”资源。 Computer Vision resource with F0 or Standard pricing tier only.
    • 具有 S0 定价层的认知服务资源 。Cognitive Services resource with the S0 pricing tier.

使用 Docker CLI 对专用容器注册表进行身份验证Use the Docker CLI to authenticate the private container registry

可通过多种方法中的任何一种使用认知服务容器的专用容器注册表进行身份验证,但建议的方法是在 Docker CLI 中使用命令行。You can authenticate with the private container registry for Cognitive Services Containers in any of several ways, but the recommended method from the command line is to use the Docker CLI.

使用 docker login 命令(如以下示例所示)登录到 containerpreview.azurecr.cn,即认知服务容器的专用容器注册表。Use the docker login command, as shown in the following example, to log in to containerpreview.azurecr.cn, the private container registry for Cognitive Services Containers. 将 <username> 替换为用户名,将 <password> 替换为从 Azure 认知服务团队收到的凭据中提供的密码 。Replace <username> with the user name and <password> with the password that's provided in the credentials you received from the Azure Cognitive Services team.

docker login containerpreview.azurecr.cn -u <username> -p <password>

如果已在文本文件中保护了凭据,则可以使用 cat 命令将该文本文件的内容连接到 docker login 命令,如以下示例所示。If you've secured your credentials in a text file, you can concatenate the contents of that text file, by using the cat command, to the docker login command, as shown in the following example. 将 <passwordFile> 替换为包含密码的文本文件的路径和名称,将 <username> 替换为凭据中提供的用户名 。Replace <passwordFile> with the path and name of the text file that contains the password and <username> with the user name that's provided in your credentials.

cat <passwordFile> | docker login containerpreview.azurecr.cn -u <username> --password-stdin

Docker Compose 文件Docker Compose file

YAML 文件定义要部署的所有服务。The YAML file defines all the services to be deployed. 这些服务依赖于 DockerFile 或现有的容器映像。These services rely on either a DockerFile or an existing container image. 在本例中,我们将使用两个预览映像。In this case, we'll use two preview images. 复制并粘贴以下 YAML 文件,并将其保存为 docker-compose.yamlCopy and paste the following YAML file, and save it as docker-compose.yaml. 在文件中提供适当的 apikeybillingEndpointUri 值。Provide the appropriate apikey, billing, and EndpointUri values in the file.

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

  ocr:
    image: "containerpreview.azurecr.cn/microsoft/cognitive-services-recognize-text"
    environment:
      eula: accept
      apikey: # < Your recognize text API key >
      billing: # < Your recognize text billing URL >
    ports:
      - "5021:5000"

重要

volumes 节点下指定的主计算机上创建目录。Create the directories on the host machine that are specified under the volumes node. 之所以需要这样做,是因为在尝试使用卷绑定装载映像之前,目录必须存在。This approach is required because the directories must exist before you try to mount an image by using volume bindings.

启动已配置的 Docker Compose 服务Start the configured Docker Compose services

使用 Docker Compose 文件可以管理所定义服务的生命周期中的所有阶段:启动、停止和重新生成服务;查看服务状态;记录流。A Docker Compose file enables the management of all the stages in a defined service's life cycle: starting, stopping, and rebuilding services; viewing the service status; and log streaming. 从项目目录(docker-compose.yaml 文件所在的位置)打开命令行接口。Open a command-line interface from the project directory (where the docker-compose.yaml file is located).

备注

为了避免出错,请确保主计算机与 Docker 引擎正确共享驱动器。To avoid errors, make sure that the host machine correctly shares drives with Docker Engine. 例如,如果在 docker-compose.yaml 文件中将 E:\publicpreview 用作目录,请与 Docker 共享驱动器 E。For example, if E:\publicpreview is used as a directory in the docker-compose.yaml file, share drive E with Docker.

在命令行接口中执行以下命令,以启动(或重启)docker-compose.yaml 文件中定义的所有服务:From the command-line interface, execute the following command to start (or restart) all the services defined in the docker-compose.yaml file:

docker-compose up

当 Docker 首次使用此配置执行 docker-compose up 命令时,它将提取 services 节点下配置的映像,然后下载并装载这些映像:The first time Docker executes the docker-compose up command by using this configuration, it pulls the images configured under the services node and then downloads and mounts them:

Pulling forms (containerpreview.azurecr.cn/microsoft/cognitive-services-form-recognizer:)...
latest: Pulling from microsoft/cognitive-services-form-recognizer
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 (containerpreview.azurecr.cn/microsoft/cognitive-services-recognize-text:)...
latest: Pulling from microsoft/cognitive-services-recognize-text
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

下载映像后,映像服务将会启动:After the images are downloaded, the image services are started:

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 Microsoft 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://chinaeast2.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 Microsoft 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://chinacentraleast.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.

验证服务可用性Verify the service availability

提示

可以使用 docker images 命令列出下载的容器映像。You can use the docker images command to list your downloaded container images. 例如,以下命令以表格列出每个下载的容器映像的 ID、存储库和标记:For example, the following command lists the ID, repository, and tag of each downloaded container image, formatted as a table:

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

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

下面是一些示例输出:Here's some example output:

IMAGE ID            REPOSITORY                                                                 TAG
2ce533f88e80        containerpreview.azurecr.cn/microsoft/cognitive-services-form-recognizer   latest
4be104c126c5        containerpreview.azurecr.cn/microsoft/cognitive-services-recognize-text    latest

测试“识别文本”容器Test the Recognize Text container

在主计算机上打开浏览器,使用 docker-compose.yaml 文件中指定的端口(例如 http://localhost:5021/swagger/index.html )转到 localhostOpen a browser on the host machine and go to localhost by using the specified port from the docker-compose.yaml file, such as http://localhost:5021/swagger/index.html. 可以使用 API 中的“试用”功能来测试“识别文本”终结点。You can use the "Try It" feature in the API to test the Recognize Text endpoint.

“识别文本”容器

测试“表单识别器”容器Test the Form Recognizer container

在主计算机上打开浏览器,使用 docker-compose.yaml 文件中指定的端口(例如 http://localhost:5010/swagger/index.html )转到 localhostOpen a browser on the host machine and go to localhost by using the specified port from the docker-compose.yaml file, such as http://localhost:5010/swagger/index.html. 可以使用 API 中的“试用”功能来测试“表单识别器”终结点。You can use the "Try It" feature in the API to test the Form Recognizer endpoint.

“表单识别器”容器