Azure Functions + Docker Container

In this post, you will be able to learn, develop and deploy two of the most used technologies today.

But first, let’s give a brief introduction about each of them.


Azure Functions (Serverless Architecture)

Azure Functions is the serverless computing service hosted on the Microsoft Azure cloud (or locally) that enables you to run code on-demand without having to explicitly provision or manage infrastructure. The idea behind serverless computing, also known as function as a service, is to eliminate those infrastructure considerations for the user.

The main benefit

With serverless, a user can simply create and upload code, and then define the triggers or events that will execute the code. Triggers can come from a wide range of sources, including another user’s application or other cloud services.

Docker Container

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow you to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. The whole idea of Docker is for developers to easily develop applications, ship them into containers which can then be deployed anywhere.

Why Docker?

  • Docker has the ability to reduce the size of development by providing a smaller footprint of the operating system via containers.
  • You can deploy containers anywhere, on any physical and virtual machines and even on the cloud.
  • Since Docker containers are pretty lightweight, they are very easily scalable.

Installation

So before we create our first azure function and docker container, you must install:


Running Azure Functions on a Docker Container

Once you’ve learned a little about Azure Functions and Docker, and you’ve installed the tools, let’s jump into the code.

Create a folder for our function and container and open the command-line inside it.

Let’s begin:

  1. Choose the language and create the dockerfile
func init . --docker

You can choose the option you want. In this case, I choose “node”. We can see the “Dockerfile” after running the command. This file was added by the — docker option and has the information necessary to create the container.

2. Create the function and choose the template.

func new

As you can see, the command ask us for a template and a function name. Like the first step, you can choose the template you like. I choose HTTP Trigger because is very simple to demonstrate how it works.

3. Change the authLevel of our function.

Before we build the docker image, we must change the authLevel of our function.

Open the function.json file and change:

to

4. Create the docker image.

Once you’ve change the authLevel, let’s jump to create our docker image.

docker build -t <dockerImageName> .

5. Run the container.

Finally, run the container executing this command:

docker run -p 8080:80 <dockerImageName>

So now we can go to the browser and enter localhost:<port> and you’ll see something like this:

Now, to see our function, enter: http://localhost<port>/api/myfirstfunction?name=<name>

And see our function finally running.

Congratulations! You’ve already create a azure function on a docker container.

Conclusion

This step-by-step guide is a brief introduction about how to use Azure Functions and Docker Containers.

I hope you’ve got involved and learned about these technologies that are very very powerful.

I’ll leave my github repo if you want to see the final structure.

Have fun!