top of page
Writer's pictureAlibek Jakupov

Azure Functions : Custom Docker Configuration

Updated: Nov 19, 2021


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:

  1. Create a function app and Dockerfile using Core Tools.

  2. Build a custom image using Docker.

  3. Publish a custom image to a container registry.

  4. Create an Azure Storage account.

  5. Create a Linux App Service plan.

  6. Deploy a function app from Docker Hub.

  7. Add application settings to the function app.

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

  1. Create a dockerhub account

  2. Create your function with a virtual environment (.env) and a requirements file

  3. Add a docker file to the root folder.

  4. Build a docker image.

  5. Push the image to your docker hub

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


FROM mcr.microsoft.com/azure-functions/python:2.0

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

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:


-FunctioName

-Dockerfile

--FunctionName/__init__.py



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

localhost:8080/api/YourFunctionName

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

your-dockerhub-id/mydockerimage:v1.0.0

Here we are, you have your service up and running. Enjoy




2,206 views2 comments

2 Comments


ajakupov
May 17, 2020

@donniekerr01 great to hear this was useful for you! And thank you for your feedback, that's very kind of you

Like

donniekerr01
May 17, 2020

Works great! Got pdf2image working in a blob-triggered Azure Function! Appreciate it! Just FYI, In my use case, user uploads PDF to blob storage which triggers the function. Blobs come in as InputStream. Had to convert it to bytes. bytes = myblob.read() images = convert_from_bytes(bytes,dpi=300,fmt="png")

Pretty cool! I originally did this process in Node using pdf2png-mp but ran into similar problem with ghostscript requirement. But, I think I'm liking the Python solution better thanks to you!

Like
bottom of page