使用 Cloud Native Buildpack 从应用生成映像并推送该映像Build and push an image from an app using a Cloud Native Buildpack

Azure CLI 命令 az acr pack build 使用 pack CLI 工具(来自 Buildpack)生成应用,并将此应用的映像推送到 Azure 容器注册表中。The Azure CLI command az acr pack build uses the pack CLI tool, from Buildpacks, to build an app and push its image into an Azure container registry. 此功能提供一个选项,用于从以 Node.js、Java 和其他语言编写的应用程序源代码快速生成容器映像,而无需定义 Dockerfile。This feature provides an option to quickly build a container image from your application source code in Node.js, Java, and other languages without having to define a Dockerfile.

可以使用本地安装的 Azure CLI 来运行本文中的示例。You can use a local installation of the Azure CLI to run the examples in this article. 若要在本地使用 Azure CLI,需要安装 2.0.70 或更高版本。If you'd like to use it locally, version 2.0.70 or later is required. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

重要

此功能目前以预览版提供。This feature is currently in preview. 需同意补充使用条款才可使用预览版。Previews are made available to you on the condition that you agree to the supplemental terms of use. 在正式版 (GA) 推出之前,此功能的某些方面可能会有所更改。Some aspects of this feature may change prior to general availability (GA).

使用 build 命令Use the build command

若要使用 Cloud Native Buildpack 生成并推送容器映像,请运行 az acr pack build 命令。To build and push a container image using Cloud Native Buildpacks, run the az acr pack build command. az acr build 命令从 Dockerfile 源和相关代码生成并推送映像,而使用 az acr pack build 可直接指定应用程序源树。Whereas the az acr build command builds and pushes an image from a Dockerfile source and related code, with az acr pack build you specify an application source tree directly.

运行 az acr pack build 时,请至少指定以下各项:At a minimum, specify the following when you run az acr pack build:

  • 在其中运行命令的 Azure 容器注册表An Azure container registry where you run the command
  • 生成的映像的映像名称和标记An image name and tag for the resulting image
  • ACR 任务的受支持上下文位置之一,例如本地目录、GitHub 存储库或远程 tarballOne of the supported context locations for ACR Tasks, such as a local directory, a GitHub repo, or a remote tarball
  • 适合你的应用程序的 Buildpack 生成器映像的名称。The name of a Buildpack builder image suitable for your application. 为加快生成速度,Azure 容器注册表会缓存 cloudfoundry/cnb:0.0.34-cflinuxfs3 等生成器映像。Azure Container Registry caches builder images such as cloudfoundry/cnb:0.0.34-cflinuxfs3 for faster builds.

az acr pack build 支持 ACR 任务命令的其他功能,包括 run 变量以及任务运行日志(这些日志会进行流式传输,还会进行保存供以后检索)。az acr pack build supports other features of ACR Tasks commands including run variables and task run logs that are streamed and also saved for later retrieval.

示例:使用 Cloud Foundry 生成器生成 Node.js 映像Example: Build Node.js image with Cloud Foundry builder

以下示例使用 cloudfoundry/cnb:0.0.34-cflinuxfs3 生成器,从 Azure-Samples/nodejs-docs-hello-world 存储库中的 Node.js 应用生成容器映像。The following example builds a container image from a Node.js app in the Azure-Samples/nodejs-docs-hello-world repo, using the cloudfoundry/cnb:0.0.34-cflinuxfs3 builder. Azure 容器注册表会缓存此生成器,因此无需指定 --pull 参数:This builder is cached by Azure Container Registry, so a --pull parameter isn't required:

az acr pack build \
    --registry myregistry \
    --image {{.Run.Registry}}/node-app:1.0 \
    --builder cloudfoundry/cnb:0.0.34-cflinuxfs3 \
    https://github.com/Azure-Samples/nodejs-docs-hello-world.git

备注

在 PowerShell 环境中运行上述 Azure CLI 脚本时,应将 --image 参数节替换为以下样式。When you run the above Azure CLI scripts on PowerShell enviornment, You should replace --image parameter section with following style. --image '{{.Run.Registry}}/node-app:1.0'

此示例生成具有 1.0 标记的 node-app 映像,并将其推送到 myregistry 容器注册表中 。This example builds the node-app image with the 1.0 tag and pushes it to the myregistry container registry. 在此示例中,已将目标注册表名称显式追加到映像名称的前面。In this example, the target registry name is explicitly prepended to the image name. 如果未指定目标注册表名称,则会自动将注册表登录服务器名称追加到映像名称的前面。If not specified, the registry login server name is automatically prepended to the image name.

命令输出显示了生成和推送映像的进度。Command output shows the progress of building and pushing the image.

成功生成映像后,可以使用 Docker(如果已安装)运行该映像。After the image is successfully built, you can run it with Docker, if you have it installed. 首先登录到注册表:First sign into your registry:

az acr login --name myregistry

运行映像:Run the image:

docker run --rm -p 1337:1337 myregistry.azurecr.cn/node-app:1.0

在你常用的浏览器中浏览到 localhost:1337,以查看示例 Web 应用。Browse to localhost:1337 in your favorite browser to see the sample web app. [Ctrl]+[C] 停止容器。Press [Ctrl]+[C] to stop the container.

示例:使用 Heroku 生成器生成 Java 映像Example: Build Java image with Heroku builder

以下示例使用 heroku/buildpacks:18 生成器从 buildpack/sample-java-app 存储库中的 Java 应用生成容器映像。The following example builds a container image from the Java app in the buildpack/sample-java-app repo, using the heroku/buildpacks:18 builder. --pull 参数指定命令应提取最新的生成器映像。The --pull parameter specifies that the command should pull the latest builder image.

az acr pack build \
    --registry myregistry \
    --image java-app:{{.Run.ID}} \
    --pull --builder heroku/buildpacks:18 \
    https://github.com/buildpack/sample-java-app.git

备注

在 PowerShell 环境中运行上述 Azure CLI 脚本时,应将 --image 参数节替换为以下样式。When you run the above Azure CLI scripts on PowerShell enviornment, You should replace --image parameter section with following style. --image 'java-app:{{.Run.ID}}'

此示例生成使用命令的运行 ID 标记的 java-app 映像,并将其推送到 myregistry 容器注册表中 。This example builds the java-app image tagged with the run ID of the command and pushes it to the myregistry container registry.

命令输出显示了生成和推送映像的进度。Command output shows the progress of building and pushing the image.

成功生成映像后,可以使用 Docker(如果已安装)运行该映像。After the image is successfully built, you can run it with Docker, if you have it installed. 首先登录到注册表:First sign into your registry:

az acr login --name myregistry

运行映像(请将 runid 替换为你的映像标记) :Run the image, substituting your image tag for runid:

docker run --rm -p 8080:8080 myregistry.azurecr.cn/java-app:runid

在你常用的浏览器中浏览到 localhost:8080,以查看示例 Web 应用。Browse to localhost:8080 in your favorite browser to see the sample web app. [Ctrl]+[C] 停止容器。Press [Ctrl]+[C] to stop the container.

后续步骤Next steps

使用 az acr pack build 生成并推送容器映像后,可以像部署任何其他映像一样将其部署到所选目标。After you build and push a container image with az acr pack build, you can deploy it like any image to a target of your choice. Azure 部署选项包括在应用服务或 Azure Kubernetes 服务中运行映像,等等。Azure deployment options include running it in App Service or Azure Kubernetes Service, among others.

有关 ACR 任务功能的详细信息,请参阅使用 ACR 任务自动执行容器映像的生成和维护For more information about ACR Tasks features, see Automate container image builds and maintenance with ACR Tasks.