Working with containers and Azure Functions
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 on Azure Container Apps
To learn more about deployments to Azure Container Apps, see Azure Container Apps hosting of Azure Functions.
Creating containerized function apps
Functions maintains a set of lanuage-specific base images that you can use to generate your containerized function apps. When you create a Functions project using Azure Functions Core Tools and include the --docker
option, Core Tools also generates a .Dockerfile that is used to create your container from the correct base image.
Update an image in the registry
When you make changes to your functions code project, 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 pushed 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:
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
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.azurewebsites.net/
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.
Next steps
The following articles provide more information about deploying and managing containers:
Feedback
Submit and view feedback for