建立要重複使用的容器Create containers for reuse

使用這些容器配方來建立可重複使用的認知服務容器。Use these container recipes to create Cognitive Services Containers that can be reused. 您可以使用部分或所有設定設定來建立容器, 以便在容器啟動時_不_需要它們。Containers can be built with some or all configuration settings so that they are not needed when the container is started.

一旦擁有這個新的容器層 (含設定), 而且您已在本機進行測試, 您就可以將容器儲存在容器登錄中。Once you have this new layer of container (with settings), and you have tested it locally, you can store the container in a container registry. 當容器啟動時, 它只會需要目前未儲存在容器中的設定。When the container starts, it will only need those settings that are not currently stored in the container. 私用登錄容器會提供設定空間, 讓您在中傳遞這些設定。The private registry container provides configuration space for you to pass those settings in.

Docker 執行語法Docker run syntax

docker run檔中的任何範例都假設 Windows 主控台^包含行接續字元。Any docker run examples in this document assume a Windows console with a ^ line continuation character. 請考慮下列各項以供您自己使用:Consider the following for your own use:

  • 若非十分熟悉 Docker 容器,請勿變更引數的順序。Do not change the order of the arguments unless you are very familiar with docker containers.
  • 如果您使用的作業系統不是 Windows, 或是 Windows 主控台以外的主控台, 請使用正確的主控台/終端機、掛接的資料夾語法, 以及主控台和系統的行接續字元。If you are using an operating system other than Windows, or a console other than Windows console, use the correct console/terminal, folder syntax for mounts, and line continuation character for your console and system. 由於認知服務容器是 Linux 作業系統, 因此目標掛接會使用 Linux 樣式的資料夾語法。Because the Cognitive Services container is a Linux operating system, the target mount uses a Linux-style folder syntax.
  • docker run範例會使用c:磁片磁碟機上的目錄, 以避免在 Windows 上發生任何許可權衝突。docker run examples use the directory off the c: drive to avoid any permission conflicts on Windows. 如果您需要使用特定目錄作為輸入目錄,您可能需要授與 Docker 服務權限。If you need to use a specific directory as the input directory, you may need to grant the docker service permission.

在映射中不儲存任何設定Store no configuration settings in image

每個docker run服務的範例命令不會在容器中儲存任何設定。The example docker run commands for each service do not store any configuration settings in the container. 當您從主控台或登錄服務啟動容器時, 這些設定值必須傳入。When you start the container from a console or registry service, those configuration settings need to pass in. 私用登錄容器會提供設定空間, 讓您在中傳遞這些設定。The private registry container provides configuration space for you to pass those settings in.

重複使用配方: 儲存所有具有容器的設定Reuse recipe: store all configuration settings with container

若要儲存所有的設定, 請Dockerfile使用這些設定來建立。In order to store all configuration settings, create a Dockerfile with those settings.

這種方法的問題:Issues with this approach:

  • 新容器的名稱和標記會與原始容器不同。The new container has a separate name and tag from the original container.
  • 若要變更這些設定, 您必須變更 Dockerfile 的值、重建映射, 然後重新發佈至您的登錄。In order to change these settings, you will have to change the values of the Dockerfile, rebuild the image, and republish to your registry.
  • 如果有人取得您容器登錄或本機主機的存取權, 他們就可以執行容器, 並使用認知服務端點。If someone gets access to your container registry or your local host, they can run the container and use the Cognitive Services endpoints.
  • 如果您的認知服務不需要輸入裝載, 請不要COPY將這些行新增至您的 Dockerfile。If your Cognitive Service doesn't require input mounts, don't add the COPY lines to your Dockerfile.

建立 Dockerfile, 從您想要使用的現有認知服務容器中提取, 然後在 Dockerfile 中使用 docker 命令來設定或提取容器所需的資訊。Create Dockerfile, pulling from the existing Cognitive Services container you want to use, then use docker commands in the Dockerfile to set or pull in information the container needs.

這個範例:This example:

  • {BILLING_ENDPOINT} 使用ENV, 從主機的環境金鑰設定計費端點。Sets the billing endpoint, {BILLING_ENDPOINT} from the host's environment key using ENV.
  • 使用 ' ENV ', {ENDPOINT_KEY}從主機的環境金鑰設定計費 API 金鑰。Sets the billing API-key, {ENDPOINT_KEY} from the host's environment key using `ENV.

重複使用配方: 使用容器儲存帳單設定Reuse recipe: store billing settings with container

這個範例示範如何從 Dockerfile 建立文字分析 ' 情感容器。This example shows how to build the Text Analytics' sentiment container from a Dockerfile.

FROM mcr.microsoft.com/azure-cognitive-services/sentiment:latest
ENV billing={BILLING_ENDPOINT}
ENV apikey={ENDPOINT_KEY}
ENV EULA=accept

視需要在本機或從您的私用登錄容器中, 建立並執行容器。Build and run the container locally or from your private registry container as needed.

重複使用配方: 使用容器儲存帳單和掛接設定Reuse recipe: store billing and mount settings with container

這個範例示範如何使用 Language Understanding, 從 Dockerfile 儲存計費和模型。This example shows how to use Language Understanding, saving billing and models from the Dockerfile.

  • 使用COPY, 從主機的檔案系統複製 Language Understanding (LUIS) 模型檔案。Copies the Language Understanding (LUIS) model file from the host's file system using COPY.
  • LUIS 容器支援多個模型。The LUIS container supports more than one model. 如果所有模型都儲存在相同的資料夾中, 您就需要COPY一個語句。If all models are stored in the same folder, you all need one COPY statement.
  • 從模型輸入目錄的相對父系執行 docker 檔案。Run the docker file from the relative parent of the model input directory. 在下列範例中, 請docker build /input從的docker run相對父系執行和命令。For the following example, run the docker build and docker run commands from the relative parent of /input. 命令的/input第一個是主機電腦的目錄。 COPYThe first /input on the COPY command is the host computer's directory. 第二/input個是容器的目錄。The second /input is the container's directory.
FROM <container-registry>/<cognitive-service-container-name>:<tag>
ENV billing={BILLING_ENDPOINT}
ENV apikey={ENDPOINT_KEY}
ENV EULA=accept
COPY /input /input

視需要在本機或從您的私用登錄容器中, 建立並執行容器。Build and run the container locally or from your private registry container as needed.

如何使用本機主機上的容器How to use container on your local host

若要建立 Docker 檔案, 請<your-image-name>將取代為映射的新名稱, 然後使用:To build the Docker file, replace <your-image-name> with the new name of the image, then use:

docker build -t <your-image-name> .

若要執行映射, 並在容器停止時將它移除--rm():To run the image, and remove it when the container stops (--rm):

docker run --rm <your-image-name>

如何將容器新增至私用登錄How to add container to private registry

請遵循下列步驟來使用 Dockerfile, 並將新的映射放在您的私人容器登錄中。Follow these steps to use the Dockerfile and place the new image in your private container registry.

  1. Dockerfile使用 [重複使用] 配方中的文字建立。Create a Dockerfile with the text from reuse recipe. A Dockerfile沒有延伸模組。A Dockerfile doesn't have an extension.

  2. 以您自己的值取代角括弧中的任何值。Replace any values in the angle brackets with your own values.

  3. 使用下列命令, 將檔案建立在命令列或終端機的映射中。Build the file into an image at the command line or terminal, using the following command. 將角括弧<>中的值取代為您自己的容器名稱和標記。Replace the values in the angle brackets, <>, with your own container name and tag.

    標記選項-t是一種方式, 可新增您已變更之容器的相關資訊。The tag option, -t, is a way to add information about what you have changed for the container. 例如, 容器名稱modified-LUIS表示原始容器已分層。For example, a container name of modified-LUIS indicates the original container has been layered. 的標記名稱with-billing-and-model表示 Language Understanding (LUIS) 容器的修改方式。A tag name of with-billing-and-model indicates how the Language Understanding (LUIS) container has been modified.

    docker build -t <your-new-container-name>:<your-new-tag-name> .
    
  4. 從主控台登入 Azure CLI。Sign in to Azure CLI from a console. 此命令會開啟瀏覽器並要求驗證。This command opens a browser and requires authentication. 驗證之後, 您可以關閉瀏覽器並繼續在主控台中工作。Once authenticated, you can close the browser and continue working in the console.

    az login
    
  5. 使用主控台的 Azure CLI 登入您的私用登錄。Sign in to your private registry with Azure CLI from a console.

    將角括弧<my-registry>中的值取代為您自己的登錄名稱。Replace the values in the angle brackets, <my-registry>, with your own registry name.

    az acr login --name <my-registry>
    

    如果您已指派服務主體, 您也可以使用 docker 登入來登入。You can also sign in with docker login if you are assigned a service principal.

    docker login <my-registry>.azurecr.io
    
  6. 以私人登錄位置標記容器。Tag the container with the private registry location. 將角括弧<my-registry>中的值取代為您自己的登錄名稱。Replace the values in the angle brackets, <my-registry>, with your own registry name.

    docker tag <your-new-container-name>:<your-new-tag-name> <my-registry>.azurecr.io/<your-new-container-name-in-registry>:<your-new-tag-name>
    

    如果您不使用標記名稱, latest則會隱含。If you don't use a tag name, latest is implied.

  7. 將新的映射推送至您的私人容器登錄。Push the new image to your private container registry. 當您查看私人容器登錄時, 下列 CLI 命令中使用的容器名稱會是存放庫的名稱。When you view your private container registry, the container name used in the following CLI command will be the name of the repository.

    docker push <my-registry>.azurecr.io/<your-new-container-name-in-registry>:<your-new-tag-name>
    

後續步驟Next steps