您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:在 Linux Service Fabric 群集上创建容器映像Tutorial: Create container images on a Linux Service Fabric cluster

本教程是演示如何在 Linux Service Fabric 群集中使用容器的教程系列的第 1 部分。This tutorial is part one of a tutorial series that demonstrates how to use containers in a Linux Service Fabric cluster. 本教程中准备了一个要用于 Service Fabric 的多容器应用程序。In this tutorial, a multi-container application is prepared for use with Service Fabric. 在后续教程中,这些映像用作 Service Fabric 应用程序的一部分。In subsequent tutorials, these images are used as part of a Service Fabric application. 本教程介绍如何执行下列操作:In this tutorial you learn how to:

  • 克隆 GitHub 中的应用程序源Clone application source from GitHub
  • 根据应用程序源创建容器映像Create a container image from the application source
  • 部署 Azure 容器注册表 (ACR) 实例Deploy an Azure Container Registry (ACR) instance
  • 标记 ACR 的容器映像Tag a container image for ACR
  • 向 ACR 上传映像Upload the image to ACR

此教程系列介绍了如何:In this tutorial series, you learn how to:

先决条件Prerequisites

  • 设置 Service Fabric 的 Linux 开发环境。Linux development environment set up for Service Fabric. 按照此处的说明设置 Linux 环境。Follow the instructions here to set up your Linux environment.
  • 本教程需要运行 Azure CLI 2.0.4 或更高版本。This tutorial requires that you are running the Azure CLI version 2.0.4 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如需进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install the Azure CLI.
  • 此外,还需要拥有可用的 Azure 订阅。Additionally, it requires that you have an Azure subscription available. 有关免费试用版的详细信息,请转到此处For more information on a free trial version, go here.

获取应用程序代码Get application code

本教程使用的示例应用程序是一个投票应用。The sample application used in this tutorial is a voting app. 该应用程序由前端 Web 组件和后端 Redis 实例组成。The application consists of a front-end web component and a back-end Redis instance. 该组件打包到容器映像中。The components are packaged into container images.

使用 git 可将应用程序的副本下载到开发环境。Use git to download a copy of the application to your development environment.

git clone https://github.com/Azure-Samples/service-fabric-containers.git

cd service-fabric-containers/Linux/container-tutorial/

该解决方案包含两个文件夹和一个“docker-compose.yml”文件。The solution contains two folders and a 'docker-compose.yml' file. “azure-vote”文件夹包含 Python 前端服务以及用于生成映像的 Dockerfile。The 'azure-vote' folder contains the Python frontend service along with the Dockerfile used to build the image. “Voting”目录包含部署到群集的 Service Fabric 应用程序包。The 'Voting' directory contains the Service Fabric application package that is deployed to the cluster. 这些目录包含本教程所需的资产。These directories contain the necessary assets for this tutorial.

创建容器映像Create container images

在“azure-vote”目录中,运行以下命令,生成前端 Web 组件的映像。Inside the azure-vote directory, run the following command to build the image for the front-end web component. 下面的命令使用此目录中的 Dockerfile 生成映像。This command uses the Dockerfile in this directory to build the image.

docker build -t azure-vote-front .

备注

如果你的权限被拒绝,请按照文档操作,了解如何在没有 sudo 的情况下使用 docker。If you are getting permission denied then follow this documentation on how to work with docker without sudo.

此命令可能需要一些时间,因为需要从 Docker 中心拉取所有必需的依赖项。This command can take some time since all the necessary dependencies need to be pulled from Docker Hub. 完成后,使用 docker images 命令查看创建的映像。When completed, use the docker images command to see the created images.

docker images

请注意,已下载或创建两个映像。Notice that two images have been downloaded or created. azure-vote-front 映像包含应用程序。The azure-vote-front image contains the application. 它派生自 Docker 中心的 python 映像。It was derived from a python image from Docker Hub.

REPOSITORY                   TAG                 IMAGE ID            CREATED              SIZE
azure-vote-front             latest              052c549a75bf        About a minute ago   708MB
tiangolo/uwsgi-nginx-flask   python3.6           590e17342131        5 days ago           707MB

部署 Azure 容器注册表Deploy Azure Container Registry

首先,运行 az login 命令,登录 Azure 帐户。First run the az login command to log in to your Azure account.

az login

接下来,使用 az account 命令来选择订阅,创建 Azure 容器注册表。Next, use the az account command to choose your subscription to create the Azure Container registry. 必须输入你的 Azure 订阅的订阅 ID 来代替 <subscription_id>。You have to enter the subscription ID of your Azure subscription in place of <subscription_id>.

az account set --subscription <subscription_id>

在部署 Azure 容器注册表时,首先需要一个资源组。When deploying 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. 在此示例中,在“westus”区域中创建了名为“myResourceGroup”的资源组。In this example, a resource group named myResourceGroup is created in the westus region.

