Working with containers and Azure Functions

This article demonstrates the support that Azure Functions provides for working with containerized function apps running in an Azure Container Apps environment. Support for hosting function app containers in Container Apps is currently in preview.

This article demonstrates the support that Azure Functions provides for working with function apps running in Linux containers.

Choose the hosting environment for your containerized function app at the top of the article.

If you want to jump right in, the following article shows you how to create your first function running in a Linux container and deploy the image from a container registry to a supported Azure hosting service:

Create your first containerized Azure Functions

Creating containerized function apps

Functions makes it easy to deploy and run your function apps as Linux containers, which you create and maintain.

Important

When creating your own containers, you are required to keep the base image of your container updated to the latest supported base image. Supported base images for Azure Functions are language-specific and are found in the Azure Functions base image repos.

The Functions team is committed to publishing monthly updates for these base images. Regular updates include the latest minor version updates and security fixes for both the Functions runtime and languages. For containers, you should regularly update the base image in the Dockerfile, rebuild, and redeploy updated versions of your containers.

For a complete example of how to create the local containerized function app from the command line and publish the image to a container registry, see Create a function app in a local container.

Generate the Dockerfile

Functions tooling provides a Docker option that generates a Dockerfile with your functions code project. You can use this file with Docker to create your functions in a container that derives from the correct base image (language and version).

The way you create a Dockerfile depends on how you create your project.

  • When you create a Functions project using Azure Functions Core Tools, include the --docker option when you run the func init command, as in the following example:

    func init --docker
    
  • You can also add a Dockerfile to an existing project by using the --docker-only option when you run the func init command in an existing project folder, as in the following example:

    func init --docker-only
    

For a complete example, see Create a function app in a local container.

Creating your function app in a container

With a Core Tools-generated Dockerfile in your code project, you can use Docker to create the containerized function app on your local computer. The following docker build command creates an image of your containerized functions from the project in the local directory:

docker build --tag <DOCKER_ID>/<IMAGE_NAME>:v1.0.0 .

For an example of how to create the container, see Build the container image and verify locally.

Update an image in the registry

When you make changes to your functions code project or need to update to the latest base image, you need to rebuild the container locally and republish the updated image to your chosen container registry. The following command rebuilds the image from the root folder with an updated version number and pushes it to your registry:

az acr build --registry <REGISTRY_NAME> --image <LOGIN_SERVER>/azurefunctionsimage:v1.0.1 .

Replace <REGISTRY_NAME> with your Container Registry instance and <LOGIN_SERVER> with the login server name.

At this point, you need to update the deployment to use the new image. The following example updates the function app to use the new image:

az functionapp config container set --image <IMAGE_NAME> --registry-password <SECURE_PASSWORD>--registry-username <USER_NAME> --name <APP_NAME> --resource-group <RESOURCE_GROUP>

In this example, <IMAGE_NAME> is the full name of the new image with version. Private registries require you to supply a username and password. Store these credentials securely.

You should also consider enabling continuous deployment.

Work with images in Azure Functions

When your function app container is deployed from a registry, Functions maintains information about the source image. Use the following commands to get data about the image or change the deployment image used:

Application settings

Azure Functions lets you work with application settings for containerized function apps in the standard way. For more information, see Use application settings.

Enable continuous deployment to Azure

Important

Webhook-based deployment isn't currently supported when running your container in an Elastic Premium plan. If you need to use the continuous deployment method described in this section, instead deploy your container in an App Service plan. When running in an Elastic Premium plan, you need to manually restart your app whenever you make updates to your container in the repository.

You can enable Azure Functions to automatically update your deployment of an image whenever you update the image in the registry.

  1. Use the following command to enable continuous deployment and to get the webhook URL:

    az functionapp deployment container config --enable-cd --query CI_CD_URL --output tsv --name <APP_NAME> --resource-group AzureFunctionsContainers-rg
    

    The az functionapp deployment container config command enables continuous deployment and returns the deployment webhook URL. You can retrieve this URL at any later time by using the az functionapp deployment container show-cd-url command.

    As before, replace <APP_NAME> with your function app name.

  2. Copy the deployment webhook URL to the clipboard.

  3. Open Docker Hub, sign in, and select Repositories on the navigation bar. Locate and select the image, select the Webhooks tab, specify a Webhook name, paste your URL in Webhook URL, and then select Create.

    Screenshot showing how to add the webhook in your Docker Hub window.

  4. With the webhook set, Azure Functions redeploys your image whenever you update it in Docker Hub.

Enable SSH connections

SSH enables secure communication between a container and a client. With SSH enabled, you can connect to your container using App Service Advanced Tools (Kudu). For easy connection to your container using SSH, Azure Functions provides a base image that has SSH already enabled. You only need to edit your Dockerfile, then rebuild, and redeploy the image. You can then connect to the container through the Advanced Tools (Kudu).

  1. In your Dockerfile, append the string -appservice to the base image in your FROM instruction, as in the following example:

    FROM mcr.microsoft.com/azure-functions/node:4-node18-appservice
    

    This example uses the SSH-enabled version of the Node.js version 18 base image. Visit the Azure Functions base image repos to verify that you're using the latest version of the SSH-enabled base image.

  2. Rebuild the image by using the docker build command, replace the <DOCKER_ID> with your Docker Hub account ID, as in the following example.

    docker build --tag <DOCKER_ID>/azurefunctionsimage:v1.0.0 .
    
  3. Push the updated image to Docker Hub, which should take considerably less time than the first push. Only the updated segments of the image need to be uploaded now.

    docker push <DOCKER_ID>/azurefunctionsimage:v1.0.0
    
  4. Azure Functions automatically redeploys the image to your functions app; the process takes place in less than a minute.

  5. In a browser, open https://<app_name>.scm.chinacloudsites.cn/ and replace <app_name> with your unique name. This URL is the Advanced Tools (Kudu) endpoint for your function app container.

  6. Sign in to your Azure account, and then select the SSH to establish a connection with the container. Connecting might take a few moments if Azure is still updating the container image.

  7. After a connection is established with your container, run the top command to view the currently running processes.

    Screenshot that shows Linux top command running in an SSH session.

Next steps

The following articles provide more information about deploying and managing containers: