Azure Functions with Docker

Matias Gonzalez
4 min readMar 15, 2019

--

A simple example of Azure Functions using Docker.

Azure Functions and Docker are a perfect match for micro services in a hybrid world. This combo gives you the flexibility to easily deploy and run your micro services either in the cloud or on-premises. In this example we’ll see the possibility of running your Azure Function App in a Docker container.

Before we start…..What are Azure Functions?

Serverless computing is the word of the day, and Microsoft Azure Functions acts a modern serverless architecture delivering event driven cloud computing and configured to comply with application development. Through Azure Functions, you can author and execute snippets of code in the cloud without the hassle of managing the web servers. So that’s why we say it has serverless architecture. It lets you run small pieces of code and the developer doesn’t have to worry about the infrastructure of the platform on which it is executed. If you need more information you can go to: Azure Functions

What do you need?

To follow this example, you need:
- Azure Account (optional)
- Azure-CLI
- Azure Functions Core Tools
- Node.js (8.5 or greater)
- Docker. You can download here. Note: Requires Microsoft Windows 10 Professional or Enterprise 64-bit. For previous versions get Docker Toolbox.

Why Docker?

The Docker goal is to ease the creation, deploy and the delivery of an application using the so called Containers. The Docker Containers allow the developer/sysadmin to bundle an application with all needed components (libraries and other resources) and to deliver it as an independent and single package.

Well, enough of words, let’s have fun…Let’s start.

Step 1: Open Command Prompt and create the project folder.

md [PROJECT-NAME]

Step 2: Go to the created folder and run the following command to create the files that you’ll need for the project.

func init . --docker

You must to choose a worker runtime. For this example I’ll use node.

Worker runtime

And select the language. You can use TypeScript if you want. I’ll use JavaScript.

Language JS

Step 3: Run the following command:

func new

When you press enter you’ll create a new function and you’ll select a template. I‘ll use a HTTP Trigger so I choose that option as shown in this picture:

Select a template.

Press Enter again and you have to enter a name for the function. For this example I’ll call it “azurefuncdemo”.

Name for the function

Step 4: Go to the folder and open function.json file and change the Authorization Level from “function” to “anonymous”. Modify it as shown in this picture:

function.json

Step 5: You need to add a new Docker container. Where “azureFuncDemo” is the name of the function that you created in the Step 3.

docker build -t azurefuncdemo .

Step 6: Now you are ready to run your image and deploy your Azure Function.

docker run -p 8080:80 azurefuncdemo

And that’s all. You have successfully deployed an Azure Function in a Docker container. You don’t believe me? No problem! Let’s check it together!!!

I am using Docker Toolbox, so I need to check the IP that Docker used. The IP will be 192.168.99.100. So, if you go to “http://192.168.99.100:8080/” you’ll see your app working. But if you are with windows 10 you can go to “http://localhost:8080/”.

app working.

And if you want to test your function you can go to: “http://192.168.99.100:8080/api/azurefuncdemo?name=matias” and you will see your function working. Or remember, if you are with windows 10 you must go to “http://localhost:8080/api/azurefuncdemo?name=matias".

function working.

Extra: If you want you can test it with Postman too. Let’s do it:

function working with Postman

Note: you can download my project in GitHub from here. My project receives a name and one number to show the multiplication table. Please see the next picture:

Well, we’re finished… You know now how to create a simple Azure Functions with Docker. For more information you can visit these links:

Official page Azure Functions
https://docs.microsoft.com/en-us/azure/azure-functions/

Official page Docker
https://www.docker.com/

My project @ GitHub: https://github.com/Matiaslg10/azure-functions

Azure Functions with Docker
https://blog.leitwolf.io/azure-functions-docker-getting-started/

A little more of Docker
http://www.clickode.com/en/2016/01/26/why-docker-important/

--

--