From Azure Function v2, .NET developers can now write functions using .NET Core.
This means you can develop and run your Azure functions in more places
(cross-platform). This opens up opportunities for running your Azure function in
Docker container and taking it one step ahead by running Azure Functions on
Kubernetes with
KEDA (Kubernetes-based Event
Driven Autoscaling). In this blog post, we will see how to run Azure Function in
Docker Container.
Now few questions arise like why do we need to run in a container, do we lose the benefit of Serverless because you will be paying only for the time your function is running based on your plan in Azure Function, etc. All these questions are valid but as soon as we enter the world of containerization and Kubernetes we want our application to be flexible enough to be hosted in an on-premises/cloud/hybrid environment. With containerization now you can use Azure Functions as your event-driven host regardless of the environment. By having Azure Function in a docker container it enables options for hosting in different environments.
Pre-requisites
Build the Azure Functions container image and test locally
1. In this demo we will
Create Azure Functions
with
HttpTriggered template and to support cross-platform we will
be creating Azure Function v2 and v3 versions. You can similarly create new
Azure Functions using Azure CLI commands e.g. func new
2. Create a Docker file for the respective version of Azure Function using
following command.
- Navigate to Azure Functions Project folder
-
Using the CLI tool/cmd prompt/PowerShell terminal use the following
command
func init --docker-only --worker-runtime dotnet --chsarp
In the above command flag --docker tells Azure Function Core Tool to generate the Dockerfile
- By default, the Docker file is created for Azure Function v2 even if you are using for Azure function v3 version. In that case please update the Docker file with the appropriate image version as shown in the below code snippet.
Azure Function v2 version of Docker file:
FROM microsoft/dotnet:2.2-sdk AS installer-env
COPY . /src/dotnet-function-app
RUN cd /src/dotnet-function-app && \
mkdir -p /home/site/wwwroot && \
dotnet publish *.csproj --output /home/site/wwwroot
# To enable ssh & remote debugging on app service change the base image to the one below
# FROM mcr.microsoft.com/azure-functions/dotnet:2.0-appservice
FROM mcr.microsoft.com/azure-functions/dotnet:2.0
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
AzureFunctionsJobHost__Logging__Console__IsEnabled=true
COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"]
Azure Function v3 version of Docker file:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS installer-env
COPY . /src/dotnet-function-app
RUN cd /src/dotnet-function-app && \
mkdir -p /home/site/wwwroot && \
dotnet publish *.csproj --output /home/site/wwwroot
# To enable ssh & remote debugging on app service change the base image to the one below
# FROM mcr.microsoft.com/azure-functions/dotnet:3.0-appservice
FROM mcr.microsoft.com/azure-functions/dotnet:3.0
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
AzureFunctionsJobHost__Logging__Console__IsEnabled=true
COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"]
- Build Azure Function docker images using the below commands by navigating to the docker file folder.
docker build -t azurefuncv3 .
- From the above step docker image is built locally, now let's see what is the command for running it locally.
docker run --rm -it -p 7071:80/tcp azurefuncv3:latest
Now you should be able to browse the HttpTriggered endpoint Azure function which is running inside a container.
Comments
Post a Comment