post-thumb

How to build a scalable C# web application in DockerFile for Container App in Azure Function?

Azure Function is a serverless and managed service. Now Azure also has released Container App plan which can allow you to easily build and deploy a custom doecker in web application to Azure Container App in Azunre Function.

You can enjoy building a high performance and scalable web application without worrying about how to manage and maintain Kubernete platform with Azure Container App.

How to get started?

I haved tried to ask ChatGPT to provide an example for Docker File.

However, it always provides a wrong example and its solution usually does not work out.

Because I think ChatGTP might not have correct and the latest training data for Azure Function’s Container App at this moment.

In this post, we will provide a Docker File in C# for you to build a high performance and scalable web appliction in Azure Function with Container App.


Create Azure Container Registry

Log in to Azure and create Azure Registry for your application to push an image

In this tutorial, we create api_server_image as an example.

Next, we start to build your web aplication in Docker File.

Build DockerFile

STEP 1 Login in to Azure

You can log in to Azure

az login

STEP2: Build Web Application in C#

This following shows how to build C# web application.

  • Use a light weight linux image apline-linux: sdk:8.0.401-alpine3.20 to build C# web appliction This is usefull for building micro-service requiring fast startup time and efficency

  • The outputted x.dll files will save in /home/site/wwwroot

  • Then copy /home/site/wwwroot to runtime image at /dotnet-isolated:4-dotnet-isolated8.0

  • This runtime image: /dotnet-isolated:4-dotnet-isolated8.0 is built for isoloated mode of Azure Function in .NET v8

# Use a light weight linux to build C# web appliction
FROM mcr.microsoft.com/dotnet/sdk:8.0.401-alpine3.20 AS installer-env

RUN mkdir /app
WORKDIR /app

COPY . .

# Restore the dependencies (NuGet packages)
RUN dotnet restore "./<Your VS Project>.csproj"

# Build and publish the application to the /app directory
RUN dotnet publish "./<Your VS Project>.csproj" -c Debug -r $BUILD_RUN_TIME -o /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

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

EXPOSE $TARGET_PORT

COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"]

STEP3: Upload Image to Azure Registry

The following shows how to upload an image to Azure Registry

TOKEN=$(az acr login --name ${ACR_NAME}  --expose-token --output tsv --query accessToken)

docker login ${ACR_NAME}.azurecr.io --username 00000000-0000-0000-0000-000000000000 --password ${TOKEN}

docker push $REMOTE_IMAGE

Docker File

We can create a script below to build and deploy Container App for Azure Function

// DockerFile

#!/usr/bin/env bash


TS=$(date +"%Y%m%d%H%M%S")
# You ACR Name
ACR_NAME="api_server_image"
REMOTE_IMAGE="${ACR_NAME}.azurecr.io/api-service:${TS}"
TARGET_PORT=80
AZURE_USERNAME="<Your User Name>"
AZURE_PASSWORD="<Your Password>"
AZURE_SUBSCRIPTION="<Your Subscription Name>"

BUILD_RUN_TIME="linux-x64"

echo "## Log in Azure ##"
# Manually log in with MFA
az login

# Connect to Subscription
az account set -s "$AZURE_SUBSCRIPTION"


echo "==========================================================================================="
echo "Use $REMOTE_IMAGE as docker image "
echo "TARGET_PORT $TARGET_PORT"
echo "ACR_NAME $ACR_NAME"
echo "==========================================================================================="


if [[ -d "api_server_image" ]]
then
    rm -rf api_server_image
fi

mkdir api_server_image

cp -rf <Your Project>/* api_server_image/
rm -rf api_server_image/bin
rm -rf api_server_image/obj

cd api_server_image

docker build --no-cache -t $REMOTE_IMAGE -f- . <<EOF
FROM mcr.microsoft.com/dotnet/sdk:8.0.401-alpine3.20 AS installer-env

RUN mkdir /app
WORKDIR /app

COPY . .

# Restore the dependencies (NuGet packages)
RUN dotnet restore "./<Your Project>.csproj"

# Build and publish the application to the /app directory
RUN dotnet publish "./<Your Project>.csproj" -c Debug -r $BUILD_RUN_TIME -o /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

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

EXPOSE $TARGET_PORT

COPY --from=installer-env /app/local.settings.json /home/site/wwwroot/
COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"]

EOF


TOKEN=$(az acr login --name ${ACR_NAME}  --expose-token --output tsv --query accessToken)

docker login ${ACR_NAME}.azurecr.io --username 00000000-0000-0000-0000-000000000000 --password ${TOKEN}

docker push $REMOTE_IMAGE

Conclusion

In this post, you have learned how to build a scalable C# web application for deploying to Azure Function in Container App.

You don’t need to type magic prompt to try and error for finding a workable Docker file.

This article will be helpful for you to save time for building a high performance and scalable C# web application for Azure Container apps in Azure Function.


If you found this article beneficial, please show your support by 👏 this article or follow us below to get the latest tutorials in the future.

Thank you for reading!

Visit us at the website: Game Tech Tutorial

Follow us on Twitter, Facebook, Reddit


You might be interested in

8 Steps to Solve Cold Start Problem in Azure Functions

How to integrate Azure Function and SQLite?