Getting Started with Azure Functions on Docker!

Ezequiel Cuagliarella
5 min readJun 16, 2019

--

Hello and welcome to my blog post! Today we’re going to learn how to create a serverless application using Microsoft Azure Functions and then run it locally using Docker.

In other words, we’re going to use Azure Functions to create our app and a Docker container to run it!

So, let’s start with a little introduction to these technologies.

What is Azure Functions?

Azure Functions is an event-based compute platform that lets you develop serverless applications. It allows you to run small pieces of code or functions without having to worry about the whole application or infrastructure behind. That way, we can focus on our code functionality for the problem at hand.

Azure Functions can run either in the cloud using Microsoft Azure services, or locally, using a container software.

Why Azure Functions?

It supports a wide variety of programming languages in which we can write our code, such as C#, JavaScript, or Python, with more being added and worked on every update.

Also, you can develop your functions starting with handy templates, easily share them, and import new ones into your project.

What is Docker?

Docker is a container platform. It creates what is called a container, an environment for our applications to run in.

You can think of containers as small virtual machines whose purpose is to run a specific application.

Why Docker?

Containers package up the code and all its dependencies in what are called images, so the application runs quickly and reliably from one computing environment to another.

A container image is an executable package that includes everything needed to run an application: code, runtime, system tools, system libraries and settings. Compared to a virtual machine image, a container image is faster and lighter.

Alright, that’s enough theory for now. Let’s get our environment set up so we start working on our first Function App!

What do we need?

Azure Functions Core Tools are installed using npm inside Node.js.

Let’s get started!

First, we’ll prepare our project files.

Step 1: Create a folder for our project.

Step 2: Run a Windows command prompt and go to our created folder.
We can navigate to said folder by using the cd command:

cd <your_folder_path>

Step 3: Initialize our project and prepare it for Docker.

func init . --docker

Then we select our worker runtime by highlighting the options using the arrow keys and pressing enter.

This determines the language in which our program will be written.

  • dotnet (C#).
  • node (JavaScript).
  • python.
  • powershell.

Since I’ll code in C# for this project, I’ll choose dotnet.

The func init command creates our initial project files while the docker flag creates a dockerfile needed to build the container image.

Step 4: Create our function from a template.

func new

We now have to select our function template within a variety of event templates such as QueueTrigger, HttpTrigger, TimerTrigger, etc.

Same as before, we highlight the options with the arrow keys and press enter.

I’ll select HttpTrigger for this project.

Then, we’ll be asked to name our function.

The func new command lets us create our function.

Step 5: Code your awesome function!

Enough commands for now, let’s get our hands on the code!

If you take a look at your project folder you’ll see a .cs file named after your function. This is where we’ll write our code in.

This is how our .cs file looks like initially.

Feel free to edit it with any text editor of your preference. Since this is C#, you could also use Visual Studio Code!

Step 6: Change the Authorization Level.

This is important! In order for us to test our function, it is necessary that we change its Authorization Level inside our .cs file from ‘Function’ to ‘Anonymous’.

Step 7: Create our container image.

Now that we have our function ready, we’ll create a container image so we can run in with Docker!

docker build -t <your_function_name> .
Lowercase only!

The docker build command creates a container image for the project using the dockerfile.

Step 8: Run the container image.

Now that our image was successfully created, we only have to run it.

docker run -p <port> <your_function_name>

You can choose any port you’d like. I’ll use port 8080 in this case.

The docker run command runs the image inside its designated container.

Step 9: Test your function!

DONE! Now if we head to localhost:8080 (or the port we’ve chosen on the previous step) on our browser we will see that our app is working!

And now to access and run our function we go to:

localhost:<port>/api/<your_function_name>?<parameter>=<value>

For example, our default template function would be:

localhost:8080/api/testfunction?name=Felix

That’s it!

We’ve learned how to develop our first function and run it in inside a Docker container!

What’s next?

This combination of technologies opens up a world of possibilities!

Expand your knowledge, be creative, keep on learning, create awesome applications, and most importantly, have fun!

All the information was taken from the official Microsoft Azure Functions and Docker documentations.

Here you’ll find samples and demos from the community and here’s a link to my Azure Functions Demo Project on GitHub.

Thank you for reading! I hope you enjoyed and learned something new!

--

--