az group create --name <myResourceGroup> --location westus

使用 az acr create 命令创建 Azure 容器注册表。Create an Azure Container registry with the az acr create command. 将 <acrName> 替换为要在订阅下创建的容器注册表的名称。Replace <acrName> with the name of the container registry you want to create under your subscription. 此名称必须为字母数字且唯一。This name must be alphanumeric and unique.

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

在本教程的余下部分,将使用“acrName”作为所选容器注册表名称的占位符。Throughout the rest of this tutorial, we use "acrName" as a placeholder for the container registry name that you chose. 请记下此值。Please make note of this value.

登录容器注册表Log in to your container registry

先登录 ACR 实例,再向其推送映像。Log in to your ACR instance before pushing images to it. 使用 az acr login 命令完成此操作。Use the az acr login command to complete the operation. 请提供创建容器注册表时所使用的唯一名称。Provide the unique name given to the container registry when it was created.

az acr login --name <acrName>

完成后,该命令会返回“登录成功”消息。The command returns a 'Login Succeeded’ message once completed.

标记容器映像Tag container images

需要使用注册表的 loginServer 名称标记每个容器映像。Each container image needs to be tagged with the loginServer name of the registry. 在将容器映像推送到映像注册表时,使用此标记进行路由。This tag is used for routing when pushing container images to an image registry.

若要查看当前映像的列表,请使用 docker images 命令。To see a list of current images, use the docker images command.

docker images

输出:Output:

REPOSITORY                   TAG                 IMAGE ID            CREATED              SIZE
azure-vote-front             latest              052c549a75bf        About a minute ago   708MB
tiangolo/uwsgi-nginx-flask   python3.6           590e17342131        5 days ago           707MB

要获取 loginServer 名称,请运行以下命令:To get the loginServer name, run the following command:

az acr show --name <acrName> --query loginServer --output table

这将输出包含以下结果的表。This outputs a table with the following results. 在将 azure-vote-front 映像推送到下一步中的容器注册表之前,此结果将用于标记该映像。This result will be used to tag your azure-vote-front image before pushing it to the container registry in the next step.

Result
------------------
<acrName>.azurecr.io

现在,使用容器注册表的 loginServer 标记 azure-vote-front 映像。Now, tag the azure-vote-front image with the loginServer of your container registry. 另外,将 :v1 添加至映像名称的末端。Also, add :v1 to the end of the image name. 此标记代表映像版本。This tag indicates the image version.

docker tag azure-vote-front <acrName>.azurecr.io/azure-vote-front:v1

标记后即可运行“docker images”验证操作。Once tagged, run 'docker images' to verify the operation.

输出:Output:

REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
azure-vote-front                       latest              052c549a75bf        23 minutes ago      708MB
<acrName>.azurecr.io/azure-vote-front   v1                  052c549a75bf       23 minutes ago      708MB
tiangolo/uwsgi-nginx-flask             python3.6           590e17342131        5 days ago          707MB

将映像推送到注册表Push images to registry

将 azure-vote-front 映像推送到注册表。Push the azure-vote-front image to the registry.

使用以下示例,将 ACR loginServer 名称替换为环境中的 loginServer。Using the following example, replace the ACR loginServer name with the loginServer from your environment.

docker push <acrName>.azurecr.io/azure-vote-front:v1

docker push 命令需要几分钟才能完成。The docker push commands take a couple of minutes to complete.

列出注册表中的映像List images in registry

若要返回已推送到 Azure 容器注册表的映像列表,请运行 az acr repository list 命令。To return a list of images that have been pushed to your Azure Container registry, use the az acr repository list command. 使用 ACR 实例名称更新命令。Update the command with the ACR instance name.

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

输出:Output:

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

完成本教程后,容器映像已存储在专用 Azure 容器注册表实例中。At tutorial completion, the container image has been stored in a private Azure Container Registry instance. 后续教程会将此映像从 ACR 部署到 Service Fabric 群集。This image is deployed from ACR to a Service Fabric cluster in subsequent tutorials.

后续步骤Next steps

本教程中从 GitHub 拉取了一个应用程序、创建了容器映像并将容器映像推送到注册表。In this tutorial, an application was pulled from GitHub and container images were created and pushed to a registry. 已完成以下步骤:The following steps were completed:

  • 克隆 GitHub 中的应用程序源Clone application source from GitHub
  • 根据应用程序源创建容器映像Create a container image from the application source
  • 部署 Azure 容器注册表 (ACR) 实例Deploy an Azure Container Registry (ACR) instance
  • 标记 ACR 的容器映像Tag a container image for ACR
  • 向 ACR 上传映像Upload the image to ACR

转到下一教程,了解如何使用 Yeoman 将容器打包到 Service Fabric 应用程序。Advance to the next tutorial to learn about packaging containers into a Service Fabric application using Yeoman.