Containerized Azure Functions Apps


In the previous article, I developed a REST API with Azure Functions using SQL database. So, In this article, I will show you how to containerized Azure Functions Apps using Docker Desktop.
You are required to have basic knowledge of Azure Functions and beginner level knowledge of Docker.
Required Tools
  1. Azure Functions Core Tools
  2. Azure Functions SDK
  3. Docker Desktop
Road Map of Article
  • Create a Docker File
  • Create Container Image
  • Run Image Inside Container
  • Testing APIs with Postman
Let's Get Started 
I will use my existing Azure function app that was created in my last article.
Step 1 - Create a Docker File
Let's open your function app project and go to the Tools tab and select Command Line and then select Developer Command Prompt.
After opening the window like shown above on your local, right now you are the outside of your source code files, so let's go inside source code files. This is because we will create a docker file inside the main folder where all files live like our classes, csproj file, etc. In the command prompt, write the dir command and hit enter. Now you can see all the files and folders that your application contains. Here, you can see a folder named RESTApiWithAzureFunction, so this is our main folder where we will create our docker file. To go inside this folder, write the cd {Your folder Name} command and hit enter. As shown below, I highlighted the commands. 
  1. dir                            //to check the folders and files inside the current directory  
  2. cd RESTApiWithAzureFunction   //to change the current directory      
Now we are in the right place and here we will use Azure Functions Core Tools to generate the docker file. Run the following command. This command will create a new docker file with all of its dependent base images.
  1. func init --docker-only  
Step 2 - Create a Container Image
We have added a Docker file inside our function app. Finally, it's time to create a container image with its dependent environments. Write the following command in order to create the image. If you already have all the dependent base images on your local, then it will take a very short time. However, if you are creating it for the first time, then it will take a bit longer because at first, Docker will pull all the base images from the Docker hub that the Azure function-core tool mentions inside the docker file. In the following command, you can see the docker build instruction and after that -t will specify the tag that specifies in the command with colon azurefuncimage:v1, and at the end, you can see a simple dot or period that indicates the current directory.
(Image Name = azurefuncimage)
  1. docker build -t azurefuncimage:v1 .  
Step 3 - Run Image Inside Container
We already built our Azure function app docker image. Run the following command to see all images that we have in Docker.
  1. docker images  
Now, we will run this image inside a new container. I will also add an environment variable in the command that will create and run the container because I want to add my SQL database connection string while creating a new container. Run the following command to get a new container up and running.
  1. docker run -e SqlConnectionString ",1433;Initial Catalog=learning;Persist Security Info=False;User ID={Your ID};
  2. Password= {Your Password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" -p 8080:80 azurefuncimage:v1  
The docker run command will run the container based on the image that I specify at the end of the command. -n  tag specifying the name of the container and -e tag specifying the environment variable and -p tag will map the port that we want to expose to the outside of the container in my case, I am exposing 8080 port for public accessing and the 80 port is the internal port of the container. At the end of this command, you can see the image name with a tag that we created in the last step.
Run the following command to see all containers that we have in Docker.
  1. docker ps  
Now if you want to stop your running container, then run the following command in order to stop the container. At the end of this command, add the container Id that you can get using the docker ps command.
  1. docker ps stop containerId
Step 4 - Testing API’s with Postman
Open Postman and hit the following URL:
Post Request  - Create a new task:
Get Request - Get Task By Id: 
Get Request - Get Tasks List:
Azure Function Logs Inside Container:
What Next?
In the next part of this article, I will demonstrate how to run containers inside Azure. Also, I will explain all steps of how to implement continuous integration and continuous deployment for Azure Function applications. Stay Tuned! Develop A REST API With Azure Functions Using SQL

Similar Articles