Azure Functions with Docker
In this post I will try to guide you trought the creation of a simple Azure function using docker and how to upload it to your Azure portal.
For this I will be using C# in windows 10 and assuming you are also using windows and know some programming language. If you are using Linux, the docker commands are the same but writing sudo first and the installation process may be different.
Note: I used a lot of embed links while writing the post, don’t forget to visit these links to get a better understanding of the subject!
What is Azure functions?
Essentially, the merge of code and events, a way to deploy a function in a platform that will execute when its called or something happens, like an schedule or an http request.
This is a function provided by the azure platform to be able to perform a particular operation like a service using the concept of serverless, which means running custom code on demand in the cloud, delegating all environment setting tasks to azure, which means you don’t have to set up an environment or worry about scalability because the azure platform does all that for you.
If you want read more about azure functions here is the official documentation.
What is docker?
Have you ever had a problem running an app or program in a computer, even when you tried it somewhere else and it worked perfectly?
Docker comes to solve this problem using a similar approach to using a virtual machine, where you can control every factor running the app and replicate that using the same virtual environment elsewhere, but solving a lot of the problems of using an actual virtual machine.
For this, Docker wraps your app in an isolated environment called a container with all the dependencies and processes required but being able to use resources from your operating system instead of running a new one like a virtual machine, saving a lot of resources, which means better loading times and more portability while still being isolated.
To learn more about how docker works you can watch this short video or check their website .
Set up
Azure Account:
To register go to the Azure website, you can make a free account but with some limitations. This is an optional step if you don’t want to upload your app to the Azure Portal.
Installing Docker:
Go to docker’s hub and download Docker Desktop to install it. To verify the installation you can run “docker version” in a command prompt.
You can explore docker’s hub to explore images uploaded by other users.
Azure Functions Tools:
To install this tool you will have to install node js from this link and then go to the command prompt (search cmd or press win+r and then write cmd and enter) and enter these line
npm install -g azure-functions-core-tools
For more installation options go to the core tools readme.
.NET core:
The last piece we need is the core .NET sdk, you can get it here.
Let’s create our function!
For this section I will be working with C# but Azure functions support multiple languages.
My function will be a simple http request handle that will encrypt the given text in SHA1 code (to now more about SHA1 encryption go here), but you can do whatever you want or even use the basic template that the tool creates for you. You can also use the same code I’m using for the example from here (the code in the encode folder is the one I’m using for this example).
The workfolder
Create a new folder to work with or open the folder where you want to work and open a windows power shell or command prompt there, you can use a shorcut to open the power shell directly in the folder be pressing shift + right click and choosing “open PowerShell windows here” or “open command windows here”.
Initial files
Open the window’s console in the folder and run
func init . --docker
you will be asked in which language you want to work, select it with your cursors and hit enter and all the initial files will be created in the folder.
Create the function
In the same location run
func new
Now you have to choose a template to start with, in the documentation you can find what you can do with each one, but I will choose HttpTrigger for this demo.
After choosing your template you have to choose a name for your function, just type the name and press enter, my function will be named “encode”.
You should get a line saying your function was created successfully.
Write the function
Now your folder should look like this
Our function is the one with the name we gave in the console ending with the .cs (because I’m using c# for this example but yours may be different), you can edit that file to program your own function to do what you need.
If you don’t want to write code for this example you can use the template which will be a function requesting a string name as querry or body and will return a string saying hello and the given name. Or you can use one my codes from this Github repository with two different examples, encode is the one I’m using here.
The one thing you should change is the AutorizationLevel in the function file and set it to anonymous so you can access your function without requiring an authentication, this way any valid HTTP request passes.
To learn more about authorization level you can read this post.
Build the Dockerfile and Run it!
To build the dockerfile just run the next code in a console inside the folder, just remember that “encode” is the name I gave to my function, yours may be different
docker build -t encode .
Once the docker file is built you can run it running the next line in a console in the folder, here 8000 is the port we will be using and remember that “encode” is the name of my demo, but yours may be different.
docker run -p 8000:80 encode
If you go to http://localhost:8000/ you should see your function running, but to see your actual implementation you have to go to http://localhost:8000/api/yourFunctionName?yourQuery=yourData replacing yourFunctionName for the name you gave the function, yourQuery for the parameter name you wrote and yourData for the data to enter, in my example it is http://localhost:8000/api/encode?toencrypt=thisismyfirstazurefunction
Congrats, now you have your Azure function using docker working!
If you need to make changes in your function remember to build again the docker file to apply the changes to the image in the container using “docker build”
One more thing, to stop a containers you can run “docker ps” to list all the containers running and see their name and then “docker kill container_name” to stop the container with that name.
Deploy Time!
For this section I will use the other example in my GitHub repository with the examples, it gets a number as parameter and returns a text telling if your number is prime or not.
For this section you will need an Azure account and the account from Docker Hub you created to download docker desktop.
Docker Hub
Docker hub is a repository for containers built with docker, if you want to explore there is a lot of useful things, but now we will use it to upload our container’s function and deploy it to the Azure portal.
To push your docker image to the hub run these commands:
docker tag hello-docker-function YOURACCOUNT/hello-docker-function
docker push YOURACCOUNT/hello-docker-function:latest
Take note of your tag, but it will be yourUsername/yourFunctionName anyway and you can access your account to look at it.
For example you can find the demo I’m using in https://hub.docker.com/r/santgr11/isprime and it’s tag is santgr11/isprime.
Azure Portal
Now in the azure portal go to function app, create a new one and fill all the fields with your function data, remember that your function tag is your yourUsername/yourFunctionName if you are using a function uploaded by you. You can use the next image to compare your configuration.
After you hit CREATE it will take a few minutes to deploy your function and when its done you can go to your function app panel to open your function.
There you will find the url to your function and some options to manage it.
Let’s test it
As you can see now we have a link to access from anywhere to our function. Pretty useful right? Now you can use it in another program, from a website or wherever you need it!
And thats it, I hope this article was somehow useful and you learned something.
Now you can build your own Azure Function and use it for a lot things.
If you want to learn more about creating Azure functions here is a good tutorial from the “Microsoft Visual Studio” channel in youtube.
Here you can read about developing a big data pipeline using an Azure Function.
If you want to read more about this topic I used these other posts as reference while making my own experience.
And don’t forget you always have the official documentations about Azure and Docker.