How to deploy Azure Functions into a Docker Container

In this post we’ll go step by step to learn the way to work with Azure Functions to deploy them later in a Docker Container. We’ll be coding in C# a simple example of Function, to see a tiny part of what you could really do with this.

What is Azure Functions?

Azure Functions is a Microsoft technology that allows us, as programmers, to worry just about coding an application or specific functions that help us solve the problem we have, without the tedium of working with the whole enviroment issues, delegating those tasks to the Azure plataform. You can learn a lot more about their uses and applications here: https://docs.microsoft.com/en-us/azure/azure-functions/

What is a Docker Container?

First of all we’ll see what a container is. A container is a tool we can use to encapsulate our applications and freeze them in images, to then be able to replicate different instances of them, coexisting at the same time and in the same Operative System. Inside this container images we’ll find everything we need to run those applications (code, system tools, libraries, etc.). On the other hand, Docker is an open source project that automates the deployment of applications within these containers. You can learn a lot more of what a container is and their functions here: https://www.docker.com/resources/what-container

So… What we need?

Important:

• For this guide I’ll be working with Windows. If you have another OS, follow this guide to install Azure CLI.

• To manage the files and build the image I’ll use GitBash, but you can use either PowerShell or Windows Command Prompt.

• I’ll use Visual Studio Code to open the created function and modify it, you can use whatever you want to do it.

• Finally (I swear), I’ll also use Postman, an app that provides an enviroment to test the functions, but you can open it in your browser like we’ll see later.

Now we’re ready.

  1. Start Docker Container for Windows (if it’s already running, close it) with Administrator Privileges. This is to avoid any kind of problem you may have when building the image. Then right click at the Docker icon and select “Switch to Linux Containers”. It should look like this:

2. Now you can open the command-line you’ve chosen, enter in the directory you want to create your function in, and type the next command:

func init . — docker will create an azure function project with docker support.

Then you’ll choose the language of your preference, I’ll work with C# as I said before, so dotnet will be. It should look like this:

3. The next step will be used to create our function, so type the next command:

With “-t” you can choose the template on which the function will be built, and “-name” is what it is, the name of the function that may be the one you want. Here you can see a list of the available templates you can work with.

4. Now, if we open in Visual Studio Code the folder where all of our files are, we should see this:

You can leave this code as it is, and if you run it (F5) and paste the endpoint provided in your browser (or in Postman as we’ll see now), you just have to add “?name=<your name>” next to it and press Enter. Now you should see this:

Note: if you want to use the default function you can go straight to the 7th step.

But I will change the code so that it simply recognizes whether or not the number entered is 2. If it is, the condition will be true, otherwise it’ll be false.

You can copy the code or download the project frome here.

5. Run the function with the new code:

6. If you paste the provided endpoint in Postman like I did, you should see that, if in Body we change the number value to any number different than 2, down in the Response window, numberTwo will be false. The next example shows how it looks if the value is 2:

7. Let’s dockerize it! First we need to change in our httpTrigger attribute the Authorization Level from “Function” to “Anonymous”, so we don’t get any error when building the image in Docker.

Then, write the next line in our command-line and you should get something like this:

“myfunction” is just the name I gave to the image, you can change it.

8. Great! Now our image is built we got to run it with the next command:

9. If everything is right, when you test it in your browser or in Postman, you should see the same as before, the function working but now from the Docker Container. Yay!

An example of the number value being 1

That’s it!

Now you can keep working and when you’re done you can check your running Container ID and kill it with the next commands:

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What *is* ‘Agile’?

Git Introduction

Weekly State of The Pad: Third Week of April 2022: Good Monday Edition!

Don’t Leave Parts Unknown: Meditations on Ethical Public API Design

MELX FRIDAY UPDATE: BIKE BATTLE WORLD MAP & MELALIE SPECIAL EDITION

How to ace your PSM I assessment

What are potential Job positions for computer science and IT major

CityJSConf talks to…Milecia McGregor

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Giuliano Taliano

Giuliano Taliano

More from Medium

Understanding Docker Architecture

Understanding Docker Architecture

Using MSBuild/VSBuild to prepare Docker image and deploy it to Azure App Service

Docker commands for software developers

Navigating Azure Storage Authentication Options using a Java Client