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.
Download Visual Studio code from Microsoft’s official website https://code.visualstudio.com/Download
- Install the Azure function command line tools using PowerShell or terminal
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
- Download Docker desktop from docker hub official website https://www.docker.com/products/docker-desktop/
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.
Once you install VS Code, just type “code .” In the terminal, and it should open Visual Studio Code GUI.
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.
Once you install docker desktop, use the below command to verify.
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.
For this demo, I have created a folder on my desktop and opened the folder in terminal.
To create a function app, use the below command.
Once the function is created, we need to create an endpoint with HTTPTrigger which returns something like below.
Now we are done with function app creation and now we will test it by running this on localhost.
Hit the highlighted URL, and you will see the following result.
Let’s open this in VS Code. Hit the following command.
Code:
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.
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 .
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
- docker run: This is the base command to run a container.
- –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.
- -it: This combines two options:
- -i stands for interactive, which keeps the STDIN open even if not attached.
- -t allocates a pseudo-TTY, which allows you to interact with the container through the terminal.
- -p 8080:80/tcp: This option maps a port on your host machine to a port in the container:
- 8080 is the port on the host machine.
- 80 is the port inside the container.
- tcp specifies the protocol. Here, it explicitly states that the TCP protocol is being used.
- mydemofuncapp:latest: This specifies the image to use for the container.
- mydemofuncapp is the name of the image.
- 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.
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.