Containerising Azure Functions without Dockerfile by info.odysseyx@gmail.com August 23, 2024 written by info.odysseyx@gmail.com August 23, 2024 0 comment 12 views 12 Inside me Previous PostWe’ve discussed the various containerization options for .NET developers, so let’s shift focus a bit. Azure Functions App. How can I containerize my Azure Functions app? Fortunately, Azure Functions Core Tools It basically supports Dockerfile. But it is Dockerfile What is the only option for containerizing a .NET-based Azure Functions app? This post explains how to containerize an Azure Functions app. Dockerfile. You can find sample code here. GitHub Repository. Prerequisites There are several prerequisites to effectively containerizing .NET-based function apps. Containerize Dockerfile Using Azure Functions Core Tools, you can create a new Azure Functions app with the following command: func init FunctionAppWithDockerfile \ --worker-runtime dotnet-isolated \ --docker \ --target-framework net8.0 You can find out --docker An option for the command. This option produces: Dockerfile It’s in the project directory. Dockerfile It looks like this: FROM mcr.microsoft.com/dotnet/sdk:8.0 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-isolated:4-dotnet-isolated8.0-appservice FROM mcr.microsoft.com/azure-functions/dotnet-isolated:4-dotnet-isolated8.0 ENV AzureWebJobsScriptRoot=/home/site/wwwroot \ AzureFunctionsJobHost__Logging__Console__IsEnabled=true COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"] There are a few points Dockerfile Pick up: The default images for the runtime are: mcr.microsoft.com/azure-functions/dotnet-isolated:4-dotnet-isolated8.0. There are two other options available. mcr.microsoft.com/azure-functions/dotnet-isolated:4-dotnet-isolated8.0-slim and mcr.microsoft.com/azure-functions/dotnet-isolated:4-dotnet-isolated8.0-marinerYou can choose any one of them according to your requirement. The function app is running. /home/site/wwwroot directory. There are two environment variables: AzureWebJobsScriptRoot and AzureFunctionsJobHost__Logging__Console__IsEnabled. doesn’t exist ENTRYPOINT The command has been defined. These points will be used later in this post. Let’s add a new function to our Azure Functions app using the following command. func new -n HttpExampleTrigger -t HttpTrigger -a anonymous Open after adding. HttpExampleTrigger.cs Modify the file as follows: // Before return new OkObjectResult("Welcome to Azure Functions!"); // After return new OkObjectResult("Welcome to Azure Functions with Dockerfile!"); You can now build the container image using the following command: docker build . -t funcapp:latest-dockerfile You have a container image for your Azure Functions app. You can run the container with the following command: docker run -d -p 7071:80 --name funcappdockerfile funcapp:latest-dockerfile Now that your function app is running in a container, open a browser and navigate to: http://localhost:7071/api/HttpExampleTrigger Make sure your function app is running. Open Docker Desktop and check the running containers. Can you see it? Path and Args value? Path The value is /opt/startup/start_nonappservice.sh and Args value is an empty array, i.e. the shell script start_nonappservice.sh It runs when the container starts and requires no arguments. Keep this information in mind, as we will use it later in this post. Once done, stop and remove the container using the following commands: docker stop funcappdockerfile && docker rm funcappdockerfile So far, we have containerized our Azure Functions app. Dockerfile. However, as mentioned earlier, there is another way to containerize your Azure Functions app. Dockerfile. Let’s continue. Containerize dotnet publish MSBuild natively supports containerization, so you can leverage: dotnet publish The command to build the container image for your function app. This option is really useful if you want to dynamically set the base container image. For this, you may need to update: .csproj Create a file to include containerization settings. First, create a new function app without containerization settings. --docker Options. func init FunctionAppWithMSBuild \ --worker-runtime dotnet-isolated \ --target-framework net8.0 Let’s add a new function to our Azure Functions app using the following command. func new -n HttpExampleTrigger -t HttpTrigger -a anonymous Open after adding. HttpExampleTrigger.cs Modify the file as follows: // Before return new OkObjectResult("Welcome to Azure Functions!"); // After return new OkObjectResult("Welcome to Azure Functions with MSBuild!"); Now the fun part begins. FunctionAppWithMSBuild.csproj Open the file and add the following nodes: Have you verified that these two environment variables are identical to the following two environment variables? Dockerfile? Let’s add another node. .csproj file: This node specifies the command to run when the container starts. This is as follows: Path value Docker Desktop Screenshot. Now you can build the container image with the following command. dotnet publish ./FunctionAppWithMSBuild \ -t:PublishContainer \ --os linux --arch x64 \ -p:ContainerBaseImage=mcr.microsoft.com/azure-functions/dotnet-isolated:4-dotnet-isolated8.0 \ -p:ContainerRepository=funcapp \ -p:ContainerImageTag=latest-msbuild \ -p:ContainerWorkingDirectory="/home/site/wwwroot" memo: You can set the base container image. mcr.microsoft.com/azure-functions/dotnet-isolated:4-dotnet-isolated8.0-slim or mcr.microsoft.com/azure-functions/dotnet-isolated:4-dotnet-isolated8.0-marinerIt depends on your requirements. This command uses four attributes: ContainerBaseImage, ContainerRepository, ContainerImageTagand ContainerWorkingDirectory. With this dotnet publish Using the command gives you the same development experience as building container images. docker build Without relying on commands Dockerfile. To run the container, run the following command: docker run -d -p 7071:80 --name funcappmsbuild funcapp:latest-msbuild Now that your function app is running in a container, open a browser and navigate to: http://localhost:7071/api/HttpExampleTrigger Make sure your function app is running. Open Docker Desktop and check the running containers. Can you see it? Path and Args value? Path The value is /opt/startup/start_nonappservice.sh and Args It’s worth it dotnet and FunctionAppWithMSBuild.dll. However, since shell scripts do not accept arguments, these arguments are ignored. Once done, stop and remove the container using the following commands: docker stop funcappmsbuild && docker rm funcappmsbuild You now have a containerized Azure Functions app. Dockerfile. With this dotnet publish The approach allows you to easily change the base container image, repository, tags, and working directory without any dependencies. Dockerfile. So far, we’ve looked at two different options for .NET developers to containerize their .NET-based Azure Functions apps. You can write: Dockerfiles or use dotnet publish How to build container images. Which one do you prefer? Want to learn more about MSBuild for containers? If you would like to learn more about container options using MSBuild, the following links may be helpful: This article was originally published on: Devkimchi. Source link Share 0 FacebookTwitterPinterestEmail info.odysseyx@gmail.com previous post Exciting Social Media Marketing Job Opportunities at Ramp Asia in Delhi for Freshers and Professionals next post California Consumer Privacy Act (CCPA) Opt-Out Icon You may also like 7 Disturbing Tech Trends of 2024 December 19, 2024 AI on phones fails to impress Apple, Samsung users: Survey December 18, 2024 Standout technology products of 2024 December 16, 2024 Is Intel Equivalent to Tech Industry 2024 NY Giant? December 12, 2024 Google’s Willow chip marks breakthrough in quantum computing December 11, 2024 Job seekers are targeted in mobile phishing campaigns December 10, 2024 Leave a Comment Cancel Reply Save my name, email, and website in this browser for the next time I comment.