Home NewsX Containerising Azure Functions without Dockerfile

Containerising Azure Functions without Dockerfile

by info.odysseyx@gmail.com
0 comment 6 views


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.

Container #1 running Azure Functions app

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.

Container #2 running Azure Functions app

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

You may also like

Leave a Comment

Our Company

Welcome to OdysseyX, your one-stop destination for the latest news and opportunities across various domains.

Newsletter

Subscribe my Newsletter for new blog posts, tips & new photos. Let's stay updated!

Laest News

@2024 – All Right Reserved. Designed and Developed by OdysseyX