Wanna learn about Azure Functions + Docker?

Nicolás Regina
11 min readApr 17, 2019

Welcome my friend, If you are reading this it’s because you are interested in learning how Azure and Docker works… Well, I’ll try to teach you the essentials, the way I would have liked someone to teached me. So you can take this post for a starting point.

NOTE: this post is an introduction on this topic and I assume you are familiarized with some of this languages: C# or Node, in this tutorial i will be working with NodeJs

What is Azure?

more info here: https://bit.ly/1IGHEIj

Microsoft Azure is a cloud service that allows you to compile, deploy and manage applications in a global network of data centers from Microsoft.”

So people can access your services (if they have the rights credentials). It has several services such as infrastructures (storage, networks, virtual machines) and platforms (SQL databases, back-end for mobile applications, etc.). They are compatible with all types of technology such as Docker.

Another important point is that you pay for what you use, so you do not have to buy a complete server, let’s think about this: you have to make a test app and you know that you will only use it a few times, so whit Azure you can do it and just paying for those uses, is an economic solution for these cases. We will go a deeper in all this later.

Azure Functions⚡!

Well, now that we have an idea of what Azure is, let’s get more involved in what are the “Azure Functions”.

Let’s start with an overview video and then i’ll explain a little more…

An Official Overview of Azure Functions

We can call them as “a solution for easily running small pieces of code, or “functions,” in the cloud. You can write just the code you need for the problem at hand, without worrying about a whole application or the infrastructure to run it.”

So it makes it more scalable [more] and also has more attributes. For example, we want to send a notification once an item has been inserted into a database. You only have to call the event and Azure will do the rest.

functions runs only when an event or trigger happens so Azure is responsible to load the code (on the best environment for this). Azure functions provides templates, it could be very useful, you can see them here.

Other good point is that we can work in several languages like C#, Node.js, Python, etc… so you doesn’t have to think in train yourself in languages that might you doesn’t know

As we know, Azure is a “ Pay-per-use” (See the Consumption hosting plan option in the pricing section), and this makes Azure great, especially for startup’s or projects that might have a good future, because it gives you the chance of make your project scalable, safer, you can make it hybrid , etc. All mentioned here its basically the meaning of serverless, It is the abstraction of servers, infrastructure and operating systems [more].

Other features:

  • Bring your own dependencies: Functions supports NuGet and NPM, so you can use your favorite libraries.
  • Integrated security: Protect HTTP-triggered functions with OAuth providers such as Azure Active Directory, Facebook, Google, Twitter, and Microsoft Account.
  • Simplified integration: Easily leverage Azure services and software-as-a-service (SaaS) offerings. See the integrations section for some examples.
  • Flexible development: Code your functions right in the portal or set up continuous integration and deploy your code through GitHub, Azure DevOps Services, and other supported development tools.
  • Open-source: The Functions runtime is open-source and available on GitHub.

source: here.

What’s Docker? Basics “have to know” about Docker.

src: https://opensource.com/resources/what-docker

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers.

Containers allows a developer 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.

It grants you it’ll work in other systems disregarding its configuration as it is shown in the pic, docker image is outside the Host Operating System so it has all dependencies that it needs.[more]

In a way, Docker is a bit like a virtual machine. But you haven’t to create a whole virtual operating system, this gives a significant performance boost and reduces the size of the application. Docker has all the necessary elements to run your application properly, it’s very useful for testing and we can have running multiple containers as we want. [More Info]

Differences with Virtual Machine:

Containers and virtual machines have similar resource isolation and allocation benefits, but function differently because containers virtualize the operating system instead of hardware. Containers are more portable and efficient.

Some docker advantages:

  • Fast application deployment and portability across machines, it’s an abstraction at the app layer that packages code and dependencies together.
  • Sharing: you can use a remote repository to share your container with others.
  • Version control and component reuse, you can track successive versions of a container, inspect differences, or roll-back to previous versions.
  • Containers takes up less space than VMs (container images are typically tens of MBs in size)
  • Simplified maintenance, Docker reduces effort and risk of problems with application dependencies.

Some Contras of VMs:

  • VMs are an abstraction of physical hardware turning one server into many servers.
  • Each VM includes a full copy of an operating system, the application, necessary binaries and libraries — taking up tens of GBs. VMs can also be slow to boot.
  • You can create or destroy containers quickly and easily with Docker. Virtual Machines require full installations and require more computing resources to execute.
  • Virtual machines can be migrated while still executing, however containers cannot be migrated while executing and must be stopped before moving from host machine to host machine.

Now that we know what is each thing we can now start to put hands on, are you ready? (I hope you are …)

Getting started:

Net Core Installation:

https://dotnet.microsoft.com/download/thank-you/dotnet-sdk-2.2.203-windows-x64-installer

Checking your version of npm and Node.js

To see if you already have Node.js and npm installed and check the installed version, run the following commands:

node -v 
npm -v

if you have to install it click here.

Install the Core Tools package:

npm install -g azure-functions-core-tools@core
model of Google search: “how to enable virtualization on {yourBIOS} motherboard”

Note: maybe you can have this error, it is because your BIOS set the virtualization “Disabled” by default. My recommendation is search on Google how to enable it, because it depends on your Motherboard

Now that we have the environment we can start

Creating an Azure Function:

you should know that there are 3 easy ways to create an Azure feature (there are more options, but I recommend one of these): From Visual Studio, your Terminal (cmd) or the Azure Portal. Now we are going to work with Terminal because we want to create an Docker Image.

  1. Starting:
func init MyAzureFunctionOnDocker -- docker

we select the path were we want to create the function project, then put the name of “-- docker” to create our DockerFile that we will need later for dockerize our function.

Dockerfile is the configuration file with build instructions for Docker images. It provides a way to automate, reuse, and share build procedures.

Docker reads the Dockerfile and runs the instructions from top to bottom. Every instruction that is successfully executed creates a layer which can be reused the next time this or another image is built. It is very important to place instructions that will rarely change at the top of your Dockerfile. Doing so ensures the next builds of the same image are very fast because the cache is not invalidated by upper layer changes. You can see more about Dockerfile here and here.

Before the docker CLI sends the context to the docker daemon, it looks for a file named .dockerignore in the root directory of the context. If this file exists, the CLI modifies the context to exclude files and directories that match patterns in it. This helps to avoid unnecessarily sending large or sensitive files and directories to the daemon and potentially adding them to images using ADD or COPY.

.dockerignore does the same function that we have in Git with his file .gitignore ; For information about how to create a .dockerignore file see the documentation on this page.

2. We select the worker runtime that we will be using in the project:

dotnet: creates a .NET class library project (.csproj).

2nd arrow marks the dockerfile being created, This file is used to create a custom container in which to run the project

3. Create the Azure Function:

Now we are going to work with an HTTP trigger (event) so we must go to our project path

cd MyAzureFunctionOnDocker

and create the function, this will display you a list of templates

PATH\MyAzureFunctionOnDocker> func new
The project is created with HttpTrigger template.
Then I put the name of the function and it’s created

Perfect! you created your first Azure Function!

4. Modify AuthorizationLevel:

Now let’s open the .cs file and modify AuthorizationLevel, you need to change it, because the template comes with AuthorizationLevel.Function, it work’s fine for now but when we dockerize it, will return us a 401 request error, so we must change it to AuthorizationLevel.Anonimous to make it public

Modify it and then we can run our host to check everything is working fine.

  • extra: you can see the Dockerfile created at the begining just under the .cs

5. Run the localhost and check the urls:

func host start

When you run the sentence of host your cmd will show something like this ↓

one of the last lines says your url localhost, in this case is http://localhost:7071/api/AzureFunctionHelloDocker
It needs add the parameter that we want to test; in this case, “name”
Hello, Azure Function, nice to meet you!

6. Containerize the application with Docker!: Creating an image

Well, looks good, but to dockerize it we must stop the hosting on cmd just closing the console or press “ctrl + c” and select “y”. Now we need to build our Docker image, here you put the name you want for it.

docker build -t azurefunctionondocker_image.

When the command executes, you see something like the following output, which in this case is for a JavaScript worker runtime

azurefunctionondocker_image is the name of the image I created.

7. Run our function on Docker:

Now that we have already our image we can run it…

docker run -p 8080:80 azurefunctionondocker_image
sentence after restart docker in red box and under after the restart working fine

Red Box Marks an error you can have, to solve it just restart docker.

Now we can enter our function going to:

http://localhost:8080/api/AzureFunctionHelloDocker?name=%22Nico+Regina%22

http://localhost:8080/api/{yourAzureFunction}

Congratulations! we finished the tour!

8. Others:

We can see if everything is working fine typing

docker images

Lists every container running with all its details.

docker ps

Stops a specific docker container running.

docker stop [IMAGE ID]

Starts a specific docker container.

docker start [IMAGE ID]
Plus: here you have Top 10 Docker Commands *Very Useful*

Something personal:

I really, really hope you had enjoy this little trip, and thanks a lot for take the time of reading this.

If I helped you with this tutorial just left your clap so i can notice it!

I recommend you to keep advancing on this topic, because Micro-services and Serverless are the future

https://trends.google.com/trends/explore?date=today%205-y&q=Microservices#TIMESERIES

Next steps:

I will start whit something that I think is a very useful tool for a developer:

Postman: It is a Google Chrome app for interacting with HTTP APIs. It presents you with a friendly GUI for constructing requests and reading responses.

It’s really helpful to test methods without having to run an app, so try it!. Get Postman here.

I recommend you to start your own projects to improve your skills or try to adapt or migrate some modules or functions of a project where you work to apply something you have learned in this post, here I leave you some links:

Some of those could be difficult but you can start playing!, ENJOY!

Least but not last: I bring you some post, articles, courses and news that might be very helpful for you and had helped me making this post…

He explains very well Azure Functions!
See you on next challenge!!

--

--