Containerized Azure Functions- Part 1
Azure functions supports containerized function app deployments. In a containerized deployment, you create your own function app instance in a local Docker container from a supported based image. You can then deploy this containerized function app to a hosting environment in Azure or run it locally as well. Creating your own function app container lets you customize or otherwise control the immediate runtime environment of your function code.
Before we jump into solution, make sure below pre-requisites are met.
Pre-requisite-
Virtualization should be enabled.
Install docker desktop.
WSL Ubuntu to run Linux based containers.
Powershell.
Follow below steps to create azure function project from scratch, create docker image and run container locally.
1. Setup function app project-
To create containerized function app, we can use Azure extension as shown below. Under workspaces, click on function app icon and you can see option for - Create New Containerized Project. Follow the prompts to setup project in required directory and language. Here we will use Azure function with C# language with runtime as .NET 6.0 LTS.
Develop your functions under this project. For demo purpose, we will add one Service bus triggered function (ServiceBusTopicTrigger1) and one HTTP triggered function (HttpTrigger1).
For this demo, we will use authorization level as anonymous for HTTP triggered function.
In part-2 blog, we will see how to use function level authorization.
2. Dockerfile
Next step is to create Dockerfile. In order to create to containerize any app, it requires a docker configuration file called "Dockerfile" (without any extension). When we create project, it will also generate Dockerfile, but we will overwrite it as below in order to simplify.
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS installer-env
COPY ./bin/Release/net6.0/publish/ /home/site/wwwroot # this will copy published artifacts to wwwroot folder
# To enable ssh & remote debugging on app service change the base image to the one below
# FROM mcr.microsoft.com/azure-functions/dotnet:4-appservice
FROM mcr.microsoft.com/azure-functions/dotnet:4
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
AzureFunctionsJobHost__Logging__Console__IsEnabled=true
ENV SB_Conn_String=<sb_connection_string>
COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"]
Note that Dockerfile contains environment variables required for azure function, example service bus connection string (SB_Conn_String). These are the same variables that we add in local.settings.json.
3. Build docker image
Before building docker image, we need to build and publish the code using below powershell commands. Open powershell and execute below commands-
> cd /<path to azure function directory containing .proj file>
> dotnet build -c release
> dotnet publish -c release
Once code is successfully published, run below command to build docker image. Make sure docker desktop is running in system.
> docker build --tag local/containerizedfnapp .
In above command, local/containerizedfnapp is name of image. This command will build docker image and show as below in docker desktop.
4. Run docker container locally
Once image is built, we can run the container with below command-
docker run -e WEBSITE_HOSTNAME=localhost -p 8080:80 local/id10072
>docker run -e WEBSITE_HOSTNAME=localhost -p 8080:80 local/containerizedfnapp
In this command, we have mentioned image name and port binding 8080:80. You can see running container in docker desktop.
When you browse port localhost:8080, you can see function app is up and running.
Hope you find this blog helpful.
You can find the repository of above discussed sample project at - https://github.com/SurajSomani14/containerized-az-function-app
Thanks for reading !
Subscribe to my newsletter
Read articles from Suraj Somani directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Suraj Somani
Suraj Somani
I am 10+ years experienced IT professional having expertise in various Azure technologies. I am certified Azure Developer & Azure Data Engineer, having vast experience in building cloud solutions.