快速入門:使用 Azure Container Registry 工作建置和執行容器映像

在本快速入門中,您會使用 Azure Container Registry 工作 命令,快速建置、推送及執行 Azure 中的 Docker 容器映射,而不需要本機 Docker 安裝。 ACR 工作是 Azure Container Registry 中的一套功能,可協助您管理及修改整個容器生命週期中的容器映像。 此範例示範如何使用本機 Dockerfile,將「內部迴圈」容器映像開發週期卸除至雲端,並搭配隨選組建。

在本快速入門之後,請使用 教學課程探索 ACR 工作的更進階功能。 ACR 工作可以根據程式代碼認可或基底映射更新,或平行測試多個容器,以及其他案例,將映像建置自動化。

如果您沒有 Azure 訂閱,請在開始之前,先建立 Azure 免費帳戶

必要條件

  • 本快速入門需要 Azure CLI 2.0.58 版或更新版本。 如果您是使用 Azure Cloud Shell,就已安裝最新版本。

建立資源群組

如果您還沒有容器登錄,請先使用 az group create 命令建立資源群組。 Azure 資源群組是在其中部署與管理 Azure 資源的邏輯容器。

下列範例會在 eastus 位置建立名為 myResourceGroup 的資源群組。

az group create --name myResourceGroup --location eastus

建立容器登錄

使用 az acr create 命令建立容器登錄。 登錄名稱在 Azure 內必須是唯一的,且包含 5-50 個英數字元。 在下列範例中, 會使用 mycontainerregistry008 。 將此更新為唯一值。

az acr create --resource-group myResourceGroup \
  --name mycontainerregistry008 --sku Basic

此範例會建立 基本 登錄,這是開發人員瞭解 Azure Container Registry 的成本優化選項。 如需可用服務層級的詳細資訊,請參閱 容器登錄服務層級

從 Dockerfile 建置和推送映像

現在,使用 Azure Container Registry 來建置和推送映像。 首先,建立本機工作目錄,然後使用單行建立名為 Dockerfile 的 DockerfileFROM mcr.microsoft.com/hello-world。 這是從裝載於 Microsoft Container Registry 的 hello-world 映像建置 Linux 容器映像的簡單範例。 您可以建立自己的標準 Dockerfile,併為其他平台建置映射。 如果您正在bash殼層工作,請使用下列命令建立 Dockerfile:

echo "FROM mcr.microsoft.com/hello-world" > Dockerfile

執行 az acr build 命令,以建置映像,並在映射成功建置之後,將其推送至您的登錄。 下列範例會建置並推送 sample/hello-world:v1 映像。 .命令結尾的 會設定 Dockerfile 的位置,在此案例中為目前目錄。

az acr build --image sample/hello-world:v1 \
  --registry mycontainerregistry008 \
  --file Dockerfile . 

成功建置和推送的輸出如下所示:

Packing source code into tar to upload...
Uploading archived source code from '/tmp/build_archive_b0bc1e5d361b44f0833xxxx41b78c24e.tar.gz'...
Sending context (1.856 KiB) to registry: mycontainerregistry008...
Queued a build with ID: ca8
Waiting for agent...
2019/03/18 21:56:57 Using acb_vol_4c7ffa31-c862-4be3-xxxx-ab8e615c55c4 as the home volume
2019/03/18 21:56:57 Setting up Docker configuration...
2019/03/18 21:56:58 Successfully set up Docker configuration
2019/03/18 21:56:58 Logging in to registry: mycontainerregistry008.azurecr.io
2019/03/18 21:56:59 Successfully logged into mycontainerregistry008.azurecr.io
2019/03/18 21:56:59 Executing step ID: build. Working directory: '', Network: ''
2019/03/18 21:56:59 Obtaining source code and scanning for dependencies...
2019/03/18 21:57:00 Successfully obtained source code and scanned for dependencies
2019/03/18 21:57:00 Launching container with name: build
Sending build context to Docker daemon  13.82kB
Step 1/1 : FROM mcr.microsoft.com/hello-world
latest: Pulling from hello-world
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586fxxxx21577a99efb77324b0fe535
Successfully built fce289e99eb9
Successfully tagged mycontainerregistry008.azurecr.io/sample/hello-world:v1
2019/03/18 21:57:01 Successfully executed container: build
2019/03/18 21:57:01 Executing step ID: push. Working directory: '', Network: ''
2019/03/18 21:57:01 Pushing image: mycontainerregistry008.azurecr.io/sample/hello-world:v1, attempt 1
The push refers to repository [mycontainerregistry008.azurecr.io/sample/hello-world]
af0b15c8625b: Preparing
af0b15c8625b: Layer already exists
v1: digest: sha256:92c7f9c92844bbbb5d0a101b22f7c2a7949e40f8ea90c8b3bc396879d95e899a size: 524
2019/03/18 21:57:03 Successfully pushed image: mycontainerregistry008.azurecr.io/sample/hello-world:v1
2019/03/18 21:57:03 Step ID: build marked as successful (elapsed time in seconds: 2.543040)
2019/03/18 21:57:03 Populating digests for step ID: build...
2019/03/18 21:57:05 Successfully populated digests for step ID: build
2019/03/18 21:57:05 Step ID: push marked as successful (elapsed time in seconds: 1.473581)
2019/03/18 21:57:05 The following dependencies were found:
2019/03/18 21:57:05
- image:
    registry: mycontainerregistry008.azurecr.io
    repository: sample/hello-world
    tag: v1
    digest: sha256:92c7f9c92844bbbb5d0a101b22f7c2a7949e40f8ea90c8b3bc396879d95e899a
  runtime-dependency:
    registry: registry.hub.docker.com
    repository: library/hello-world
    tag: v1
    digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
  git: {}

Run ID: ca8 was successful after 10s

執行映像

現在,快速執行您建置並推送至登錄的映像。 在這裡,您會使用 az acr run 來執行容器命令。 在您的容器開發工作流程中,這可能是部署映像之前驗證步驟,或者您可以在多步驟 YAML 檔案中包含 命令。

下列範例會使用 $Registry 來指定執行命令之登錄的端點:

az acr run --registry mycontainerregistry008 \
  --cmd '$Registry/sample/hello-world:v1' /dev/null

cmd此範例中的 參數會在其預設組態中執行容器,但cmd支援其他docker run參數,甚至是其他docker命令。

輸出類似下列內容:

Packing source code into tar to upload...
Uploading archived source code from '/tmp/run_archive_ebf74da7fcb04683867b129e2ccad5e1.tar.gz'...
Sending context (1.855 KiB) to registry: mycontainerre...
Queued a run with ID: cab
Waiting for an agent...
2019/03/19 19:01:53 Using acb_vol_60e9a538-b466-475f-9565-80c5b93eaa15 as the home volume
2019/03/19 19:01:53 Creating Docker network: acb_default_network, driver: 'bridge'
2019/03/19 19:01:53 Successfully set up Docker network: acb_default_network
2019/03/19 19:01:53 Setting up Docker configuration...
2019/03/19 19:01:54 Successfully set up Docker configuration
2019/03/19 19:01:54 Logging in to registry: mycontainerregistry008.azurecr.io
2019/03/19 19:01:55 Successfully logged into mycontainerregistry008.azurecr.io
2019/03/19 19:01:55 Executing step ID: acb_step_0. Working directory: '', Network: 'acb_default_network'
2019/03/19 19:01:55 Launching container with name: acb_step_0

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

2019/03/19 19:01:56 Successfully executed container: acb_step_0
2019/03/19 19:01:56 Step ID: acb_step_0 marked as successful (elapsed time in seconds: 0.843801)

Run ID: cab was successful after 6s

清除資源

不再需要時,您可以使用 az group delete 命令來移除資源群組、容器登錄,以及儲存在那裡的容器映像。

az group delete --name myResourceGroup

下一步

在本快速入門中,您已使用 ACR 工作的功能,在 Azure 內原生建置、推送及執行 Docker 容器映像,而不需要本機 Docker 安裝。 繼續進行 Azure Container Registry 工作教學課程,以瞭解如何使用 ACR 工作將映射組建和更新自動化。