There is an excellent official documentation of Microsoft explaining how to create a simple Azure Function on Python.
If you create it with Visual Studio code things become even easier, you only need to use Ctrl+Shift+P and select one of the predefined commands. If some of your packages are extremely heavy, Azure functions extension creates a docker image and runs your function from automatically.
However, I have recently faced a serious challenge in creating a function that converts a PDF into an image using pdf2image package via Azure Functions. The main reason is that this package is only a wrapper to poppler utils, a linux package that actually does all the magic. And if you are working locally the only thing you need to do is to launch:
sudo apt-get -y install poppler-utils
and it works like a charm.
Great,but i needed to install this on the Azure Functions. I had no knowledge on how Docker works and was really wondering if there is a simple way to add this simple command in my environment. First of all I started analyzing the official documentation and did not understand a thing. As the introduction says, the steps you need to implement are:
Create a function app and Dockerfile using Core Tools.
Build a custom image using Docker.
Publish a custom image to a container registry.
Create an Azure Storage account.
Create a Linux App Service plan.
Deploy a function app from Docker Hub.
Add application settings to the function app.
Enable continuous deployment
Well, sound intelligent, but still I wanted a really simple solution, and which is most important, I was still willing to use Visual Studio Code environment. So after several days of struggling I started to understand (yet, vary basically) the basic principle.
So, in a couple of words, the principle is the following.
Create a dockerhub account
Create your function with a virtual environment (.env) and a requirements file
Add a docker file to the root folder.
Build a docker image.
Push the image to your docker hub
Crete a function in Azure. Important: while creating a function you need to choose docker (and not code) as a source. Here is the trick: when the function is created it actually pulls you image.
Here I am going to provide all the steps I implemented to set my PDF conversion service up and running.
Create a function
Using Visual Studio Code create a new function project.
In the Activity Bar, click on the Azure logo to show the AZURE Functions explorer and click the Create Project icon.
This will create a virtual environment. You can now develop your application
Add a docker file
Now you can the following docker file to a root folder.
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
COPY . /home/site/wwwroot
RUN apt-get update && \
apt-get -y install sudo
RUN sudo apt-get -y install poppler-utils
RUN cd /home/site/wwwroot && \
pip install -r requirements.txt
So the structure be the following:
Build a docker image
First create a docker hub account. Important: Memorize your docker hub id as you will need it later.
Now you only need to build your docker image and test if the function works properly on the docker.
docker build --tag your-dockerhub-id/yourdockerimage:v1.0.0 .
Important: do not forger . at the end of the command
Run a docker image
If the function executed successfully you only have to test if everythong is working fine.
docker run -p 8080:80 -it your-dockerhub-id/yourdockerimage:v1.0.0
N.B. This time, no need to add point at the end of the command.
To test your function go to
Push the image to docker hub
docker login --username your-dockerhub-id
And now push
docker push your-dockerhub-id/mydockerimage:v1.0.0
Create a function on Azure
Now when you create a new function in Azure you need to select Linux and choose Docker as the source. In the docker configuration section put the following text
Here we are, you have your service up and running. Enjoy