Build and push an image from an app using a Cloud Native Buildpack

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. 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.

You can use the Azure Cloud Shell or a local installation of the Azure CLI to run the examples in this article. If you'd like to use it locally, version 2.0.70 or later is required. Run az --version to find the version. If you need to install or upgrade, see Install Azure CLI.

Important

This feature is currently in preview. Previews are made available to you on the condition that you agree to the supplemental terms of use. Some aspects of this feature may change prior to general availability (GA).

Use the build command

To build and push a container image using Cloud Native Buildpacks, run the az acr pack build command. 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.

At a minimum, specify the following when you run az acr pack build:

  • An Azure container registry where you run the command
  • An image name and tag for the resulting image
  • One of the supported context locations for ACR Tasks, such as a local directory, a GitHub repo, or a remote tarball
  • The name of a Buildpack builder image suitable for your application. If not cached by Azure Container Registry, the builder image must be pulled using the --pull parameter.

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.

Example: Build Node.js image with Cloud Foundry builder

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:cflinuxfs3 builder.

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

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.

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.io/node-app:1.0

Browse to localhost:1337 in your favorite browser to see the sample web app. Press [Ctrl]+[C] to stop the container.

Example: Build Java image with Heroku builder

The following example builds a container image from the Java app in the buildpack/sample-java-app repo, using the heroku/buildpacks:18 builder.

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

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.

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, substituting your image tag for runid:

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

Browse to localhost:8080 in your favorite browser to see the sample web app. Press [Ctrl]+[C] to stop the container.

Next steps

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 deployment options include running it in App Service or Azure Kubernetes Service, among others.

For more information about ACR Tasks features, see Automate container image builds and maintenance with ACR Tasks.