Working with containers and Azure Functions
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:
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 thefunc 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 thefunc 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.
Create your function app in a container
With a Functions-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 an existing deployment to use the new image. You can update the function app to use the new image either by using the Azure CLI or in the Azure portal:
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.
In the Azure portal, locate your function app and select Deployment > Deployment center on the left-hand side.
Under Settings, select Container registry for Source, update the Full Image Name and Tag value to the update version in the registry, and then select Save.
The specified image version is deployed to your app.
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:
az functionapp config container show
: returns information about the image used for deployment.az functionapp config container set
: change registry settings or update the image used for deployment, as shown in the previous example.
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.
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 theaz functionapp deployment container show-cd-url
command.As before, replace
<APP_NAME>
with your function app name.Copy the deployment webhook URL to the clipboard.
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.
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).
In your Dockerfile, append the string
-appservice
to the base image in yourFROM
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.
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 .
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
Azure Functions automatically redeploys the image to your functions app; the process takes place in less than a minute.
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.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.
After a connection is established with your container, run the
top
command to view the currently running processes.
Related articles
The following articles provide more information about deploying and managing containers: