The power of combine Azure Functions and Docker

Agustin Bellorini Mansilla
6 min readMay 22, 2018

--

In this step by step guide we are going to learn about what means combine Azure Functions and Docker and we will see an example of how create our first Function with Docker.

  • Introduction
  • Pre-requisites
  • Creating our first app

INTRODUCTION

First of all we need to know what is Docker and Azure Functions.

Docker

Docker is and open source project who let us build software containers, which allow us to deploy a complete app without worrying about the environment in which the app is going to run.

Before continuing we need to define two very important concepts of docker, which usually can confuse each other, they are containers and images.

Docker images themselves are never “started” and never “running”. The docker run command takes the Docker image as a template and produces a container from it.

But, what is a container? We can think in a container like a package in which we can save all the things that we need to run our app without problem, things like libraries, dependencies and all the development tools that we are using at the moment of the creation of the container.

Containers require far fewer resources (they all run on the same OS instance), they start fast and are easy to deploy.

We can make it clearer with a short example, suppose we are developing an app with java 8 and a teammate need to run it in his environment, but he has java 7, not java 8 like us, then our teammate have 2 options, or install java 8, or we can create an docker image of our app and send it to he, and he just need to have Docker and run the container without install anything

We can see how much portability this give us.

You can read more about this HERE

Azure Functions

Azure Functions is a serverless service that enables you to run a small piece of code, or ”function”, on-demand without having to provision or manage infrastructure. You can use your development language of preference, like C#, Node.js, Java or F#.

Using this we can focus only on the code to solve the problem and the rest azure solve them for us.

You can read more about this HERE

Pre-requisites

Pre-requisites to run azure on docker.

If you need it, you can see a tutorial to install Docker HERE

If we have all what we need, lets do it!

Creating our first app

With the all readed we can conclude that deploy azure functions on docker is a very interesting and powerful tool.

1.First of all we need to init an azure function with the option of docker like this:

func init . — docker

If you are in the folder that you want to create the Project you can use the dot, in other case you can name the folder like this:

func init funciontsAndDockerDemo — docker

We can see here “Writing Dockerfile”, this file contains the steps that Docker will going to use to create the docker image.

2. Inside the folder of the project we are going to create a new function with the command

Func new

Now we can choose the language, in this case i choose C#, but you can do it in Java Script too

Select a template, i selected HTTP trigger

And last give a name to the function.

The code of your function is saved by default in the file run.csx.

3. In the folder we have the files that azure create, one of this files is function.json, in this file we need to change the authLevel to anonymous, this is because in our first app we are not going to use an API key.

Before

After

** If you are not using Docker Toolbox you can jump to step 5

4. If we are using Docker Toolbox we can create a docker machine, Docker Machine is a tool that lets you install Docker Engine on virtual hosts, and manage the hosts with docker-machine commands, with the command:

Docker-machine create <name of the machine>

To see the machines that we have and their states we can run the command

Docker-machine ls

You can read more about docker machine HERE

5. Now we are able to create the docker image, for do this we are going to run the command

Docker build –t <name> .

6. Run it!

Now we can deploy our azure function on docker container

Docker run –p 8080:80 <name of the image>

The flag “-p” is used to expose the port that will we be available for the host, in this case the port is 8080

7. To check if your function is running you just need to open the explorer and go to the ip of the docker-machine and the selected port in the step 6, like you see in this picture

If you are running with the natively docker you just need to replace the ip for “localhost” and the result will be the same.

8. To see the result of the function we need to write the complete URL, in this case the function receive one parameter “name” and return a phrase with the name, the URL will have the form:

http://192.168.99.100:8080/api/demoFunction?name=yourName

In this case “demoFunction” is the name of my function.

If you are running with the natively docker you just need to replace the ip for “localhost” and the result will be the same, for example if the name of the function is the same, the url will be:

http://localhost:8080/api/demoFunction?name=yourName

Congratulations! You deployed your first azure function on docker.

You can see my project of a Caesar’s cipher with Azure Functions and Docker in https://github.com/BelloriniAgustin/AzureFunctionsAndDockerExample

This is only a small part of a lot of things you can do with these technologies, then i leave a serie of links that can help you if you want to read and learn more about this:

--

--