使用 Docker Compose 來部署多個容器

本文說明如何部署多個 Azure AI 容器。 具體而言,您將學習如何使用 Docker Compose 來協調多個 Docker 容器映像。

Docker Compose 是用來定義和執行多容器 Docker 應用程式的工具。 在 Compose 中要使用 YAML 檔案設定應用程式的服務。 然後,只要執行單一命令,即可從您的設定建立並啟動所有服務。

在單一部主機電腦上協調多個容器映像非常實用。 在本文中,我們會一起提取讀取和文件智慧容器。

必要條件

您必須在本機安裝並執行多項工具才能執行此程序:

  • Azure 訂用帳戶。 如果您沒有 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"

重要

在 [磁碟區] 節點下指定的主機電腦上建立目錄。 這是必要的做法,因為必須先有目錄,您才能使用磁碟區繫結嘗試掛接映像。

啟動已設定的 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 命令時,會提取在 [服務] 節點下設定的映像,然後下載並掛接這些映像:

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 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://westus2.api.cognitive.microsoft.com/'.
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://westcentralus.api.cognitive.microsoft.com/'.
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 \(英文\) 命令來列出已下載的容器映像。 例如,下列命令會列出每個已下載之容器映像的識別碼、存放庫和標籤,並將它格式化為表格:

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 檔案中指定的連接埠,移至 localhost,例如 http://localhost:5021/swagger/index.html. 例如,您可以使用 API 的 [試用] 功能來測試文件智慧端點。 這兩個容器 Swagger 頁面應可供使用及測試。

文件智慧服務容器標籤

後續步驟

Azure AI 容器