Azure Functions in a Docker Container

Matias Miguenz
Jan 14 · 8 min read

In this post, I am going to show you a quick example working with Azure Functions and Docker Containers. We are going to code our function in C# with DotNet Core using Visual Studio Code as our IDE. This is a great oportunity to dive into the new flexibility that came up with DotNet Core platform, which alows us to code, compile and run .NET software beyond Microsoft enviroments.

We are going to implement an azure function, run it locally and interetact with it using Post Man. After that, we are going to create a Docker image with our Azure Function, then again,we will run and interact locally, now with our Docker image running.


Overview

Brief tech context:

  1. Azure Functions and Serverless
  2. Docker containers

Setting up our enviroment:

  1. Install Dotnet sdk for Ubuntu
  2. Install Azure Functions Tools
  3. Install Docker

Let’s do this!

  1. Business context for our Azure Function
  2. Create and run our Azure Function
  3. Dockerize and run

Conclusion


Azure Functions and Serverless

Azure functions is one of the newest features of Azure Cloud Platform services. It comes with the idea of Faas (Function as a service) and serverless. This allows us, as programmers, to only worry about the code, delegating all environment setting tasks to Azure platform.

Azure Functions gives programmers the oportunity to write their pieces of code in serveral languages and deploy it easily, getting rid of the efforts that come with building and publishing servers . You can find more information about this service here.


Docker containers

At some point in the history of software developing, we had to deal with the scalability issue. So we needed to deploy several instances of our applications. By then we took the decision of building virtual machines, and scale our application using those tools. It worked well for a while and it was also cheaper than buying and deploying new physical servers, it seems to be a right desision, we did not want to get involve in troubles with the coexistence of the several dependencies of our applications, but.. was it worth ? We were replicating at least 100 GB of space and other resources (Operative Sistem and all random and needless software that came with them) that a virtual machines needs only for our 2 GB weight application.

What about if we can just freeze all our application and its dependencies in an image. Then we can replicate and run it in as many instances we want, and all of them coexisting in the same operative system. Yes, we want to encapsulate our applications in a something like containers, of course Docker Containers.


Setting up our enviroment

Install Dotnet sdk for Ubuntu

Well, as we are going to work with Linux Ubuntu enviroment and .Net Core application, we need to install DotNet Core SDK. In my case, while I was installing it in my Ubuntu 18.04, I got some dependencies troubles, so I needed to install DotNetCore 2.1 first. If you got this problem, you should follow this first before installing DotNetCore SDk 2.2.

Install Azure Functions Tools

You can follow this post in order to install Azure Functions Tools . You must have been installed NodeJs first.

Install Docker

You will be able to get Docker tools installed following this Docs


Let’s do this!

Business context for our Azure Function

Now, we will get hands on building our azure function. Azure functions are a geat solution for that cases where whe want to encapsulate some behavior we don’t want to scatter all over lots of applications, in this case we want to develop the person admission for some feature.

We are going to develop an azure function that is triggered by HttpRequest and recieve in the request body a person and then we are going to return a json with the data we got and adding a property which means if that person is admitted or not. In order to keep this implementation easy, we are just going to check the age of the person we received. If the person age is greater or equal than 18 we return admitted = true, otherwise it will be false.

Here you can check this code.


Create and run our Azure Function

After getting our enviroment set up , we are going to create the folder where we want to set our azure function and in a terminal we need to run this command:

--docker stands for creating our DockerFile that we will need later for dockerize our function.

After that, we will run this command in order to get our first function initialized:

We should choose the triger we want to use and write the function name

Then if we open our folder in Visual Studio Code, we will notice all the files that azure tools have created for us. It creates a standard example function:

Now, if we just run our code using Visual Studio Code Tools we will got the Azure Function running locally and the command line will provide us an endpoint to call for it.

Notice before running this I have already pasted the code I showed you before on my link to my github repository

Okey, now we will test our Azure Function using Post Man:

Here we are sending the request
Here we got the response

We can notice we send an object which represent for us a person, with 2 properties (name and age), and as the age is greater than 18 the azure funcion returns a json with the same data, adding the property admitted = true.


Dockerize and run

Cool, we got our Azure Function running. Now it is time to freeze it in a docker image and then run it.

First, we need to make some changes in our code. We need to change the authorization level for HttpTriger Atribute. It works fine when we are running azure function locally, but it will return us a 401 request error when whe run our Azure Function inside a Docker Container. We need to set AuthorizationLevel to Anonymous.

After have made this changes, we need to build our Docker image:

funcadmission_image is just the name I gave to my image, you can choose what ever you want.

Once we got our Docker image built, we can check it:

And then, we can run it:

Notice -p stand for choosing the port you want to your Docker Container listen to.

Now again, we are going to test it with Post Man:

Also You can check you Containers running:

And turn them down:

Conclusion

We had developed an Azure Function and combined it with Docker Containers, this are two pretty good tools now we have to develop our applications. Easily we can develop endpoints which can work out some stuff and get rid of all the heavy work that setting up servers demands.

Nervertheless I can’t avoid telling you about some awkward issues that I found while I was diving into Azure Function stuff:

If you took a read the documentation tha Azure Functions provides, you can easily notice that this feature has the advantage of scaling limitless. But you can bump into some problems with it:

At developer side:

Azure Functions can scale limitless but it does not necessarily mean that your third party server will. So the limitless scalability of azure functions can make troubles with the third party server you are using (ie: APIs, DataBases servers)

At Azure Cloud Platform side:

Your Azure Functions instances are limitless, right, but the sockets that azure provides to you are not. So if you are requesting third party servers with several instances in your Azure Function you can run out of the available sockets that the platform provides you.


All Links mentioned in this post :

Join our community Slack and read our weekly Faun topics ⬇

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

Faun

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts

Matias Miguenz

Written by

Faun

Faun

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade