Toll Free:

1800 889 7020

Creating a Dockerfile to Deploy .NET 9 Azure Functions with VS Code and CLI

Agenda: In this article, we will learn that how to create a .net 9 azure function app and deploy it to the docker using Dockerfile. Whether you’re looking to hire ASP.NET developers for your next project or need expert ASP.NET Development services, this guide will walk you through the process of deploying an Azure function app using Docker and Visual Studio Code.

Download .NET SDK for Windows or Mac based on your operating system from https://dotnet.microsoft.com/en-us/download/visual-studio-sdks.

image 44

Download Visual Studio code from Microsoft’s official website https://code.visualstudio.com/Download

image 45
  • Install the Azure function command line tools using PowerShell or terminal
image 46

If you are on Windows platform use the command

winget install Microsoft.Azure.FunctionsCoreTools

If you are on Mac, use the commands

brew tap azure/functions

brew install azure-functions-core-tools@4

For more information, you can refer to https://github.com/Azure/azure-functions-core-tools?tab=readme-ov-file#azure-functions-core-tools

image 47

Verify the installations using below ways

Once you install .NET SDK, type the below command and you should see the result like below. If the result is something different, then there might be some issue with .net installation.

image 48

Once you install VS Code, just type “code .” In the terminal, and it should open Visual Studio Code GUI.

image 49

Once you install Azure function core tools, type the following command to verify. If the result is something different, then there might be some issue with the installation.

image 50

Once you install docker desktop, use the below command to verify.

image 51

First we will create the Azure function using pre-defined templates that comes out of the box with Azure function core tools.

Create a directory where you want to create the project and open the directory in the command line tool like PowerShell or Terminal.

image 52

For this demo, I have created a folder on my desktop and opened the folder in terminal.

image 53

To create a function app, use the below command.

image 54

Once the function is created, we need to create an endpoint with HTTPTrigger which returns something like below.

image 55

Now we are done with function app creation and now we will test it by running this on localhost.

image 56

Hit the highlighted URL, and you will see the following result.

image 57

Let’s open this in VS Code. Hit the following command.

Code:

image 58

If you check Line 21, we are returning exactly same response which we saw when we hit localhost URL when we ran the app. This ensures that the azure function with HttpTrigger is working as expected from localhost server.

Now we need to create a Dockerfile which helps us in bundling the package contents into an image which we will later deploy to docker.

Since we used the Azure function command line tools, we used —docker option in the command at the time of function app creation. It gave us a docker file with predefined configuration.

Open Dockerfile and you will see code like below.

image 59

If you don’t see this file by default, you can manually create it and write the same code in it. Just make sure that you are creating this file in the root directory of the project.

  • Line 1 specified the base image to use for the build process. Here, it is using the .NET SDK 9.0 image from Microsoft’s container registry.
  • AS installer-env creates a named build stage called installer-env, which allows you to reference this stage later in the Dockerfile.
  • Line 3 copies the contents of the current directory (where the Dockerfile is located) to the /src/dotnet-function-app directory inside the Docker image.
  • RUN cd /src/dotnet-function-app changes the working directory to /src/dotnet-function-app.
  • mkdir -p /home/site/wwwroot creates the directory /home/site/wwwroot if it does not already exist.
  • dotnet publish *.csproj –output /home/site/wwwroot runs the dotnet publish command on the project file (*.csproj), outputting the published application to /home/site/wwwroot.
  • Line 8 specifies the runtime base image, which is the Azure Functions image for .NET isolated worker with .NET 9.0. This image will be used to run the application.
  • Line 9-10 specified the Environment configuration variables which are basic need of an Azure function to execute.
  • Line 12 copies the published application from /home/site/wwwroot in the build stage to /home/site/wwwroot in the final runtime image.

Now, in the same command line tool, which we used previously, hit the below command to build an image with this dockerfile

docker build -t mydemofuncapp:latest .

image 60

Once the image is built, we need to create a container of it, which will make the app in an executable state or running state. Run the following command.

docker run –rm -it -p 8080:80/tcp mydemofuncapp:latest

image 61
  1. docker run: This is the base command to run a container.
    1. –rm: This option ensures that the container is automatically removed after it stops. This helps in keeping your Docker environment clean by not leaving stopped containers behind.
    2. -it: This combines two options:
    3. -i stands for interactive, which keeps the STDIN open even if not attached.
    4. -t allocates a pseudo-TTY, which allows you to interact with the container through the terminal.
    5. -p 8080:80/tcp: This option maps a port on your host machine to a port in the container:
      1. 8080 is the port on the host machine.
      2. 80 is the port inside the container.
      3. tcp specifies the protocol. Here, it explicitly states that the TCP protocol is being used.
  2. mydemofuncapp:latest: This specifies the image to use for the container.
    1. mydemofuncapp is the name of the image.
    2. latest is the tag of the image, usually indicating the latest version of the image.

Now hit http://localhost:8080/api/httpdemo and you will see the exact same result that we saw on running the app on localhost server.

image 62

Hence, we deployed the Azure function to Docker using Dockerfile.

Conclusion:

With the steps outlined above, we successfully created and deployed an Azure Function app to Docker. For businesses looking to streamline their development process, ASP.NET Development services can be crucial in building scalable and efficient applications.

Harsh Savani

Harsh Savani is an accomplished Business Analyst with a strong track record of bridging the gap between business needs and technical solutions. With 15+ of experience, Harsh excels in gathering and analyzing requirements, creating detailed documentation, and collaborating with cross-functional teams to deliver impactful projects. Skilled in data analysis, process optimization, and stakeholder management, Harsh is committed to driving operational efficiency and aligning business objectives with strategic solutions.

Scroll to Top