教學課程:部署和使用 Azure Container RegistryTutorial: Deploy and use Azure Container Registry

Azure Container Registry (ACR) 是適用於容器映像的私人登錄。Azure Container Registry (ACR) is a private registry for container images. 私人容器登錄可讓您安全地建置及部署應用程式和自訂程式碼。A private container registry lets you securely build and deploy your applications and custom code. 在本教學課程 (2/7 部分) 中,您將部署 ACR 執行個體,並將容器映像推送至該處。In this tutorial, part two of seven, you deploy an ACR instance and push a container image to it. 您會了解如何:You learn how to:

  • 建立 Azure Container Registry (ACR) 執行個體Create an Azure Container Registry (ACR) instance
  • 標記 ACR 的容器映像Tag a container image for ACR
  • 將映像上傳至 ACRUpload the image to ACR
  • 檢視登錄中的映像View images in your registry

在其他教學課程中,此 ACR 執行個體會與 AKS 中的 Kubernetes 叢集整合,並從該映像部署應用程式。In additional tutorials, this ACR instance is integrated with a Kubernetes cluster in AKS, and an application is deployed from the image.

開始之前Before you begin

上一個教學課程中,我們已針對簡單的 Azure Voting 應用程式建立容器映像。In the previous tutorial, a container image was created for a simple Azure Voting application. 如果您尚未建立 Azure Voting 應用程式映像,請回到教學課程 1 – 建立容器映像If you have not created the Azure Voting app image, return to Tutorial 1 – Create container images.

在本教學課程中,您必須執行 Azure CLI 2.0.53 版或更新版本。This tutorial requires that you're running the Azure CLI version 2.0.53 or later. 執行 az --version 以尋找版本。Run az --version to find the version. 如果您需要安裝或升級,請參閱安裝 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

建立 Azure Container RegistryCreate an Azure Container Registry

若要建立 Azure Container Registry,您必須已有資源群組。To create an Azure Container Registry, you first need a resource group. Azure 資源群組是在其中部署與管理 Azure 資源的邏輯容器。An Azure resource group is a logical container into which Azure resources are deployed and managed.

使用 az group create 命令來建立資源群組。Create a resource group with the az group create command. 在下列範例中,會在 eastus 區域中建立名為 myResourceGroup 的資源群組:In the following example, a resource group named myResourceGroup is created in the eastus region:

az group create --name myResourceGroup --location eastus

使用 az acr create 命令建立 Azure Container Registry 執行個體,並提供您自己的登錄名稱。Create an Azure Container Registry instance with the az acr create command and provide your own registry name. 登錄名稱在 Azure 內必須是唯一的,且包含 5-50 個英數字元。The registry name must be unique within Azure, and contain 5-50 alphanumeric characters. 本教學課程的其餘部分,會使用 <acrName> 作為容器登錄名稱的預留位置。In the rest of this tutorial, <acrName> is used as a placeholder for the container registry name. 提供您自己唯一的登錄名稱。Provide your own unique registry name. 基本 SKU 對開發用途而言是最符合成本效益的進入點,可在儲存體和輸送量之間取得平衡。The Basic SKU is a cost-optimized entry point for development purposes that provides a balance of storage and throughput.

az acr create --resource-group myResourceGroup --name <acrName> --sku Basic

登入容器登錄Log in to the container registry

若要使用 ACR 執行個體,您必須先登入。To use the ACR instance, you must first log in. 請使用 az acr login 命令,並提供在先前的步驟中指定給容器登錄的唯一名稱。Use the az acr login command and provide the unique name given to the container registry in the previous step.

az acr login --name <acrName>

此命令在完成之後會傳回登入成功訊息。The command returns a Login Succeeded message once completed.

標記容器映像Tag a container image

若要查看目前的本機映像清單,請使用 docker images 命令:To see a list of your current local images, use the docker images command:

$ docker images

REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
azure-vote-front             latest              4675398c9172        13 minutes ago      694MB
redis                        latest              a1b99da73d05        7 days ago          106MB
tiangolo/uwsgi-nginx-flask   flask               788ca94b2313        9 months ago        694MB

