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
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:
- 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.
- 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.
- 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:
"authLevel": "function"
to
"authLevel: "anonymous"
- Create the docker image.
Once you’ve change the authLevel, let’s jump to create our docker image.
docker build -t <dockerImageName> .
- Run the container.
Finally, run the container executing this command:
docker run -p 8080:80 <dockerImageName>
-p 8080:80 means that we map our 8080 local machine port against the 80 Docker container port
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!
GET INVOLVED
If you enjoyed this post and you want learn deeply:
- https://www.dynatrace.com/news/blog/azure-services-explained-part-2-azure-functions/
- https://azure.microsoft.com/en-us/blog/introducing-azure-functions/
- https://www.docker.com/why-docker
- https://docs.docker.com/