Codementor Events

Azure Functions + Docker Container

Published Sep 04, 2020
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)

1_yqwkTUnnp-OmSqSelnW-LA.png

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

1_Yo7ixCaV3BGK-vrNKdPwAg.png

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:

  1. Choose the language and create the dockerfile
func init . --docker

2020-09-04 11_24_02-Azure Functions + Docker Container _ by Germán González _ Medium - Brave.png

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.

  1. Create the function and choose the template.
func new

2020-09-04 11_25_15-Azure Functions + Docker Container _ by Germán González _ Medium - Brave.png

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.

  1. 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"
  1. Create the docker image.

Once you’ve change the authLevel, let’s jump to create our docker image.

docker build -t <dockerImageName> .

2020-09-04 11_27_27-Azure Functions + Docker Container _ by Germán González _ Medium - Brave.png

  1. 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

2020-09-04 11_28_58-Azure Functions + Docker Container _ by Germán González _ Medium - Brave.png

So now we can go to the browser and enter localhost:<port> and you’ll see something like this:

2020-09-04 11_32_56-Azure Functions + Docker Container _ by Germán González _ Medium - Brave.png

Now, to see our function, enter: http://localhost<port>/api/myfirstfunction?name=<name>
And see our function finally running.

2020-09-04 11_34_02-Azure Functions + Docker Container _ by Germán González _ Medium - Brave.png

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/
Discover and read more posts from Germán González
get started