Azure Functions in a Docker Container

Matias Miguenz
Jan 14, 2019 · 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:

Setting up our enviroment:

Let’s do this!

Conclusion

Azure Functions and Serverless

Image for post
Image for post

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

Image for post
Image for post

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:

Image for post
Image for post
--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:

Image for post
Image for post
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:

Image for post
Image for post

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.

Image for post
Image for post
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:

Image for post
Image for post
Here we are sending the request
Image for post
Image for post
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.

Image for post
Image for post

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

Image for post
Image for post
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:

Image for post
Image for post

And then, we can run it:

Image for post
Image for post
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:

Image for post
Image for post
Image for post
Image for post

Also You can check you Containers running:

Image for post
Image for post

And turn them down:

Image for post
Image for post

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 :

Image for post
Image for post

Join our community Slack and read our weekly Faun topics ⬇

Image for post
Image for post

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 Creative Developers & DevOps Enthusiasts

Sign up for FAUN

By FAUN

Medium’s largest and most followed independent DevOps publication. Join thousands of aspiring developers and DevOps enthusiasts Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Matias Miguenz

Written by

FAUN

FAUN

The Must-Read Publication for Creative Developers & DevOps Enthusiasts. Medium’s largest DevOps publication.

Matias Miguenz

Written by

FAUN

FAUN

The Must-Read Publication for Creative Developers & DevOps Enthusiasts. Medium’s largest DevOps publication.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store