You are required to have basic knowledge of Azure Functions and beginner level knowledge of Docker.
- 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.
- dir
- cd RESTApiWithAzureFunction
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.
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)
- 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.
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.
- docker run -e SqlConnectionString = "Server=tcp:development.database.windows.net,1433;Initial Catalog=learning;Persist Security Info=False;User ID={Your ID};
- 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.
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.
- 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:
https://localhost:8080/api/task/id
Get Request - Get Tasks List:
https://localhost:8080/api/task
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!