若要將 azure-vote-front 容器映像用於 ACR,該映像必須標記登錄的登入伺服器位址。To use the azure-vote-front container image with ACR, the image needs to be tagged with the login server address of your registry. 將容器映像推送到映像登錄時,此標籤可用於路由傳送。This tag is used for routing when pushing container images to an image registry.

若要取得登入伺服器位址,請使用 az acr list 命令,並查詢 loginServer,如下所示:To get the login server address, use the az acr list command and query for the loginServer as follows:

az acr list --resource-group myResourceGroup --query "[].{acrLoginServer:loginServer}" --output table

現在,請以容器登錄的 acrloginServer 位址標記您的本機 azure-vote-front 映像。Now, tag your local azure-vote-front image with the acrloginServer address of the container registry. 若要指出映像版本,請在映像名稱結尾處加上 :v1To indicate the image version, add :v1 to the end of the image name:

docker tag azure-vote-front <acrLoginServer>/azure-vote-front:v1

若要確認標記是否已套用,請再次執行 docker imagesTo verify the tags are applied, run docker images again. 映像會加上 ACR 執行個體位址和版本號碼的標記。An image is tagged with the ACR instance address and a version number.

$ docker images

REPOSITORY                                           TAG           IMAGE ID            CREATED             SIZE
azure-vote-front                                     latest        eaf2b9c57e5e        8 minutes ago       716 MB
mycontainerregistry.azurecr.io/azure-vote-front      v1            eaf2b9c57e5e        8 minutes ago       716 MB
redis                                                latest        a1b99da73d05        7 days ago          106MB
tiangolo/uwsgi-nginx-flask                           flask         788ca94b2313        8 months ago        694 MB

將映像推送到登錄Push images to registry

利用您已建置並標記的映像,將 azure-vote-front 映像推送至您的 ACR 執行個體。With your image built and tagged, push the azure-vote-front image to your ACR instance. 請使用 docker push,並提供您自己的 acrLoginServer 位址作為映像名稱,如下所示:Use docker push and provide your own acrLoginServer address for the image name as follows:

docker push <acrLoginServer>/azure-vote-front:v1

將映像推送至 ACR 的作業可能需要幾分鐘才會完成。It may take a few minutes to complete the image push to ACR.

列出登錄中的映像List images in registry

若要傳回已推送至 ACR 執行個體的映像清單,請使用 az acr repository list 命令。To return a list of images that have been pushed to your ACR instance, use the az acr repository list command. 請提供您自己的 <acrName>,如下所示:Provide your own <acrName> as follows:

az acr repository list --name <acrName> --output table

下列範例輸出會將 azure-vote-front 映像列為登錄中的可用映像:The following example output lists the azure-vote-front image as available in the registry:

Result
----------------
azure-vote-front

若要查看特定映像的標記,請使用 az acr repository show-tags 命令,如下所示:To see the tags for a specific image, use the az acr repository show-tags command as follows:

az acr repository show-tags --name <acrName> --repository azure-vote-front --output table

下列範例輸出會顯示在先前的步驟中標記的 v1 映像:The following example output shows the v1 image tagged in a previous step:

Result
--------
v1

現在,您已有儲存在私人 Azure Container Registry 執行個體中的容器映像。You now have a container image that is stored in a private Azure Container Registry instance. 此映像會在下一個教學課程中,從 ACR 部署至 Kubernetes 叢集。This image is deployed from ACR to a Kubernetes cluster in the next tutorial.

後續步驟Next steps

在本教學課程中,您已建立 Azure Container Registry,並推送了可在 AKS 叢集中使用的映像。In this tutorial, you created an Azure Container Registry and pushed an image for use in an AKS cluster. 您已了解如何︰You learned how to:

  • 建立 Azure Container Registry (ACR) 執行個體Create an Azure Container Registry (ACR) instance
  • 標記 ACR 的容器映像Tag a container image for ACR
  • 將映像上傳至 ACRUpload the image to ACR
  • 檢視登錄中的映像View images in your registry

請繼續進行下一個教學課程,了解如何在 Azure 中部署 Kubernetes 叢集。Advance to the next tutorial to learn how to deploy a Kubernetes cluster in Azure.