You probably have heard about Azure Functions and Docker since they are becoming more and more popular every day. Here you will learn what they are and why they go great together! Plus a step-by-step tutorial on how to get started using them.
Ignacio Boada · Follow
13 min read · Sep 14, 2019
--
This post presents an overview of what Azure Functions and Docker are, and a step-by-step guide on how to start using Azure Functions With Docker
Let’s say you have a great idea for an application and you have some programming skills. So, you just want to write your code and show your product to the world. Well… you will find that is not so easy, you need servers, frameworks, and more.. Until NOW! With Azure Functions you can just code without having to worry about servers and all that, and with Docker you can run the same applications everywhere. That’s why these two make such a great team!
Let’s start by learning a little bit about what Azure Functions and Docker are. Along the way you will find some links about different subjects in case you are interested in deepening your knowledge about them.
Let’s see, what is Azure Functions? Azure Functions is a solution for easily running small pieces of code, or “functions,” in the cloud. Azure Functions acts as a modern serverless architecture delivering event driven cloud computing and configured to comply with application development.
If you don’t like reading so much, here is a short video (less than 2 minutes!) on Azure Functions.
Azure Functions is a great solution for processing data, integrating systems, working with the internet-of-things (IoT), and building simple APIs and microservices.
Some of the main benefits are:
- End-to-end development experience
- Variety of programming languages and hosting options
- No need for server maintenance.
- Automated and flexible scaling based on your workload volume
- Integrated security
- Open-source
And much, much more. You can read more about these benefits and Azure Functions on the official documentation web page.
Docker is here to save us from having to create different codes for different platforms, and to avoid hours of troubleshooting trying to figure out why an application doesn’t work on some computers. Thanks to Docker, if an application works on a computer, it will surely work on any computer.
But, what is docker? Docker is a tool that lets you create, deploy, and run applications by using containers. Okay, but what are containers and what’s so great about them? Technically a container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. This means that you have everything that your application needs to run on a big box that you can share, and when that box is open on another computer, everything in it will work just like it did on your computer, regardless of operating system or any particular configuration.
If you have worked on a team project, you know this would have solved many problems, like everyone wanting to use THEIR favorite OS taking hours of discussion between team members on why one OS is better than other. So, it’s not just a solution for deploying applications on final users, but it also helps developers.
Now, you might be thinking, “Hey, this reminds me of Write once, run anywhere from Java, so, are containers Virtual Machines?”. The answer to that question would be, No. What Virtual Machines do is recreate a whole operating system on top of the hosting operating system, which takes a lot of resources, while Containers are an abstraction at the app layer that packages code and dependencies together. Multiple containers can run on the same machine and share the OS kernel with other containers, each running as isolated processes in user space. Containers take up less space than VMs (container images are typically tens of MBs in size).
To understand a little bit more why Containers are better than Virtual Machines (VM) when trying to run apps, you can say that a VM is essentially an emulation of a real computer that executes programs like a real computer, while a container provides operating-system-level virtualization by abstracting the “user space”.
This diagram shows you that containers package up just the user space, and not the kernel or virtual hardware like a VM does. Each container gets its own isolated user space to allow multiple containers to run on a single host machine. You can see that all the operating system level architecture is being shared across containers. The only parts that are created from scratch are the bins and libs. This is what makes containers so lightweight. This is just a little summary. If you are interested in deepening your knowledge on the difference between VMs and Containers I highly recommend you read this post.
Now that you know what Azure Functions and Docker are, you can see why they go so well together!
The next step is to start using them!
NOTE: The installation process will be done on a 64-bits Windows 10 Home Edition operating system.
- Installing Docker
- If you have 64 bits Win 10 64 Pro or Enterprise Edition, the installation process it’s fairly simple, just click here and in the opened tab click “Download Desktop for Mac and Windows”, this will open a new tab where you should click on “Get Started with Docker Desktop”, this will open a step-by-step tutorial that you should just follow. (I don’t go into much detail on this tutorial sin I don’t have Win 10 Pro nor the Enterprise Edition. The installation on Windows 10 Home Edition is more detailed).
- If you have 64 bits Windows 10 Home Edition, Docker Desktop is not supported. If you try to install it you will get the following error:
Luckily, Docker offers the Docker Toolkit which is very similar to Docker Desktop but with a different interface.
With Docker Toolbox you will get the following Docker tools:
- docker
- docker-machine
- docker-compose
- Kitematic
- Boot2Docker ISO
- VirtualBox
Why are so many things needed? Because the Docker Engine daemon uses Linux-specific kernel features, you can’t run Docker Engine directly on Windows. Instead, you must use the Docker Machine command, to create and attach to a small Linux VM on your machine. This VM hosts Docker Engine for you on your Windows system.
To download it, click here. On the opened tab you will see that there are a lot of different versions of the Docket Toolkit, just install the latest one (in my case is version 19.03.1). Please ensure that your system has all of the latest updates before attempting the installation.
From here is pretty straight forward. Select where you want to save the file. Double click on the .exe file once it’s downloaded. Select where to install it (like any other program) and click on Next until it finishes with the installation process. If everything went well the following sign should pop up. Click on Finish.
Once you clicked on Finish, you should see a folder with these applications. Open “Docker Quickstart Terminal”
A terminal will appear, don’t be scared, it’s just the initialization process to get docker running on your computer for the first time. This will take a couple of minutes, and when it’s set up, you will see the iconic Docker Whale in the terminal, this means that everything went well. Cheers!
To make sure that there is no problem, you can run the following command line (and your first command line in the Docker terminal!):
docker run hello-world
At first it will say it’s unable to find the image, but don’t panic, just wait a little while and Docker will find it automatically. You should see something like this in your terminal if everything is okay:
Well done!
2. Quickstart on Azure Functions
To be free from using any IDE, we will use the Command Line to create our Azure Function.
First you need to Install Node.js, which includes npm. You can find it here. Download the .msi file and double click it. The installation process is very easy, just click Next, Next, Next and Install. If there were no problems, you should see something like this. Click on Finish.
Then open a terminal and Change directories until you are in your nodejs directory. There run the following line:
npm install -g azure-functions-core-tools
You should see something like this. If there are no errors, it means everything went well.
This will install the Core Tools package in your system.
The next step is installing the .NET Core 2.x SDK for Windows. You can find it here. Since we want to Build Apps, the “Build Apps” download should work fine (We won’t be using Visual Studio, so just click on the “Download .NET Core SDK”).
When the file finishes downloading, double click it, and click Install. Once it’s installed you should see this:
Since we want to use the Command Line to create Azure Functions, we need to install the Azure CLI. The Azure CLI is a command-line tool providing a great experience for managing Azure resources. The CLI is designed to make scripting easy, query data, support long-running operations, and more. To download it, go to this link and click on the “Download the MSI installer” button.
Like all other installations, double click on the downloaded file, accept the terms and conditions and hit Install. It will prompt for permissions, press Yes.
You should see this if everything went okay:
That was easy!
Finally, if you want, you can create an Azure account. You’ll need this if you want to upload your function to your Azure Portal.
3. Getting started
Now that you have everything installed, it’s time to create your project.
Since many of you probably decided that you didn’t want to give your credit card information until you are 100% sure that Azure Functions are really useful, let’s begin by creating a function locally.
First Create a folder where you want your project to be. Then open a Command Prompt by writing “cmd” on windows search, and go to that folder on the Terminal. In my case my project will be in “C:\Users\Nacho\FirstAzureFunctionWithDocker”
Now run the following command:
func init --docker
Select a worker runtime from the following language choices (use the arrow keys to move, enter to select):
dotnet
: creates a .NET class library project (.csproj).node
: creates a Node.js-based project. Choose eitherjavascript
ortypescript
.python
: for a Python project, please instead complete Create an HTTP triggered function in Azure.powershell
: for a PowerShell project, please instead complete Create your first PowerShell function in Azure.
I chose dotnet because I will create my function in C#. I recommend you follow my steps for your first function, since there are differences in the creation process between each option. Once you understood the first steps, just then, you can spread your wings and fly on your own.
You will see that many files where created on your project folder. These are all the files needed for the project.
As you can see, a Dockerfile was created on the project folder. This file is used to create a custom container for your project. This is all you need to add Docker to you Azure Functions. Now you can focus on Azure.
On the Command Prompt type func new
You will see a list of possible Template to select. Here’s a short description of the main ones:
- HTTPTrigger — Trigger the execution of your code by using an HTTP request. For an example, see Create your first function.
- TimerTrigger — Execute cleanup or other batch tasks on a predefined schedule. For an example, see Create a function triggered by a timer.
- QueueTrigger — Respond to messages as they arrive in an Azure Storage queue. For more information, see Azure Queue storage bindings.
- EventGridTrigger — Respond to events delivered to a subscription in Azure Event Grid. Supports a subscription-based model for receiving events, which includes filtering. A good solution for building event-based architectures. For an example, see Automate resizing uploaded images using Event Grid.
To see more on templetes you can go to the Azure Functions Overview webpage.
I will use the HTTPTrigger template since it’s the easiest one to start using Azure Functions.
Once you’ve selected the template, you will have to choose a function name. I named my function “FirstAzureFunctionWithDockerFunction”.
You have created your first function!
Let’s check the code. On your project folder open the .cs file (by the way, I like to use notepad++, it’s a fast and very powerful tool that supports almost all programming languages).
Look at all the code you have, and you haven’t written a single line. Templates are just great!
If you want to share your functions, you can always use GitHub. I uploaded my project to this GitHub repository.
To make things easier to use, change the AuthoritationLevel on line 17 from “Function” to “Anonymous”, this way you won’t have to remember which key you assigned or anything. If you are interested in Authoritation levels, you can learn all about them here.
Now you can call the function without having to supply the function key (The function key is never required when running locally, but it will be in a Docker Container).
Let’s go back to containers. You want to run your function on a container. To achieve this you need to build an image of your function.
Open “Docker Quickstart Terminal” and go to the “Docker Toolbox” folder. There run this line (imageName is the name that you want for your Docker image):
docker build -t <imageName> Path_to_the_project
I called my image “firstimage”. Note that images names must be in lowercase.
The building process might take a while, be patient.
The next step running the image that you created. Run the following command , still on the “Docker Tools” folder:
docker run -p <port> <imageName>
To avoid conflicts I recommend you use the port 8080:80. If there are no problems, you should see something that starts and ends like the following images:
And with this you have the custom image running in a local Docker container!
Now you can see the execution of your function directly on an internet browser. To do this all you need to do is to write this on the URL:
http://<docker-machine ip>:<port>/api/<functionName>?name=<yourName>
To find the ip of your docker-machine, just excecute docker-machine ip. This is just for those who have Docker Toolbox. If you have Docker Desktop you type “localhost” instead.
The template Function shows me this.
And that’s it!
Now you know what Azure Function and Docker are, and the whole process on how to use Azure Functions with Docker!
But this is just the beginning, Azure Functions and Docker have much more to offer, and trust me, it’s in your best interest to know as much as you can about these technologies because they are the future. They are growing fast, becoming popular, and making life easier to everyone.
I recommend you to complement this tutorial by reading the official documentation on Azure Functions. The information is clear, precise, and up to date since they are the ones who make changes. I think that Investigating more on what can you do with Azure Functions is the next step, since it will help you understand the huge potential of Azure Functions. So, click here and start your way into becoming an Azure Functions expert!
With great knowledge on Azure Functions, and the capability of using containers to share you developments, you will be unstoppable!
Another choice is to watch educational videos. In my opinion videos are more friendly and easy going than long texts. If you like videos too, I highly recommend this series. Mostly short videos, so that you don’t become overwhelmed, and with continuous growth on subjects, so that you don’t have to start over every time as it normally happens with isolated tutorials.
To keep going, if you are interested in the serverless features that Azure provide, you can what this video.
Or maybe containers just blew your mind and want to go deeper in that area, you can read this article.
On the internet there is a tremendous amount of information. Don’t be anxious, don’t try to read and know absolutely everything. Focus on some tutorials, and remember that it’s better to learn a lot from a few than little from many.
Thank you very much for reading my post!
I hope you learned a lot and enjoy yourself while doing it. I am sure this knowledge will be of great use in your future.
Feel free to share it with your colleagues and friends, and maybe this is the first step towards your own team project!